Unit-6 Handling Sessions and Cookies
Concept of Session Session values are store in server side not in user’s machine. A session is available as long as the browser is opened. User couldn’t be disabled the session. We could store not only strings but also objects in session.
PHP Session Variables Session variables hold information about one single user, and are available to all pages in one application. When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database. Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.
Starting a PHP Session Before you can store user information in your PHP session, you must first start up the session. The session_start() function must appear BEFORE the tag: <?php session_start(); echo "session start"; ?>
The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.
Storing a Session Variable The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:
Output: Pageviews=1
In the example below, we create a simple page-views counter. The isset() function checks if the "views" variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't exist, we create a "views" variable, and set it to 1:
Destroying a Session If you wish to delete some session data, you can use the unset() or the session_destroy() function. The unset() function is used to free the specified session variable:
You can also completely destroy the session by calling the session_destroy() function: Note: session_destroy() will reset your session and you will lose all your stored session data
Concept of Cookies The main use for cookies is to solve the problem of lack of status when browsing web pages. With cookies, small portions of information are embedded in the browser, allowing the identification of cookies in several pages from the same site or even during visits between several days.
Operation The cookie is sent to the web browser from the server, and if the web browser accepts it, it remains there
Pages request a cookie from the server... The web browser sends the cookies allowing the server to identify the user.
How to use cookies The management of cookies in PHP is done by using the statement setcookie, this statement is available since version 3 of PHP. Syntax: int setcookie (string Name [, string Value [, int Expire [, string Path [, string Domain]]]])
Setcookie() defines a cookie that is sent along with the rest of the information from the header. Cookies shall be sent before any html tag; therefore, we shall call one of these statements before any tag or. This is a restriction of cookies, not of PHP. All messages, except name, are optional.
Name. Name of the cookie. If we create a cookie only with its name, the cookie existing in the client under said name will be deleted. We can also replace any argument with an empty string(""). Value. Value to be stored by the cookie in the client. Expire. The argument expire is an integer argument that indicates the time a cookie will be deleted in the time format returned by the UNIX statements time() and mktime(). Time() + N seconds of duration is generally used to specify the duration of the cookie.
Path. Subdirectory where the cookie has a value. Domain. domain where cookie will be available. Instead of path you can use domain settings. For example, if the domain is set to ".yourdomain.com", the cookie will be available within the domain and all its sub- domains, example news.yourdomain.com.
Example setcookie("user", “T.Y.B.C.A", time()+3600,“tybca.com"); In this example, we set a user name cookie that has the value Luis, lasts 1 hour (3600 seconds) valid for the whole domain webestilo.com