How to use store and use session variables across pages?
Solution 1:
Sessions Step By Step
-
Defining session before everything, No output should be before that, NO OUTPUT
<?php session_start(); ?>
-
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]'; ?>
-
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.
Solution 2:
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
?>