ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Sessions

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
ÓÃPHP5µÄDirector..
PHP ×èÖ¹SQL×¢ÈëÊ..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
ÓÃPHP¿ØÖÆÓû§µÄä..
PHP PDO ѧϰ±Ê¼Ç

PHP Sessions


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-07-24 ä¯ÀÀ:7274 :: ::

A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.
PHP session±äÁ¿µÄ×÷ÓÃÊÇ£º´æ´¢Óû§µÄsessionÐÅÏ¢£¬»òÕ߸ıäÓû§µÄsessionÉèÖá£Session±äÁ¿´¢´æÁËÒ»¸öµ¥Ò»Óû§µÄÐÅÏ¢£¬Ëü¿ÉÒÔ±»ËùÓеÄÒ³ÃæÊ¹Óá£


PHP Session Variables
PHP Session±äÁ¿

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
µ±ÄãÔÚ×Ô¼ºµÄ¼ÆËã»úÉÏÔËÐÐÒ»¸öÓ¦ÓóÌÐòʱ£¬Äã´ò¿ªËü£¬¶ÔËû×öһЩ¸Ä±ä£¬È»ºó¹Ø±ÕËü£¬Õâ¸ö¹ý³ÌºÍsessionºÜÏàËÆ¡£¼ÆËã»úÖªµÀÄãÊÇË­£¬ÖªµÀÄãʲôʱºòÆô¶¯ÁËÓ¦ÓóÌÐò£¬Ê²Ã´Ê±ºò¹Ø±ÕÁËÓ¦ÓóÌÐò¡£µ«ÔÚ»¥ÁªÍøÉÏ£¬»á³öÏÖÕâÑùÒ»¸öÎÊÌ⣺ÒòΪHTTPµØÖ·²»¿ÉÄÜÓÀ¾ÃµÄ±£ÁôÏÂÀ´£¬ËùÒÔ·þÎñÆ÷ºÜÄѱæÈÏÄãÊÇË­£¬ÄãÔÚ¸Éʲô¡£

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.
PHP SessionÔÊÐíÄãÔÚ·þÎñÆ÷ÉÏ´¢´æÓû§ÐÅÏ¢£¨È磺Óû§Ãû[username]¡¢¹ºÎïÇåµ¥[shopping]µÈµÈ£©£¬´Ó¶ø½â¾öÁËÕâ¸öÎÊÌ⡣Ȼ¶ø£¬sessionÐÅÏ¢Ò²ÊÇÁÙʱ´æÔڵ쬵±ÄãÀ뿪Õâ¸öÍøÕ¾Ê±£¬Ëû»á±»×Ô¶¯É¾³ý¡£Èç¹ûÄãÏëÓÀ¾Ã±£ÁôÕâЩÐÅÏ¢£¬Äã¿ÉÒÔ³¢ÊÔ×ŰÑËü´¢´æÔÚÊý¾Ý¿âÖС£

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.
Sessionͨ¹ýΪÿ¸ö·ÃÎÊÕß´´½¨Ò»¸ö¶ÀÁ¢µÄID£¨UID£©²¢´¢´æ»ùÓÚUIDµÄ±äÁ¿À´ÔËÐС£UID´æ´¢ÔÚcookieÖУ¬»òÔÚURLÖÐÕ¹ÏÖ³öÀ´¡£


Starting a PHP Session
Æô¶¯PHP Session

Before you can store user information in your PHP session, you must first start up the session.
ÔÚÄ㽫Óû§ÐÅÏ¢´æÈëPHP Session֮ǰ£¬Äã±ØÐëÏÈÆô¶¯Session¡£

Note: The session_start() function must appear BEFORE the <html> tag:
×¢Ò⣺session_start()º¯Êý±ØÐëдÔÚ<html>±êǩ֮ǰ£º

<?php session_start(); ?>
<html>

<body>
</body>
</html>

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.
ÉÏÊö´úÂ뽫ÔÚ·þÎñÆ÷ÉÏ×¢²áÒ»¸öÓû§µÄsession£¬ÔÊÐíÄã´¢´æÓû§ÐÅÏ¢£¬²¢ÎªÓû§sessionÖ¸¶¨Ò»¸öUID¡£


Storing a Session Variable
´¢´æÒ»¸öSession±äÁ¿

The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:
´¢´æºÍ»ñÈ¡session±äÁ¿µÄ×î¼Ñ·½·¨ÊÇʹÓÃPHP $_SESSION±äÁ¿£º

<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>

Output:
½á¹û£º

Pageviews=1

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:
ÔÚÉÏÊö°¸ÀýÖУ¬ÎÒÃǽ¨Á¢ÁËÒ»¸ö¼òÒ×µÄÒ³Ãæ¼ÆÊýÆ÷¡£Isset()º¯Êý¼ì²é“views”±äÁ¿ÊÇ·ñÒѾ­±»ÉèÖá£Èç¹û“views”±äÁ¿ÒѾ­±»ÉèÖÃÁË£¬ÎÒÃÇ»áÔö¼ÓÎÒÃǵļÆÊý¡£Èç¹û“views”±äÁ¿²»´æÔÚ£¬ÎÒÃÇ»áÏÈ´´½¨Ò»¸ö“views”±äÁ¿£¬²¢°Ñ“1”¸³¸øËü¡£

<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>

 


Destroying a Session
ɾ³ýSession

If you wish to delete some session data, you can use the unset() or the session_destroy() function.
Èç¹ûÄãÏ£Íûɾ³ýһЩsessionÊý¾Ý£¬Äã¿ÉÒÔʹÓÃunset()º¯Êý»òsession_destroy()º¯Êý¡£

The unset() function is used to free the specified session variable:
Unset()º¯ÊýµÄ×÷ÓÃÊÇÊÍ·ÅÖ¸¶¨µÄsession±äÁ¿£º

<?php
unset($_SESSION['views']);
?>

You can also completely destroy the session by calling the session_destroy() function:
ÄãÒ²¿ÉÒÔʹÓÃsession_destroy()º¯Êý½«sessionÈ«²¿É¾³ý£º

<?php
session_destroy();
?>

Note: session_destroy() will reset your session and you will lose all your stored session data.
×¢Ò⣺session_destroy()»áÖØÐÂÉèÖÃÄãµÄsession£¬Äã»á¶ªÊ§ËùÓÐÒѱ£´æµÄsessionÊý¾Ý¡£

ÆÀÂÛ (3) 1 All