j.smith1981 Posted February 23, 2011 Share Posted February 23, 2011 I am having problems understanding the reason for why the user has to click logout twice, here's the bulk of the code: <?php ini_set('display_errors',0); require_once 'header.html'; require_once 'db.functions.php'; require_once 'config.php'; $database = dbConnect($host, $username, $password, $database); // should output 1 or nothing at all! if($database == true) { // now connected? // carry on with logic of outputting the blog contents: $result = entries("SELECT * FROM entries"); printf("<table>"); while($row = mysql_fetch_array($result)) { printf(" <tr> <td>%s</td> <td>%s</td> </tr> <tr> <td colspan=\"2\">%s</td> </tr> ", $row[2], $row[4], $row[3]); } printf("</table>"); printf("\n\n"); session_name("jeremysmith_blog"); session_start(); if(array_key_exists('login',$_SESSION)) { if($_SESSION['login'] == 1) // change this to correspond with session on the login.php script { printf("<p>Welcome %s</p> <p>To logout, click <a href=\"index.php?action=logout\">here</a></p> ",$_SESSION['username']); } } else { printf("<p>You are not logged in, please click <a href=\"login.php\">here</a> to login.</p>"); } } else { printf("\n<p id=\"error\">Could not connect to database, please try again later.</p>"); } // init the logout script? if(array_key_exists('action',$_GET)) { if($_GET['action'] == 'logout') { // log user out of the system: unset($_SESSION['login']); unset($_SESSION['username']); session_destroy(); } } printf("\n"); // just for output format! require_once 'footer.html'; Why does the user have to click logout twice, have I missed anything? Any helps appreciated thanks. Quote Link to comment https://forums.phpfreaks.com/topic/228606-using-a-logout-script-user-has-to-click-the-logout-link-twice/ Share on other sites More sharing options...
kenrbnsn Posted February 23, 2011 Share Posted February 23, 2011 Move these lines <?php session_name("jeremysmith_blog"); session_start(); ?> to right after the opening "<?php". Since you currently have them after you send output to the browser, the session isn't being started correctly. Ken Quote Link to comment https://forums.phpfreaks.com/topic/228606-using-a-logout-script-user-has-to-click-the-logout-link-twice/#findComment-1178661 Share on other sites More sharing options...
j.smith1981 Posted February 23, 2011 Author Share Posted February 23, 2011 Of course! Sorry heads not in gear, sorting out my login security, getting some much needed advice on this and I forgot that part. Nice one, thank you ever so much! Jez. Quote Link to comment https://forums.phpfreaks.com/topic/228606-using-a-logout-script-user-has-to-click-the-logout-link-twice/#findComment-1178696 Share on other sites More sharing options...
j.smith1981 Posted February 23, 2011 Author Share Posted February 23, 2011 No its still doing that, where the user has to click twice, I now have this: <?php ini_set('display_errors',0); session_name("jeremysmith_blog"); session_start(); require_once 'header.html'; require_once 'db.functions.php'; require_once 'config.php'; $database = dbConnect($host, $username, $password, $database); // should output 1 or nothing at all! if($database == true) { // now connected? // carry on with logic of outputting the blog contents: $result = entries("SELECT * FROM entries"); printf("<table>"); while($row = mysql_fetch_array($result)) { printf(" <tr> <td>%s</td> <td>%s</td> </tr> <tr> <td colspan=\"2\">%s</td> </tr> ", $row[2], $row[4], $row[3]); } printf("</table>"); printf("\n\n"); if(array_key_exists('login',$_SESSION)) { if($_SESSION['login'] == 1) // change this to correspond with session on the login.php script { printf("<p>Welcome %s</p> <p>To logout, click <a href=\"index.php?action=logout\">here</a></p> ",$_SESSION['username']); print_r($_SESSION); } } else { printf("<p>You are not logged in, please click <a href=\"login.php\">here</a> to login.</p>"); } } else { printf("\n<p id=\"error\">Could not connect to database, please try again later.</p>"); } // init the logout script? if(array_key_exists('action',$_GET)) { if($_GET['action'] == 'logout') { // log user out of the system: session_unset($_SESSION['login']); session_unset($_SESSION['username']); session_unset($_SESSION['type']); session_destroy(); } } printf("\n"); // just for output format! require_once 'footer.html'; Have I obviously missed something sorry? Jez. Quote Link to comment https://forums.phpfreaks.com/topic/228606-using-a-logout-script-user-has-to-click-the-logout-link-twice/#findComment-1178703 Share on other sites More sharing options...
xylex Posted February 23, 2011 Share Posted February 23, 2011 You're doing the login check and outputting the logout link before you're logging the user out, so they are being logged out on the first click; the display just isn't reflecting that until the reload.. Move the logout piece up to the top right after the session_start() cal.l Quote Link to comment https://forums.phpfreaks.com/topic/228606-using-a-logout-script-user-has-to-click-the-logout-link-twice/#findComment-1178704 Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2011 Share Posted February 23, 2011 Read the manual entry for session_unset. Quote Link to comment https://forums.phpfreaks.com/topic/228606-using-a-logout-script-user-has-to-click-the-logout-link-twice/#findComment-1178705 Share on other sites More sharing options...
j.smith1981 Posted February 23, 2011 Author Share Posted February 23, 2011 Sorry my mistake it works now. Sorry its just last time I did this, used a seperate page but wanted to keep it as much on one page as possible. Thanks again, Jez. Quote Link to comment https://forums.phpfreaks.com/topic/228606-using-a-logout-script-user-has-to-click-the-logout-link-twice/#findComment-1178716 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.