Once your members are able to sign in, they should have the option to sign out. Create a file called
"logout.php" and if a member is logged in, display this link so that they may access it. Below is the code
used to successfully logout a user:
As you see, we set $_SESSION['logged_in'] to equal 0, because it is set to 1 when they are signed in.
Next, notice we are setting another cookie. Notice though, the setcookie( ); function. We are not setting a
cookie, but deleting the one specified. This is achieved by setting a negative time. In this example I used
time() - 60. This will successfully delete the cookie that was set if the user chose "Remember me."
Finally, a call to session_destroy(); will delete all of the data that is in the current session.
Afterwards, we use header( ); to automatically reload us to a desired page. In most instances, the index or
main page of your site if you so choose.
CONCLUSION
Well, Kudos to those who actually read through the entire tutorial! If you did, you should be able to
grasp the concept pretty easy. I didn't care how long the tutorial would turn out to be. I wanted to make
sure that everyone who read through would be able to have a good start at building a member system for
their own website. I did not write the tutorial in one setting, this is probably a couple of months worth
of 10 minuts free time here and there. So if any part seems repetitive, irrelevant or off topic in any way
just let me know and I can make revisions.
In this tutorial, code for scripts are broken up into alot of parts. So here is the full code for each
script. If anyone has any questions, feel free to join discussion in our AllSyntax.com Forums!
Subject: "session logged_in is zero"
Date: May 01 2007 at 10:47 am
I'm a PHP noob. I implemented the script and everything looks ok and works from a database standpoint but I do not think the session from the login is getting set. After login, I redirect to a page called usercp.php which I included the page header code in. I check the session[logged_in] befor the "check cookie" and the session is set to 0.
Make sure your session code to recognize members is in your "page header" code. Also that the code in your page header is executed to check login status before anything else.
You may need to bring this to the forums and start a topic, post some of your code and we can help you from there.
Subject: "Error in Login.php"
Date: Jun 14 2007 at 1:03 am
I have looked over the code for quite a bit, can't figure it out.
Here is my error...
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\MyLogin.php:6) in C:\wamp\www\login.php on line 27
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\wamp\www\login.php on line 36
Login Failure: An error occured, please verify your username and password are correct.
if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
//REDIRECT TO USERS PROFILE...
header("Location: ../www/temp.html");
} //end if logged in
if(!$_POST['username']) die("Error: You must enter your username before logging in.");
if(!$_POST['password']) die("Error: You must enter your password before logging in.");
//set cookie if checked
if(!empty($_POST['stay_in'])) {
$joined =''.$_POST['username'].'[]'.md5($_POST['password']).'';
setcookie('login_cookie', $joined, 2147483647, '/', '../www/MyPage.htm');
} //end if
//verify user...
$get_user = mysql_query("SELECT * FROM `familymembers` WHERE username = '".$_POST['username']."' AND
user_password = '".md5($_POST['password'])."'");
$q = mysql_fetch_object($get_user);
if(!$q) die("Login Failure: An error occured, please verify your username and password are correct.");
Duece in answer to your question i had the same problem its to do with the "\" it messes up the code, if you view it in notepad++ you''ll see that nearer the end of that line it turns into a comment and therefore doesnt read the ");" the only way i found to fix it was by adding another "\" so it was '\\'. Wether or not this will still block them from using "\" is another matter.
Subject: "Error login.php"
Date: Jun 14 2007 at 6:14 am
In answer to your second question i couldnt see much wrong or different to my code the only thing i could think of was the header location maybe put the precise location of it I.E include the http:// other then that i dont know, im only a php noob myself. On another note i'd like to thank bs0d for creating this tutorial, it has been really helpful for me.
Subject: "Discuss in Forums"
Date: Jun 14 2007 at 4:32 pm
If you have specific issues, please discuss them in the forums. To answer your question, as the tutorial states, you may have to use ob_start(); as the first line of code on your page to eliminate the "headers already sent" error(s).
Subject: "logout.php"
Date: Jun 22 2007 at 3:28 pm
Ok i belive that i have the correct format for the logout.php script. At first i had the same 2 similar problems as duece and corrected based on your advice. The problem is not that when i hit the logout link i dont think that it logs out instead im am redirected to my home page which is not my index page. i also set a part of my page to echo the $user but that is blank.. before i loged in i said welcome guest but now i cant tell if its still working dut to the logout problem. please advise.. this is the code for the whole logout page.
Subject: "Integrate"
Date: Dec 20 2007 at 10:22 pm
Hi!
Thank you for your excellent tutorial. This is just what I've been looking for. I have a quick question though: is it possible to integrate this with your Comments Script? Kinda like this site~
Subject: "need help with logout function"
Date: Nov 07 2008 at 9:08 pm
my error is as follows
Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in /home/wemwngdt/public_html/logout.php on line 6
Warning: Cannot modify header information - headers already sent by (output started at /home/wemwngdt/public_html/logout.php:6) in /home/wemwngdt/public_html/logout.php on line 7
ive created a link on my member page to logout with the following code:
//check cookie
if ($_SESSION['logged_in'] != 1 && isset($_COOKIE['login_cookie'])) {
list($user, $pass) = explode('[]', $_COOKIE['login_cookie']);
$qu = mysql_query("SELECT `user_password` FROM `members` WHERE `username` = '".addslashes($user)."'");
if (mysql_num_rows($qu) == 1) {
$passw = mysql_fetch_object($qu);
if ($passw->user_password == $pass) {
$_SESSION['logged_in'] = 1;
$_SESSION['username'] = $user;
$_SESSION['password'] = $pass;
}
}
}
if(!isset($_SESSION['username']) && !isset($_SESSION['password'])) {
$_SESSION['logged_in'] = 0;
$user = "Guest";
}
?>
and the loginpage with the following:
if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
//REDIRECT TO USERS PROFILE...
header("Location: welcome.php");
} //end if logged in
if(!$_POST['username']) die("Error: You must enter your username before logging in.");
if(!$_POST['password']) die("Error: You must enter your password before logging in.");
//set cookie if checked
if(!empty($_POST['stay_in'])) {
$joined =''.$_POST['username'].'[]'.md5($_POST['password']).'';
setcookie('login_cookie', $joined, 2147483647, '/', '.welcome.php');
} //end if
//verify user...
$get_user = mysql_query("SELECT * FROM `members` WHERE username = '".$_POST['username']."' AND
user_password = '".md5($_POST['password'])."'");
$q = mysql_fetch_object($get_user);
if(!$q) die("Login Failure: An error occured, please verify your username and password are correct.");