25

When one page is accessed, I would like to start a session and store a session variable:

<?php
  session_start(); 
  $_SESSION['myvar']='myvalue';
?>

Then from another page, I would like to check if that session variable has been stored:

<?php
    session_start();
    echo("1");
    if(isset($_SESSION['myvar']))
    {
        echo("2");
       if($_SESSION['myvar'] == 'myvalue')
       {
           echo("3");
           exit;
       }
    }
    ?>

This code does not work for me.

ab11
  • 19,770
  • 42
  • 120
  • 207
  • did u try `var_dump($_SESSION)` on the 2nd page? – Naftali Mar 30 '11 at 16:47
  • 1
    How is the session ID transferred? Did you check whether the session ID is the same on both pages? – Gumbo Mar 30 '11 at 16:48
  • Code looks correct. You didn't output anything (even whitespace before the opening ` – Michael Berkowski Mar 30 '11 at 16:49
  • are cookies activated in your browser? – ITroubs Mar 30 '11 at 16:49
  • @Neal, output of var_dump($_SESSION) was: array(0) { } – ab11 Mar 30 '11 at 16:49
  • so the session was never saved. u have to check on that – Naftali Mar 30 '11 at 16:50
  • @Itroubs cookies are activated – ab11 Mar 30 '11 at 16:50
  • @Gumbo. I don't know how they are transferred nor how to check if they are the same, could you post a little sample code? – ab11 Mar 30 '11 at 16:51
  • Insanity check: are the two pages on the same domain/path? – zzzzBov Mar 30 '11 at 16:53
  • @zzzzBov. They are in the same domain, but under different directories. This shouldn't matter? – ab11 Mar 30 '11 at 16:55
  • @ab11, make sure the session cookie path is set to their common directory. The second page is probably not able to access the session cookie – zzzzBov Mar 30 '11 at 16:56
  • 1
    @ab11: In that case the current [session configuration](http://php.net/session.configuration) for *session.use\_cookies*, *session.use\_cookies\_only*, and *session.use\_trans\_sid* would be helpful (see [`ini_get`](http://php.net/ini_get)). And the current session ID can be retrieved with [`session_id`](http://php.net/session_id) – Gumbo Mar 30 '11 at 16:59
  • @zzzzBov. Could you direct me to some code showing how to do that, please? – ab11 Mar 30 '11 at 16:59
  • @Gumbo. Those values are: on, off, 1. Respectively. – ab11 Mar 30 '11 at 17:03
  • 1
    @ab11: And what about the session ID? Are they identical on both pages? – Gumbo Mar 30 '11 at 17:04
  • @Gumbo. Yes they are. I added: echo(session_id()); to each page (after session_start();). They both give: e92d1212a93e216e96523ff1e903ed41. – ab11 Mar 30 '11 at 17:09
  • 1
    @ab11: Then my last guess is that either *session.save\_handler* or *session.save\_path* is not properly set. – Gumbo Mar 30 '11 at 17:11
  • @Gumbo. Values for those are: files and /var/php_sessions. Respectively. – ab11 Mar 30 '11 at 17:14
  • 1
    @ab11: Last question: Does `/var/php_sessions` exist and is it readable/writable by PHP? You can check that easily by browsing to that directory and looking for a file named *sess\_e92d1212a93e216e96523ff1e903ed41*. – Gumbo Mar 30 '11 at 17:16
  • @Gumbo. I'm not sure how to browse to that directory (/var does not show in my filezilla). Probably promising, this gives false: if(file_exists('/var/php_sessions')) { echo('true'); } else { echo('false'); } – ab11 Mar 30 '11 at 17:20

8 Answers8

26

Sessions Step By Step

  1. Defining session before everything, No output should be before that, NO OUTPUT

    <?php
    session_start();
    ?>
    
  2. Set your session inside a page and then you have access in that page. For example this is page 1.php

    <?php
       //This is page 1 and then we will use session that defined from this page:
        session_start();
        $_SESSION['email']='email@example.com';
    ?>
    
  3. Using and Getting session in 2.php

     <?php
    
    //In this page I am going to use session:
    
      session_start();
      if($_SESSION['email']){
      echo 'Your Email Is Here!  :) ';
      }
     ?>
    

NOTE: Comments don't have output.

BrianS
  • 13,284
  • 15
  • 62
  • 125
Mohammad Kermani
  • 5,188
  • 7
  • 37
  • 61
16

All you want to do is write --- session_start(); ----- on both pages..

<!-- first page -->
<?php
  session_start(); 
  $_SESSION['myvar'] = 'hello';
?>

<!-- second page -->
<?php
    session_start();
    echo $_SESSION['myvar']; // it will print hello 

?>
j0k
  • 22,600
  • 28
  • 79
  • 90
gaurav
  • 171
  • 1
  • 2
9

Reasoning from the comments to this question, it appears a lack of an adjusted session.save_path causes this misbehavior of PHP’s session handler. Just specify a directory (outside your document root directory) that exists and is both readable and writeable by PHP to fix this.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 1
    sorry to ask for yet more clarification, but what do you mean by "outside your document root directory"? – ab11 Mar 30 '11 at 17:38
  • 1
    @ab11: Since sessions commonly contain sensitive data, you wouldn’t want the session files to be accessible via the web. And that would be the case when you store them in a directory inside the [document root directory](http://httpd.apache.org/docs/2.2/mod/core.html#documentroot). – Gumbo Mar 30 '11 at 17:42
  • So, using remote view in dreaweaver, i navigated above "public_html" to the root of my website, and added directories "var/php_sessions". And then I added: session_save_path ('/var/php_sessions');, add the beginning of both pages. This had no effect. – ab11 Mar 30 '11 at 17:59
  • 2
    @ab11: The path in *session.save\_path* is an absolute file system path not is not interpreted relatively to the document root directory (where you *absolutely* shouldn’t store the session files!). – Gumbo Mar 30 '11 at 18:01
  • @Gumbo. Please forgive my ignorance, but that means the steps I took above should work? Creating the directories at the website root means they are off the document root directory, and then the path I used above should work? – ab11 Mar 30 '11 at 18:04
  • 1
    @ab11: No. Since “…/public_html” is your document root directory, you should not store the session files somewhere inside that directory (e.g. not “…/public\_html/var/php_sessions”). Besides that, the path in *session.save\_path* is interpreted as an absolute file system path and not relative to the document root directory. So `/var/php_sessions` would be interpreted as “/var/php_sessions” and not as “…/public_html/var/php_sessions”. – Gumbo Mar 30 '11 at 18:09
  • @Gumbo. I should clarify. I navigated above "public_html" when creating my var/php_sessions directory. It is not inside of the "public_html" directory. – ab11 Mar 30 '11 at 18:11
  • @ab11: Then you might just have specified the path to that directory falsely or the directory is not readable/writeable by PHP. – Gumbo Mar 30 '11 at 18:16
  • @Gumbo. Thanks for all your help, but this still isn't working. Can you recommend a way to test if the directory is readable/writeable? – ab11 Mar 30 '11 at 18:28
  • Well, I contacted my web hosting service and the rectified the problem. Thank you for your help. – ab11 Mar 30 '11 at 19:13
1

In the possibility that the second page doesn't have shared access to the session cookie, you'll need to set the session cookie path using session_set_cookie_params:

<?php
session_set_cookie_params( $lifetime, '/shared/path/to/files/' );
session_start();
$_SESSION['myvar']='myvalue';

And

<?php
session_set_cookie_params( $lifetime, '/shared/path/to/files/' );
session_start();
echo("1");
if(isset($_SESSION['myvar']))
{
    echo("2");
   if($_SESSION['myvar'] == 'myvalue')
   {
       echo("3");
       exit;
   }
}
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • i added this line to no effect. I assume its possible that I should be using a different path? – ab11 Mar 30 '11 at 17:15
0

Try this

First Page

<?php
   session_start();
   $_SESSION['myvar']='myvalue';
?>

Second page

 <?php
   session_start();
   echo $_SESSION['myvar'];
 ?>
vivek
  • 354
  • 2
  • 9
-1

Every time you start a session (applies to PHP version 5.2.54), session_start() creates a new session id.

Here is the fix that worked for me.

File1.php

session_id('mySessionID'); //SET id first before calling  session start
session_start();

$name = "Nitin Hurkadli";
$_SESSION['username'] = $name;

File2.php

session_id('mySessionID'); 
session_start();

$name = $_SESSION['username'];
echo "Hello  " . $name;
-1

Starting a Session:

Put below code at the top of file.

<?php session_start();?>

Storing a session variable:

<?php $_SESSION['id']=10; ?>

To Check if data stored in session variable:

<?php if(isset($_SESSION['id']) && !empty(isset($_SESSION['id'])))
echo “Session id “.$_SESSION['id'].” exist”;
else
echo “Session not set “;?>

?> detail here http://skillrow.com/sessions-in-php-4/

-5

Try this:

<!-- first page -->
<?php
  session_start(); 
  session_register('myvar');
  $_SESSION['myvar'] == 'myvalue';
?>

<!-- second page -->
<?php
    session_start();
    echo("1");
    if(session_is_registered('myvar'))
    {
        echo("2");
       if($_SESSION['myvar'] == 'myvalue')
       {
           echo("3");
           exit;
       }
    }
    ?>
iamandrus
  • 1,400
  • 2
  • 16
  • 25
  • 2
    and it says on the man page for `session_register`: `This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.` – Naftali Mar 30 '11 at 16:52
  • Added session_register() and session_is_registered(). I had this problem with an old project of mine and this seemed to fix it. – iamandrus Mar 30 '11 at 16:52
  • I don't think he's worried about code validation right now, to be honest. – iamandrus Mar 30 '11 at 16:53
  • 3
    this solution is complete nonsense. session_register('myvar') would register $myvar and would not do anything to the $_SESSION variable. also session_register() is deprecated and in the manual the also say this: If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister(). – ITroubs Mar 30 '11 at 16:55
  • Ah, I'm sorry. But I also added a == when assigning the session variable. – iamandrus Mar 30 '11 at 18:56