Introduction
As we know that http protocol is a stateless protocol so the web server does not recognize you . 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.
What is session control?
Gives ability to track a user through site, and easily move data related to that user among pages.No need to move data through hidden form fields.
Very useful for authentication, but can be used any time persistent data needed throughout a site visit.
Very useful for authentication, but can be used any time persistent data needed throughout a site visit.
How sessions work
Sessions are identified by a random number (Session ID) generated by PHP and stored on the client computer in 1 of 2 ways:
- Using a cookie, if the user's browser supports.
- Appending the session number to URLs as user traverses site (i.e www.whatever.com?PHPSESSID=495294532459x)
A session will eventually expire‐‐usually after aspecified period of inactivity.
Progression of events
PHP script starts a session. Done before any other page activity.
session_start();// Session ID created and stored on user's computer. (if possible)
Session variables are created, and values stored on the server.
PHP script can use these variables from page to page throughout a site.
Starting a session In PHP
In any script using sessions, you must first call
session_start();
- If session has not been established, this will do that.
- If a session has been established, this will load session data.
Add or access session variables by using the $_SESSION superglobal array.
Session Handling
Createa page ses1.php copy the following code in that page. and run it. now session will be set.
<?php session_start(); $_SESSION['name'] = "your name"; $_SESSION['office'] = "company name"; $_SESSION['phone'] = 96951; ?>
Let's create another page ses_result.php copy following codes and run.
<?php session start();
session_echo "Here's the session info:<br />"; foreach ($_SESSION as $var=>$contents) echo "$var: $contents<br />"; ?>
Manipulating the Session data
session_unset() erases all session variables and data.
unset() can be used to erase a single variable and data.
unset($_SESSION['myvar']);
session destroy() destroys the session data (without destroying the session variables).Can be useful for "logging out" user.
Session variable arrays
Session variables can be arrays
<?php session_start(); $_SESSION['list'][]="Hello"; $_SESSION['list'][]="Wow"; echo count($_SESSION['list'])."<br />"; foreach ($_SESSION['list'] as $item) echo "$item<br />"; ?>
Can be useful technique for shopping carts or other data that is accumulated over multiple page visits.
When and why to use Sessions
- Performance: When performing a slow operation, storing the results for use on several pages is better than repeating the calculation on each.
Example: storing results of SQL query - Sequence: When a user process takes place over a sequence of screens, storing information saves time and user input.
No comments:
Post a Comment