PHP Simple Session Tutorial

In: PHP

21 Sep 2009

Session is global variable which mainly use to send data from one page to another. Session variables allow you to store “global” data for use by all your pages and are specific or unique to each visitor to your site.” Using sessions you can transfer data between various pages. If you are using sessions then each of your visitors will got a unique id. This id will identify various visitors and with the help of this id are the user data stored on the server.”

Starting Session

session_start() creates a session or resumes the current one based on the current session id that’s being passed via a request, such as GET, POST, or a cookie.

Example:

<?php
  session_start();
  $_SESSION['name']='data you want to store';
?>

Session Variable should use before <html> tag.

Print Session Variable:

<?php
echo $_SESSION['name'];
?>

Or can be assign to any other variable names:

<?php
$username=$_SESSION['name'];
?>

Destroy a Session

To destroy any specific session use unset function.

<?php
unsert($_SESSION['name']);
?>

To destory all session variables use destroy function.

<?php
session_destroy();
?>

Related Articles

Comment Form

  • suba: $(this).addClass("active") and $(this).removeClass("hideme") does n't seem to be working in IE7, whe [...]
  • admin: @hadi: please check your code you may have deleted one "}" bracket. [...]
  • hadi: Hi, I get this error Parse error: syntax error, unexpected $end in C:\Users\acer\Desktop\xampp\h [...]
  • SF: High resolution zoom with image tiles: http://www.ajax-zoom.com [...]
  • Alonso: It seems that I didn't post my code properly sorry about that, that was not the problem however I wa [...]
Buy Stock Flash | Buy and Sell Royalty-Free Flash effects music and video files

Free web hostingWeb hosting
Display Pagerank