PHP session 小结

初学php,自己弄个小结:)
Hope it can be helpful! 
 
PHP Session
  • How a session starts?
    • Automatically if session.auto_start is set to 1
    • Explicitly through session_start()
    • Implicitly through session_register()
  • In your php.ini session.save_path should be set to a valid path, I find sometimes the default value is /tmp which is not valid for windows by default. Change it to "", then the session can start normally. This issue only exists before PHP 4.3.6.

Note:

Prior to PHP 4.3.6, Windows users had to change this variable in order to use PHP’s session functions. A valid path must be specified, e.g.: c:/temp.

  •  If you use $_SESSION and register_global is off, there’s no need for you to know session_register() or session_unregister or session_is_regisstered.
  • To setup a session variable, use $_SESSION[$key] = $value;

To unregister, use unset($_SESSION[$key]);

  • To end a session, use session_destroy();
  • If session id is passed by cookie, SID will be an empty string. SID is available when session id is passed by url.

Note that SID is only defined if the client didn’t send the right cookie.

  • To check whether a session has been started, use the following code:

$started = session_id() ? true : false;

because if session is not started session_id() returns ""(empty string), it’s equivalent to write

$started = (session_id() != "") ? true : false;

 

/**

* check whether the session has been started

* @return  bool, true if the session has been started else false

*/

function isSessionStarted() {

    return !!session_id();

}

  • Different instances of a web browser may send different cookie header info. For example, a newly opened ID window will not send the same session id as its parent window. To share a same session among different browser instances, you can specify the session id by session_id($id);

where $id is gained by $id = session_id();

However, IE6 doesn’t sends the same cookie for both the parent and the child window.

demo:

test.php is written as

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>Counter</title>

</head>

 

<body>

<?php

session_start();

if (!isset($_SESSION[‘count’])) {

  $_SESSION[‘count’] = 0;

} else {

  $_SESSION[‘count’]++;

}

echo "You have visited the page for ".$_SESSION[‘count’]." times.<br/>";

?>

<a href="#" title="Open the same page in a new browser window" target="_blank">Open me in new window</a>

 

</body>

</html>

 

Launch 2 instances of IE, type the same url and press go, then click refresh button to observe how the counter grows. You will find that the counters in two windows are independent.

Screen clipping taken: 2005-7-20, 21:57

 

 

Now click the link on the page to open a new window you will see the newly opened window shares the same session with the parent window.

 

In the example above, we open a new window via <a href="#">..</a>. We can approach the same result using javascript:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>Counter</title>

<script language="javascript">

<!–

var popUpWin=0;

function popUpWindow()

{

popUpWin = open("http://localhost/test/test.php&quot;);

}

//–>

</script>

</head>

 

<body>

<?php

session_start();

if (!isset($_SESSION[‘count’])) {

  $_SESSION[‘count’] = 0;

} else {

  $_SESSION[‘count’]++;

}

echo "You have visited the page for ".$_SESSION[‘count’]." times.<br/>";

?>

<p>

<input name="Button" type="button" onclick="popUpWindow()" value="Open me in new window" />

</p>

</body>

</html>

 

Counter

http://localhost/test/test.php

Screen clipping taken: 2005-7-20, 22:35

 

Advertisements
This entry was posted in PHP Rocks. Bookmark the permalink.

One Response to PHP session 小结

  1. Unknown says:

    Hi,Do you have used LCDs, used flat screens and secondhand LCDs? Please go here:www.sstar-hk.com(Southern Stars).We are constantly buying re-usable LCD panels and working for LCD recycling.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels.
    Contact Us
    E-mail:sstar@netvigator.com
    website:www.sstar-hk.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s