A session variable is a great way to store information about visitors on your site. For example if a user logs in you could store their username, and the date of login as session variables, then add a file include that checks to make sure the user is logged in before viewing a page.
Below is example code using session variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php echo "Creating session variables... <br />"; $_SESSION['uname'] = "Steve"; $_SESSION['pass'] = "MyPassword"; echo "Displaying session data... <br />"; echo $_SESSION['uname'] . "<br />" . $_SESSION['pass'] . "<br />"; echo "Destroying session data... <br />"; unset($_SESSION['uname']); unset($_SESSION['pass']); echo "Displaying session data again (should be empty)... <br />"; echo $_SESSION['uname'] . "<br />" . $_SESSION['pass'] . "<br />"; echo "Done! <br />"; ?> |
That code, when run will output:
Creating session variables...
Displaying session data...
Steve
MyPassword
Destroying session data...
Displaying session data again (should be empty)...
Done!
The code is very simple to follow, $_SESSION[] variables are created named uname and pass, values are assigned to them and displayed via echo. Then unset() is used to delete the values, thus when echoing the output again the lines are blank. Cake.