We help IT Professionals succeed at work.

php session

beridius
beridius asked
on
For some reason this code is not restarting the session as the detail for previous page
<?php

if (isset($_SESSION['previous'])) {
   if (basename($_SERVER['PHP_SELF']) != $_SESSION['previous']) {
        session_destroy();
   }
}

session_start(); 
?> 

Open in new window

Comment
Watch Question

may be you can put this, session_start();  on top
<?php

session_start(); 

if (isset($_SESSION['previous'])) {
   if (basename($_SERVER['PHP_SELF']) != $_SESSION['previous']) {
        session_destroy();
   }
}


?>

Open in new window

Commented:
session_start()  should be before your conditional. Your isset() conditional will always return false because your session variable wont be initialized before testing it is set.

Author

Commented:
yes I have tired that with no luck
you can also destroy the session and redirect to another page where you can start the session again..

Commented:
Try
var_dump($_SESSION);

Open in new window


To see what is actually stored in the $_SESSION array
otherwise, try

unset($_SESSION['previous']);
Most Valuable Expert 2011
Top Expert 2016

Commented:
Here is the basic rules of how to use sessions.

1. Use session_start() unconditionally at the top of all of your scripts without exception.  No excuses.
2. Put information into the $_SESSION variable.  You can put all sorts of information in there, but certain kinds do not make sense, such as resource data types.
3. Expect to find the information in the $_SESSION variable across requests.

That's it.  That is everything.  Don't do anything else.  It is just that easy.  

Now let's get to some ways to really muck things up.  

First you need to know that the $_SESSION array is not immutable.  In my opinion, using $_SESSION before calling session_start() should trigger a fatal error, but PHP does not work that way.  So the original code snippet was silent about the fact that you have the instructions out of order.  

Next, you need to know that the contents of $_SESSION are created by PHP from stored data.  So if you put something into the $_SESSION array and then call session_start() what would you expect to find in the $_SESSION array?  I don't know either.  

Third, the session handler relies on cookies.  Cookies are part of the HTTP headers.  You cannot write headers if you have created any browser output (even invisible whitespace).  So if your script looks like this, your session will not work, because the line-feed between these two lines is browser output.

<?php /* SOMETHING */ ?>
<?php session_start();

Fortunately there are not many session-related functions and you can read the man pages for all of them in less than 15 minutes.  They are all linked from the left side of this page.
http://php.net/manual/en/function.session-start.php

One last note.  If you rely on an unset part of the session array, you will never know, because PHP will be silent about the use of undefined variables.  For that reason you want to add error_reporting(E_ALL) to the top of your script.  Put it right after <?php and right before session_start();

Best regards, ~Ray
Most Valuable Expert 2011
Top Expert 2016

Commented:
To quote ID:37249052: yes I have tired that with no luck

Eh?