Use something like this:
page1.php,
<?php session_start(); $_SESSION['myValue']=3;
Any other PHP page:
<?php session_start(); echo $_SESSION['myValue']; ?>
A few notes to keep in mind: you need to call session_start() BEFORE any output, HTML, echos - even white space characters.
You can continue to change the value in the session - but it can only be used after the first page - this means that if you install it on page 1, you cannot use it until you get to another page or refresh the page.
Setting the variable itself can be done in one of several ways:
$_SESSION['myValue']=1; $_SESSION['myValue']=$var; $_SESSION['myValue']=$_GET['YourFormElement'];
And if you want to check if a variable is set before getting a potential error, use something like this:
if(!empty($_SESSION['myValue']) { echo $_SESSION['myValue']; } else { echo "Session not set yet."; }
source share