Because cookies are about 30% unreliable, and that % is rising.
Plus more and more browsers are coming with security and privacy settings that do not allow storing of cookies on computers.
PHP has a great function(s) that have the same results as cookies and more,
plus they are invisible and store information on web server.
Session have great capabillity.
For example if you want to have a member system on your website,
with session you could identify a user, user's level and other.
Session start function: session_start()
First thing on using sessions: you must avoid errors!
You can do that by calling session before anything is output to the browser.
Error example:
CODE
<?php
echo "Error example</br>";
session_start();
?>
echo "Error example</br>";
session_start();
?>
As a result you will get:
Error example
QUOTE
Warning: Cannot send session cookie - headers already sent by (output started at c:\apache\htdocs\session_error.php:2) in c:\apache\htdocs\session_error.php on line 3
Warning: Cannot send session cache limiter - headers already sent (output started at c:\apache\htdocs\session_error.php:2) in c:\apache\htdocs\session_error.php on line 3
Warning: Cannot send session cache limiter - headers already sent (output started at c:\apache\htdocs\session_error.php:2) in c:\apache\htdocs\session_error.php on line 3
No errors example:
CODE
<?php
session_start();
echo "No errors here :)";
?>
session_start();
echo "No errors here :)";
?>
Next step is to register a session variable.
When we started a session, we would use a php to start storing information about user.
I used a form to post a variable 'name'.
CODE
<?php
session_start();
// get the variable name from the form
$name = $_POST['name'];
// register session key with the value
$_SESSION['name'] = $name;
?>
session_start();
// get the variable name from the form
$name = $_POST['name'];
// register session key with the value
$_SESSION['name'] = $name;
?>
In code above i assigned $name to session key 'name' ( $_SESSION['name'] = $name; )
Another way to assign that value is: $_SESSION['name'] = $_POST['name'];
Displaying the results.
CODE
<?php
session_start();
?>
Welcome to members area <? echo $_SESSION['name']; ?>
session_start();
?>
Welcome to members area <? echo $_SESSION['name']; ?>
This is pretty simple, we started session, and echoed a result.
Unregistering variables.
If we want to remove session variable 'name', we would use this code.
CODE
<?php
session_start();
$_SESSION['name'] = FALSE;
?>
session_start();
$_SESSION['name'] = FALSE;
?>
We cleared session variable 'name' ( $_SESSION['name'] = FALSE; )
, and it does not exist any more, unless we register it again.
You can check if it's exist by using echo (explained earlier).
Destroying a Session.
Session destroy function: session_destroy();
CODE
<?php
session_start();
$_SESSION = array();
session_destroy();
?>
session_start();
$_SESSION = array();
session_destroy();
?>
Session destroy function just deletes the session files and clears any trace
of that session. You can use this f. if you have a log in script you will need
also log out script.
Thats all.
I hope this tutorial would be useful for you.
REMEMBER: In every script where you wanna use a session variables, you must
use a session start function !!!
I am sorry if you find any errors because i wrote this from my head, and if my english is bad (not my mother tonque).


