The protocol used to run web pages is the HTTP protocol. This is a stateless protocol. That is, the web server does not track the behavior of individual users. Each ask for comes to the web server are treated as sole. Hence it is not possible to decide which user made a request at what time. To overcome this “stateless” nature of the web server, we use sessions and cookies to uphold the state of the site.
Sessions
Sessions are used to path the activities of a particular user. If a user visits our site, then the server generates an only one of its kind id known as session id. Using this number we can also recreate the before created session environment. Using session variables we can store either textual or numeric in sequence and these information can be easily accessed from side to side the super global array $_SESSION. Some of the common php session handling functions is listed below.
session_start()
This function is used to start a session in php. If we use cookies to store session data, then we must call the session_start() function before any output is produce. In such cases the session_start() function must be the first line in our script. This line must be there for all pages in which we use $_SESSION variables to re make the preceding session surroundings
<? Php
//Used to start the session
session_start();
//registering a session variable
$_SESSION ['state'] =”Delhi”;
?>
Save this page as first.php and create one more page named second.php and save the following data
<? Php
//Used to restart the session
session_start();
// now we using the session stored in the previous page
echo “You are from “.$_SESSION['state'];
?>
session_destroy()
This function is used to destroy the current session. This function is mainly seen in logout module of a project. Before using the session_destroy() function we must use session_start() to recreate the environment.
<?php
//Used to restart the session
session_start();
//destroy the session
session_destroy();
//testing the session
echo “You are from “.$_SESSION['state'];
?>
Cookies
Cookies are used as an alternate of session. In cookies the information about the client state is kept at a file inside the client system. Cookies can be read only by the site which creates them. Maximum size of a cookie is limited to 4-6KB. If the user turn off cookie support in their browser, then the cookie does not saved. So using cookies for important user tracking is risky.
setcookie()
The setcookie() function is used for saving the cookies in client system. The first parameter of this function indicates the Name of the cookie and second parameter indicates the Value stored in the cookie. The date and time at which the cookie expired is set in Expires limit. Path specifies the index on the domain from which cookie data can be accessed>Domain specifies the domain for the cookie. The secure attributes is a Boolean flag indication that the cookie should be transmitted over a secure link https
<? Php
// set a cookie named user with value Mike and expires after 1 day
$flag = setcookie(‘users’,'Mike’,time()+(3600 * 24),’/');
# time() will returns the current unix time stamp
if($flag)
echo “Cookie is created”;
else
echo “Cookie is not created”;
?>
Accessing value from a cookie
To access the value from a cookie we use the super global variable $_COOKIE.
<?php
if(isset($_COOKIE['users']))
echo “Cookie value is “.$_COOKIE['users'];
?>
setrawcookie()
This function is also used for creating a cookie. This method is similar to set cookie method except that the values saved in the cookie is not url encoded automatically when sending to the browser
Deleting a cookie
For deleting a cookie we used the same function setcookie() with same name. But the expires parameter should be something from the past.
<?php
// now the cookie is deleted…Note the expire field
setcookie(‘user’,'Mike’,time()-(3600 * 24),’/');
?>
You must be logged in to post a comment.