|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Understanding and Using Sessions
By: FYICenter.com
Part:
1
2
3
4
5
A collection of 19 tips on understanding and using sessions in PHP. Clear explanations and tutorial exercises are provided on starting and closing sessions, saving and retrieving values in sessions, deciding how session IDs to be transferred, deciding where to store session files, deciding when to expire session values, etc.
Topics included in this collections:
- What Is a Session?
- How To Turn On the Session Support?
- How To Save Values to the Current Session?
- How To Retrieve Values from the Current Session?
- What Is a Session ID?
- How To Retrieve the Session ID of the Current Session?
- What Are the Options to Transfer Session IDs?
- How Session IDs Are Transferred on Your Web Server?
- How To Force the PHP Engine to Use Cookies to Transfer Session IDs?
- Is It More Secure to Use Cookies to Transfer Session IDs?
- Where Are the Session Values Stored?
- What Is the Timeout Period on Session Values?
- How To Test the Session Garbage Collection Process?
- How To Set session.gc_maxlifetime Properly?
- How To Set session.gc_divisor Properly?
- How To Remove Values Saved in the Current Session?
- How To Tell If a Session Is New?
- How To Close a Session Properly?
- What Is session_register()?
What Is a Session?
A session is a logical object created by the PHP engine to allow you
to preserve data across subsequent HTTP requests.
There is only one session object available to your PHP scripts at any time.
Data saved to the session by a script can be retrieved by the same script
or another script when requested from the same visitor.
Sessions are commonly used to store temporary data to allow multiple PHP pages
to offer a complete functional transaction for the same visitor.
How To Turn On the Session Support?
The session support can be turned on automatically at the site level, or
manually in each PHP page script:
- Turning on session support automatically at the site level: Set session.auto_start = 1 in php.ini.
- Turning on session support manually in each page script: Call session_start() funtion.
How To Save Values to the Current Session?
When session is turned on, a session will be automatically created for you
by the PHP engine. If you want to save any values to the session, you can use the
pre-defined associative array called $_SESSION. The following PHP script shows
you how to save values to the session:
<?php
session_start();
print("<html><pre>");
$_SESSION["MyLogin"] = "FYICenter";
print("A value saved in the session named as MyLogin.\n");
$_SESSION["MyColor"] = "Blue";
print("A value saved in the session named as MyColor.\n");
print("Click <a href=next_page.php>Next Page</a>"
." to retrieve the values.\n");
print("</pre></html>\n");
?>
If you save this script to your Web server as first_page.php and
visit it with a browser, you will get:
A value saved in the session named as MyLogin.
A value saved in the session named as MyColor.
Click Next Page to retrieve the values.
How To Retrieve Values from the Current Session?
If you know some values have been saved in the session by an other script
requested by the same visitor, you can retrieve those values back by using
the pre-defined associative array called $_SESSION. The following PHP script shows
you how to retrieve values from the session:
<?php
session_start();
print("<html><pre>");
$myLogin = $_SESSION["MyLogin"];
print("Value of MyLogin has been retrieved: ".$myLogin."\n");
$myColor = $_SESSION["MyColor"];
print("Value of MyColor has been retrieved: ".$myColor."\n");
print("</pre></html>\n");
?>
You need to save this script to your Web server as next_page.php. Now visit first_page.php
and click the "Next Page" hyper like, you will get:
Value of MyLogin has been retrieved: FYICenter
Value of MyColor has been retrieved: Blue
(Continued on next part...)
Part:
1
2
3
4
5
|