Posted by tutor | Posted in PHP Tutorial | Posted on 15-12-2009-05-2008
0
Whenever a user logs in to a system some variables are set to identify this same user throughout his working duration and when the user logs out of the system the variables are reset. This duration from log=in to log-out is called a session and the variables used are session variables to identify a session.
Starting a PHP Session
Before storing the user information in a session, it should be first started. The code to start a session must be at the very beginning of your code, before any HTML or text is sent.
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
When you want to store user data in a session use the built-in PHP associative array $_SESSION. This is where you both store and retrieve session data. In previous versions of PHP there were other ways to perform this store operation, but it has been updated and this is the correct way to do it.
<?php
session_start();
// store session data
$_SESSION['username']=”Tutor”;
?>
<html>
<body>
<?php
//retrieve session data
echo “Username=”. $_SESSION['username'];
?>
</body>
</html>
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:
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo “Views=”. $_SESSION['views'];
?>
Posted by tutor | Posted in PHP Tutorial | Posted on 15-12-2009-05-2008
0
A PHP session allows you to store user information like username, shopping cart items, etc or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.
However, this session information is temporary and is deleted very quickly after the user has left the website that uses sessions.
It is important to think and verify if the sessions’ temporary storage is applicable to your website. If you require a more permanent storage you will have to use a database.
Sessions too have some major loop holes which need some advanced techniques to fill into those gaps. So if you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security.
PHP Session Variables
Session is the duration for which you browse a website or you visit some page. That means, when you are working with an application, you open it, do some changes and then you close it. This is a Session. The time you login to an application or start it the computer recognizes you.
It knows when you start the application and when you end. This is the case with applications running on your system, but on the internet there is one problem: HTTP address doesn’t maintain state, so a web server cannot know who you are and what you do.
A PHP session is used to solve this problem. It allows you to store user information on the server for later use. However, session information is temporary and is 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.