...

View Full Version : Help Logout destroy_session not working for me.



cabnet
02-19-2012, 03:08 AM
Hi!

i have a problem with my log out script i can log in but log out not working for me here are my codes.

My main.php this page supposed to be my private page you need to login with your true username and pass, before you can view this page, but still
accessible even without login you can access it directly to the browser. I need help on how to make this codes work properly maybe I just put the code in the wrong place..
==================
main.php
================

<?
session_start();
if(!session_is_registered("username")){
header("location:index.php"); // <--is this correct?
}
?>
<?
session_start();
session_destroy(); <--is this correct here i put my log out?
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to the main page</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="container">
<div id="header"><img src="../img/banner1.gif"/></div>

<div id="contents">
<table>
<ul>
<li><a href="Logout.php">LogOut</a></li>

</ul>
</table>


<h1>under construction</h1>




<table>


</table>









</div><!--contents-->
</div>
</body>
</html>

==================================
my check log in code




<?php
$host="localhost"; // Host name
$username="*********"; // Mysql username
$password="********"; // Mysql password
$db_name="my_db"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$username=$_POST['username'];
$password=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$mysql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($mysql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:main.html");
}
else {
echo "Wrong Username or Password";
}
?>
=========================
my success_login.php



<?php
session_start($username);
if(!session_is_registered($username)){
header("location:index.html");
}
?>
<html>
<body>
<h2>Login Successful</h2>
</body>
</html>

======================
My logout_php



<php?
session_start();
session_destroy();

?>

I need to know the where to put the log-out code? that when the user want to log-out it will redirect to my index


Many thanks in advance I hope You can help I am just the newbie in php tnx.

firepages
02-19-2012, 09:07 AM
try
unset($_SESSION); which might give you better results, you can also explicitly delete the session cookie , see the example at http://php.net/manual/en/function.session-destroy.php though I have never found that necessary

cabnet
02-19-2012, 10:15 AM
try
unset($_SESSION); which might give you better results, you can also explicitly delete the session cookie , see the example at http://php.net/manual/en/function.session-destroy.php though I have never found that necessary


Thank you sir....

joda
02-19-2012, 06:54 PM
Try this:



// this should be in your login function or form
session_start();
$_SESSION['username'] = $username;


if you logged in then you started a session witch is recognized as the logged in user username. In my case it is the $username and I store it in the $_SESSION['username'].

When you wish to log out and destroy the session then you have to do this:



// this should be in your logout function or form
session_start();
unset ($_SESSION['username']);
session_destroy();


This should unset the stored session name variable and destroy the session .

cabnet
02-20-2012, 12:19 PM
Try this:



// this should be in your login function or form
session_start();
$_SESSION['username'] = $username;


if you logged in then you started a session witch is recognized as the logged in user username. In my case it is the $username and I store it in the $_SESSION['username'].

When you wish to log out and destroy the session then you have to do this:



// this should be in your logout function or form
session_start();
unset ($_SESSION['username']);
session_destroy();


This should unset the stored session name variable and destroy the session .

Many thanks sir..

joda
02-20-2012, 09:09 PM
Well done, if you can please click on "Thank User for this post" I'll appreciate it... :-)

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum