[php] How to use store and use session variables across pages?

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.

This question is related to php session session-variables

The answer is


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;
       }
    }
    ?>

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/


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 protected]';
    ?>
    
  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.


Try this

First Page

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

Second page

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

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;
   }
}

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;

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 

?>

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to session

What is the best way to manage a user's session in React? Spring Boot Java Config Set Session Timeout PHP Unset Session Variable How to kill all active and inactive oracle sessions for user Difference between request.getSession() and request.getSession(true) PHP - Session destroy after closing browser Get Current Session Value in JavaScript? Invalidating JSON Web Tokens How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session How can I get session id in php and show it?

Examples related to session-variables

Local storage in Angular 2 Set session variable in laravel Session variables not working php Setting session variable using javascript How to use sessions in an ASP.NET MVC 4 application? Check if PHP session has already started How to use store and use session variables across pages? MySQL wait_timeout Variable - GLOBAL vs SESSION How do servlets work? Instantiation, sessions, shared variables and multithreading How to empty/destroy a session in rails?