TheJoey Posted October 9, 2009 Share Posted October 9, 2009 Just wondering how i would create a logout function Would it just be <?php session_start(); HTML CODE unset($_SESSION['login']); header('location index.php'); ?> or should i use session_destroy mind you that im using sessions to maintain information from my shopping cart also. Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/ Share on other sites More sharing options...
mikesta707 Posted October 9, 2009 Share Posted October 9, 2009 if you test being logged in by (isset($_SESSION['login']) than that should work fine. have you tried it? Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-933761 Share on other sites More sharing options...
GKWelding Posted October 9, 2009 Share Posted October 9, 2009 This is a login class I wrote a long time ago, hope it helps you: class login{ var $response; function set_user_login($username, $password) { $username=mysql_real_escape_string($username); $password=mysql_real_escape_string($password); $query = "select salt from user where username='$username' limit 1"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { $user = mysql_fetch_array($result); $encrypted_pass = md5(md5($password).$user['salt']); $query = "select ID, username from user where username='$username' and password='$encrypted_pass'"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { $user = mysql_fetch_array($result); $userid = $user['ID']; $encrypted_id = md5($user['userid']); $encrypted_name = md5($user['username']); $_SESSION['userid'] = $userid; $_SESSION['username'] = $username; $_SESSION['encrypted_id'] = $encrypted_id; $_SESSION['encrypted_name'] = $encrypted_name; $this->response = 'Correct'; } else { $this->response = 'Invalid Password'; } } else { $this->response = 'Invalid Username'; } } function get_user_login() { return $this->response; } function set_user_logout() { session_unset (); session_destroy (); echo"You have been successfully logged out, you will be redirected shortly."; echo"<META HTTP-EQUIV=\"Refresh\" Content=\"2;URL=index.php\">\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-933787 Share on other sites More sharing options...
TheJoey Posted October 10, 2009 Author Share Posted October 10, 2009 im not using databases. but thanks. Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934118 Share on other sites More sharing options...
TheJoey Posted October 10, 2009 Author Share Posted October 10, 2009 would that only unset the login function and not the cart session. Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934119 Share on other sites More sharing options...
mikesta707 Posted October 10, 2009 Share Posted October 10, 2009 it unsets whatever variable you haveit unset, and no more. have you tried it Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934123 Share on other sites More sharing options...
TheJoey Posted October 10, 2009 Author Share Posted October 10, 2009 well yer.. it destroys the session but im not sure im declaring it right. Do i have to use <? session_start(); isset($_SESSION['login']); ?> or just session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934124 Share on other sites More sharing options...
mikesta707 Posted October 10, 2009 Share Posted October 10, 2009 that doesn't make any sense. isset returns a boolean value, so its only of use in a control statement (if, while, for, etc.) what are you trying to do with that snippet? Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934125 Share on other sites More sharing options...
TheJoey Posted October 10, 2009 Author Share Posted October 10, 2009 Sorry i did a little bit of research. It works fine. just wanted to understand it a little more. Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934126 Share on other sites More sharing options...
TheJoey Posted October 10, 2009 Author Share Posted October 10, 2009 Redirecting You Now Warning: Cannot modify header information - headers already sent on line 48 im getting this error for this code its included in the body of my html code. <?php unset($_SESSION['loginsuccessfull']); echo 'Redirecting You Now'; header('location : ../../index.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934131 Share on other sites More sharing options...
cags Posted October 10, 2009 Share Posted October 10, 2009 headers can only be sent before output has begun, echo causes output (obviously, thats it's whole point ) so you cannot use the header function after using echo. Incidently there would be no point in echo'ing that to the screen anyway as the redirect is instantaneous, so just remove the echo statement. You should also put exit; on the line below the redirect as the script will continue to run before the redirect otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/177098-solved-logout-script/#findComment-934252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.