Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
PHP Basics
Introduction
Variables & Arrays
Sub & Functions
If Statements
Case Statements
Loop Statements
Date Objects
Form Processing
Text File Objects
Database Access
Adding Record
Updating Record
Deleting Record
Virtual Includes
Cookies
Session
Server Variables
Creating Login Page
E-mail
PHP Books @
CA Amazon
UK Amazon
US Amazon

  :: Home
PC Topics
  :: Build A PC
  :: Windows XP
  :: PC Help
Tutorials
  :: HTML
  :: JavaScript
  :: ASP
  :: PHP
  :: VBScript
  :: SQL
Miscellaneous
  :: Code/Scripts
  :: Forum
  :: Links
  :: Contact us
  :: Tell A Friend
  :: In Somali
  ::
Web Articles
  Top Web Hosting
Reviews
  Processing
Online Card Payment Guide
  Domain name
registration & buying guide
  Getting
a website online guide
  Search Engine
submission & optimization tips
Netfirms Web Hosting
Free trial
Quality business correspondence.
Yahoo! Search Marketing


PHP Session Tutorial

A session is object used to store information as long as your browser is open or until specify expiry time ends.  It's best used to store user login information to identify the user by login information and let them access restricted area of the web site.   It's also used to pass variables from one page to another or store user access information for visit statistics use. To create session with php, you will have to create the session it self by typing session_start(); at beginning of your php code. It's important that the session creating statement must be the first line of you code other wise, you may get some ugly errors. This code registers user's session with server.

Now, we are going to create a user login session and store the values username and password:
<?php
session_start();
$_SESSION['userName'] ="MyUserName";
$_SESSION['password'] ="MyPassword";
?>

We created a session that retains username and password.
Just to show you what is stored, the following example check if a session exist and if so, prints the session values to the browser.
<?php
session_start();
if(isset($_SESSION['userName']))
{
  print "Your session username: ".$_SESSION['userName']. "<br>";
  print "Your session password: ".$_SESSION['password']."<br>";
}
else
{
  print "Session does not exist";
}
?>

Execution Result:
Your session username: MyUserName
Your session password: MyPassword

To see more of utilization of session(), the following example checks if session exists and redirects the browser to different pages.
<?php
session_start();
if(isset($_SESSION['userName']))
{
  header("Location: member.php");
}
else
{
  header("Location: nonemember.php");
}
?>

Destroy session

session_destroy(); function destroy the session completely.
The following example is the code that you would normally put your php logout page. It simply logs out the user by deleting the session and redirects the browser to the index page.
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.php");
?>
Cookies Server Variables
Listed @ ConsumerVote.com - The Consumer Rated Web Directory
PC Articles
  Computer Safety
prevent viruses & Spyware