Using Sessions in PHP - Basic Sessions
(Page 2 of 5 )
<?php // page1.php session_start(); $_SESSION["real_name"] = "Hermawan Haryanto"; print "<a href='page2.php'>Go to this page</a>"; ?> |
<?php // page2.php session_start(); print $_SESSION["real_name"]; ?> |
The above example shows you that I create a session variable named "real_name" storing my name as the value. Then on the second page, I print out the session variable and my name will show up.
Let’s do another example:
<?php session_start(); $count = $_SESSION["counter"] + 1; $_SESSION["counter"] = $count; print $_SESSION["counter"]; ?> |
When you first access that page, it will display 1. Try to refresh the page and the number will grow.
To destroy or delete an existing session variable, you can use the unset command.
unset($_SESSION["session_name"]); |
Or if you want to delete all session variables (and the session itself), you can do it by using the destroy command.
The destroy command is usually used to log-off a user from the membership area. Let’s make a membership area for our example.
Next: Running Membership with Sessions >>
More Miscellaneous Articles
More By Hermawan Haryanto