In PHP special variables called SESSIONS can be used to store data into for a longer period ( a session ). Session variables can as well be used on different pages of your website ( any page ). They can for example be used to save a login session which has to be remembered for a longer period ( until the user logs out ) and on all pages of the website.
A session variable can be created like this:
$_SESSION['variable_name'] = "value";
So it’s just like the POST variables, an array which contains ( in this case ) all SESSION variables. To be able to use session variables on a page, you need to first use the session_start function on the top of the page ( before any (HTML/graphical) output ).
<?php session_start(); ... rest of your page (SESSION variables enabled) ... ?>
An example of a session variable which we can use on any page again:
File: create_session.php
<?php session_start(); $_SESSION['username'] = "Webcodez"; ?>
File: example.php
<?php session_start(); echo "Hey ".$_SESSION['username']; ?>
We could also make a page which unsets the session variable ‘username’ (logs the user out for example). We use the function unset to do this.
File: example.php
<?php session_start(); unset($_SESSION['username']); ?>
You could combine this with POST variables, to make the user choose an username to be logged in with for example:
File: create_session.php
<?php
session_start();
if($_POST) {
$_SESSION['username'] = $_POST['username'];
header("location: example.php");
}else{
?>
<?php
}
?>
File: example.php
<?php
session_start();
If($_SESSION['username']) {
echo "Hi there ".$_SESSION['username'];
}else{
header("location: create_session.php");
}
?>
The function session_start is again used in all pages to be able to create and use sessions. For the rest we only create the session $_SESSION['username'] and use a form and POST variables to save the user input into the session variable $_SESSION['username']. The header(location: location_here) is used to make it go to another page automaticly. If you aren’t familiar with the POST variables and form, you should have a look at Chapter 9 – POST variables.
NOTE: You can also use the function session_destroy to simply unset (destroy) ALL session variables. – session_unset() seems to work as well for unsetting the whole $_SESSION array.
Instead of using Sessions, you could also use Cookies to store data into for a longer period ( couple of days for example, anything ).
set_cookie("my_cookie", "my_cookie_value", ... timestamp untill which the cookie stays remaining ... , ... path on which the cookie should work ... );
You see we use the function set_cookie to create a cookie variable. In this example we called it “my_cookie” so it’d be $_COOKIE['my_cookie'] and we gave it the value “my_cookie_value”. So it created $_COOKIE['my_cookie'] = “my_cookie_value”, which however can not be set in this way ( as we done it for SESSION variables ) – the set_cookie function is used as we need to set more parameters. The time we want the cookie to last ( better said: the expiration time ) for example. This needs to be written in timestamp (the UNIX timestmap) which is in seconds. The function time() gives the current timestamp in seconds, so time() + 3600, would make a cookie stay for 3600 seconds more and then it exires ( which is 1 hour more ). The path can be anything, say you want to have the cookie work on all paths of your website, then you use “/” as path.We create one cookie that works on all paths of the website for 1 hour:
set_cookie("my_cookie", "my_cookie_value", time() +3600 , "/" );
To delete a cookie, you simply make it expire:
set_cookie("my_cookie", "", time() -1, "/"); //-1 could be anyhing, "" could be anything aswell as it will expire anyway so no matter what value you give it
Now we set the expiration time to 1 second before the current time. You could set it any time before the current time, just so it would expire right now.
To check whether a sesson or cookie exists, you can use the isset function. Like:
if( isset($_SESSION['my_session']) OR isset($_COOKIE['my_cookie']))
Cheers,
Admin.


Mon, Jan 18, 2010
PHP & MySql