Cookies and Sessions in PHP
Cookie:
A Cookie is basically a message given by the web server to the web browser.This message is stored in a text file and the message is sent back to the server every time the browser requests a page from the web server.The main purpose is to identify the users.Here we will see how to set and retrieve values from a cookie in PHP.
In PHP, a cookie is set using the setcookie() function.
Syntax: setcookie(cookiename,value,expiration);
Lets see a simple example to set a cookie.
<?php
setcookie("username","vivek",time()+2592000);
?>
<html>
....
</html>
In the above example a cookie named “username” is created with value “vivek”.The Cookie expires in 30 days.
The setcookie() function url encodes the value so that it can be safely transmitted over the internet.url encoding converts the character to a format consisting of a % symbol followed by a 2 digit hexadecimal number.
Note that the cookie is set before the html tag.
You can also use the setrawcookie() function if the information to be transmitted is not so important.setrawcookie() does not url encode the cookie value.
Having set the cookie,lets see how we can retrieve the cookie.When the cookie was set using setcookie(),the value was url encoded and send.When retrieved,this value is automatically decoded.
To retrive the cookie value,the $COOKIE variable is used.
Lets retrieve the cookie we just created above.
<html>
<body>
<?php
if(isset($_COOKIE["username"]))
echo "Welcome back to three2tango.com". $_COOKIE("username");
else
echo "Welcome to three2tango.com";
?>
</body>
</html>
The isset() function checks whether the cookie exists.
To destroy a cookie,simply use setcookie() again with the expiration time set to the past.
<?php
setcookie("username","",time()-3600);
?>
The setcookie() function can be used only for browsers that supports cookies.
Sessions:
The difference between a session and a cookie is that a cookie is set in the user’s browser while the session is stored on the web server for later use.A common use is when you start an application and close it.The web server cannot retrieve your information because the HTTP address does not maintain state.The scope of the session is between
the time you enter the website and the time you leave the website.In effect, a session stores information of a session the website.
The session_start() function is used to start up the session. Like the setcokie(),the session_start() function should be placed before the html tag.
So,lets see how to start a session
<?php
session_start();
$_SESSION['username']=$_POST[username];
?>
<html>
...
</html>
This page accepts data from another page and sets the username as the session variable which will now be available throughout the application.
A session can be destroyed using the unset() or the session_destroy function
<?php
unset($_SESSION['username']);
?>
The difference between unset() and session_destroy is that unset() merely free up the session variable while session_destroy will completely reset your session and the session data becomes unavailable.
<?php
session_destroy();
?>
To get the session id you can use the session_id() function.It returns the session id for the curent session or an empty string if it is a new session.
Related posts:








Leave your response!