http://php.net/manual/en/ref.session.php
sessions have numerous advantages, but you will still need to set cookies for somethings.
for example, sessions default to a lifetime of 180min(you can change it to any value), so after that time is up, the info in the $_SESSION array wont be avail anymore. but if you want to have a "remember me" button for login or something, you prob want it to last for weeeks. in that case, set a cookie.
you need to call session_start(); on any page you want to add or retrieve info from thier session
session_start(); will send a cookie to the user if the browser did not send one to the server containing a PHPSESSID along w/ the request. so you dont need to worry about checking or anything, php handles it automatically for you.
also, w/ sessions, you dont store any info in the cookie, and as such, makes the cookie small, and only need one. plus since their is no info in the cookie, they cant modify it. if they modify the session_id, then they lose the entire session(which is good)
think of it as sending a cookie w/ a unique identifier, and then it allows you to store info in a database, but only for that user. php automatically loads all the info from the database for the identifier it receives, and stores in in the $_SESSION array., so its easy for you to use
this is how easy sessions make it
Code: Select all
session_start(); // this sends a cookie w/ a session id number
if (isSet($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
}
$_SESSION['visited_page1'] = true; // this is how you store info into the session, very simple
// now you have thier name available on any page
echo $_SESSION['name'];
page 2
Code: Select all
session_start();
echo 'your name is ' . $_SESSION['name'];
if (isSet($_SESSION['visited_page1'])) {
echo 'you have visted page 1!';
}