Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

PHP Tutorial
PHP Introduction
PHP Install
PHP Syntx
PHP Variables
PHP Operators
PHP Loops & Arrays
PHP Functions
PHP Forms
PHP Includes & Require
PHP File Handling
PHP Cookies
PHP Session
PHP E-mail
PHP SQL Database
PHP Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


Session in PHP

Previous Next



Besides cookies, there is one more way to pass information to different web-pages: Sessions. A session-enabled page allocates unique identifiers to the users the first time they access the page, and then reassociates them with previously allocated ones when they return to the page. Any global variables which were associated with the session will then become available to your code.


The difference between sessions and cookies

The main difference between sessions and cookies is that a session can hold multiple variables, and you need not have to set cookies for every variable. By default, the session data is stored in the cookie wich has an expiry date of zero, which means that the session remains active only as long as the browser.Once you close the browser, all the stored information is lost. This behavior can be modified by changing the “session. cookie_lifetime” setting in “php.ini” from zero to whatever you want the cookie lifetime to be.


How to Start a Session

Before starting to work with sessions, you must explicitly start a session using “session_start()” function. If you want sessions to start themselfs automatically, you should enable the “session.auto_start” setting in PHP’s configuration file.

session_start();
//starts or resumes a function
print “Your session ID is: “ . session_id();
//displays the session ID
session_destroy();
//ends the session; comment this line and the browser will output the same session ID as before

After starting a session, you can access to the session ID via the “session_id()” function. After completing the work, you can destroy the session using “session_destroy()”.


Register Variables to a Session

the main objective of the session is to hold the values of variables. You must register session variables using the “session_register()” function, before trying to read them on a session-enabled page. Remember that a “session_register()” requires you to pass as an argument "variable name", and not the variable itself:

<?php
session_start();
?>
<html>
<body>
<?php
if(isset($stored_var))
{
print $stored_var; //this will not be displayed the first time you load the page, because you haven’t registered the variable yet!
}
else
{
$stored_var = “Hello from a stored variable!”;
session_register(“stored_var”); //don’t do this: session_register($session_var)
}
?>
</body>
</html>

you can test if a variable is assigned using the “isset()” function.


Remove the Registered Variables

To remove the registered variables, you need to use the session_unset() function. This function when called destroys all variables associated with a session, both in the the script and within session file .

<?php
session_start();
?>
<html>
<body>
<?php
session_register("test");
$test = 12;
print $test;//outputs 12 session_unset(); //$test is destroyed
session_destroy();
print $test; //outputs nothing
?>
</body>
</html>




Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords:php tutorial, php scripts, php nuke, php download, php editor, php mysql, php forum, php add link, learn php, php code


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.