| NAME: |
|
PHP Sessions |
| AUTHOR: |
|
punkstar |
| DATE: |
|
20th Nov, 2005 - 09:20:31 AM |
| DESCRIPTION: |
|
Sessions can store user information, heres how to use them |
Need some help with that coding?
Have you ever seen member systems, or did you read the little introduction to MySQL with PHP tutorial on this site? If you have, then you must be wondering how all the user data is carried from page to page. The answer is Sessions.. (or cookies, but saying that would have ruined the atmosphere :P)
| PHP Code |
<?php
session_start();
$_SESSION['variable'] = "Content";
?>
<a href="nextpage.php">Session Variable Set, click here to continue!</a>
|
The above code starts a session, then assigns a session variale a value. If that was a normal variable, we would not be able to get its contents on the other page right? Correct. This is a session variable. Aslong as you call session_start() at the top of every page on your website, the session will continue, and you will have access to the session variables, ie $_SESSION.
| PHP Code |
<?php
session_start();
?>
Welcome to nextpage.php, the variable $_SESSION['variable'] contains the value "<?=$_SESSION['variable'];?>".
|
The above code would echo..
"Welcome to nextpage.php, the variable $_SESSION['variable'] contains the value "Content".
Easy peasy. Experimenting is the best way to learn, so give sessions a bash.. they will come in handy for any php application that atleast requires a login ;)
|