PHPSuperNewb Posted April 26, 2011 Share Posted April 26, 2011 hello guys, this is my first post on this forum and I really need your help on this. What I have is: I created a login page a home page and a index page. The index page checks if the session is set. If it's not the login page will be shown. If it is the homepage will be shown. Whenever the user logs in, the session gets set. The problem is is that whenever I login and the user presses the back button on his browser my session will always be returned false which means that whenever a user has logged in, the index page doesn't show home but shows the login page again even though the user has already logged in. Here is my code to make you understand a little bit better: session.php: <?php class Session { function __construct() { } function set($name, $value) { $_SESSION[$name] = $value; } function get($name) { return $_SESSION[$name]; } function stopSession() { unset($_SESSION); session_destroy(); } function startSession() { if(!isset($_SESSION)) { session_start(); } } function check_session() { if(isset($_SESSION['username']) && !empty($_SESSION['username'])) { return true; } else { return false; } } } ?> login.php: <?php class Handler_Login extends Action_Handler { function __construct($action_handle) { parent::construct($action_handle); $this->action = $action_handle; } function secured_handler() { $password = $_POST['password']; $username = $_POST['username']; $login = $this->dbh->Login($username, $password); if ($login == true) { $this->session->startSession(); $this->session->set('username', $username); $this->view->displayHome(); $this->view->display(); } else { //This is going to get more advanced later on, I'm currently working on resolving my session issue before I continue on this. echo "you are not logged in"; } } } ?> index.php: <?php class Handler_home extends Action_Handler { public function __construct($action_handle) { parent::construct($action_handle); $this->action = $action_handle; } function secured_handler() { // for some reason this always returns false when the user goes back in history if ($this->session->check_session() == false) { $this->view->displayLogin(); $this->view->display(); } else { $this->view->displayHome(); $this->view->display(); } } } ?> anyone has an idea why the login page is always shown ? Quote Link to comment https://forums.phpfreaks.com/topic/234745-a-little-problem-with-sessions-and-browser-history/ Share on other sites More sharing options...
kney Posted April 26, 2011 Share Posted April 26, 2011 If you log in, and press back u get the login page? What if you refresh after pressing the back button? Quote Link to comment https://forums.phpfreaks.com/topic/234745-a-little-problem-with-sessions-and-browser-history/#findComment-1206332 Share on other sites More sharing options...
PHPSuperNewb Posted April 26, 2011 Author Share Posted April 26, 2011 yes you still get the login page even though you refresh after going back in history I find it really strange and am really cracking my head open on this problem whats really strange is that whenever I check session after the guy logged in, the session is set... But whenever I go back in history the session will not be set, even though I refreshed the page Quote Link to comment https://forums.phpfreaks.com/topic/234745-a-little-problem-with-sessions-and-browser-history/#findComment-1206334 Share on other sites More sharing options...
kney Posted April 26, 2011 Share Posted April 26, 2011 put session_start(); on top of both pages (index & home) and not when you actually log in. Quote Link to comment https://forums.phpfreaks.com/topic/234745-a-little-problem-with-sessions-and-browser-history/#findComment-1206335 Share on other sites More sharing options...
PHPSuperNewb Posted April 26, 2011 Author Share Posted April 26, 2011 are you saying that every time the page is changed the session will be stopped for some reason? I'll try to do what your asking but I don't know why I should start it every time a page has been changed. Could you elaborate? edit: it works 0.0! please tell me what was wrong ! I'd love to know. b.t.w. does this mean I have to start the session on every page that I go to from now on? I always thought you should only start a session once :S What happens when the session gets destroyed, does this mean more sessions are still active? Quote Link to comment https://forums.phpfreaks.com/topic/234745-a-little-problem-with-sessions-and-browser-history/#findComment-1206337 Share on other sites More sharing options...
kney Posted April 26, 2011 Share Posted April 26, 2011 are you saying that every time the page is changed the session will be stopped for some reason? I'll try to do what your asking but I don't know why I should start it every time a page has been changed. Could you elaborate? edit: it works 0.0! please tell me what was wrong ! I'd love to know. b.t.w. does this mean I have to start the session on every page that I go to from now on? If you don't put session_start(); on every page the session will not stay. Just include it in a header file and ur fine Quote Link to comment https://forums.phpfreaks.com/topic/234745-a-little-problem-with-sessions-and-browser-history/#findComment-1206340 Share on other sites More sharing options...
PHPSuperNewb Posted April 26, 2011 Author Share Posted April 26, 2011 thanks a lot man, this has been headaching me for ages now I really appreciate your help! b.t.w. does this mean that when I put my session_start(); in the construct of my session.php it also works? edit: seems to work ^.^! Quote Link to comment https://forums.phpfreaks.com/topic/234745-a-little-problem-with-sessions-and-browser-history/#findComment-1206341 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.