Jump to content

A little problem with sessions and browser history


PHPSuperNewb

Recommended Posts

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 :(?

Link to comment
Share on other sites

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  :confused:

 

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  :confused:

Link to comment
Share on other sites

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 :D! 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?

Link to comment
Share on other sites

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 :D! 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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.