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

How to use store and use session variables across pages?


Sessions Step By Step

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

    <?phpsession_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.


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


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.