A detailed analysis of PHP session

Source: Internet
Author: User

1. PHP session working principle

The session file is stored on the server side and, by default, the directory saved by the session file is specified by Session.save_path, the filename is prefixed with sess_, followed by the session ID, such as: Sess_ c72665af28a8b14c0fe11afe3b59b51b. You can come to the user's file based on the session ID provided by the client, get the value of the variable, and the session ID can use the client's cookie or the query_string of the Http1.1 protocol (that is, the "?" of the access URL). Later) to the server, and the server reads the directory of the session. In other words, the session ID is the identity card that gets the session variable stored on the service.

When the Code session_start (), the runtime, on the server generated a session file, followed by a unique corresponding session ID, the definition of the session variable in a certain form stored in the session file just produced. With the session ID, you can take out the defined variables. After the page spread, in order to use the session, you must also execute session_start (), and will produce a session file, corresponding to produce the corresponding session ID, with this session The ID is not a variable in the first session file mentioned earlier, because the session ID is not the key to open it. If the code session_id ($session ID) is preceded by the session_start (), the new session file is not generated and the session file corresponding to this ID is read directly.

2. Session common function and usage

2.1 Session_Start (): starts a session or returns a session that already exists.

This function has no arguments and the return value is true. If you use a cookie based session, the browser cannot have any output before using Session_Start (). You can start the session.auto_start= in php. ini 1 so that you do not need to invoke session_start () every time you use the session. However, there are some limitations on enabling this option, and if Session.auto_start is enabled, you cannot put the object into a session because the class definition must be loaded before the session is started to rebuild the object in the session.

2.2 Register Session Variable:

PHP5 registers the session global variable with the $_session[' xxx ']=xxx. Note Session_register (),

Session_unregister, session_is_registered is no longer used under PHP5, unless in php.ini

Register_globle is set to on, but for security reasons, it is strongly recommended that register_globle be closed.

Http_session_vars also does not advocate the use, the official proposal uses $_session to replace it.

page1.php
<?php
Session_Start (); You must call this function before you use session.
$_session[' name ']= "I am the black whirlwind likui!"; Register a Session variable
$_session[' passwd ']= "Mynameislikui";
$_session[' time ']=time ();

If the client supports cookies, you can pass the session to the next page through the link.
Echo ' <br/><a href= "page2.php" > Pass the Cookie session</a> ';

This method is used to pass the session when the client does not support cookies.
Echo ' <br/><a href= ' page2.php? Sid. ' > Pass session</a> via URL ';


page2.php
<?php
Session_Start ();
echo $_session[' name '];

echo $_session[' passwd '];

echo Date (' Y m D h:i:s ', $_session[' time ']);
Echo ' <br/><a href= "page1.php" > Return to prev </a> ';
?>

2.3 session_id ([string $id]): Get and/or set the current session ID

either session_id () can be used in php5, or the session_id and Session_name of the current session can be obtained by the SID attached to the URL.

If session_id () has a specified value (that is, the parameter $id is specified), the current session_id value is replaced. You must start a session before using this function: session_start ();

Example: Set the lifetime of the session manually:

<?php

Session_Start ();

Save one day

$lifeTime = 24 * 3600;

Setcookie (Session_name (), session_id (), time () + $lifeTime, "/");

?>

In fact, the session also provides a function session_set_cookie_params (); To set the lifetime of the session, the function must be called before the session_start () function call:

<?php

Save one day

$lifeTime = 24 * 3600;

Session_set_cookie_params ($lifeTime);

Session_Start ();

$_session["Admin"] = true;

?>

If the client uses IE 6.0, Session_set_cookie_params (); There are some problems with the function setting cookies, so we call the Setcookie function manually to create the cookie.

2.4 Check whether the session exists.

In previous versions of PHP, Session_is_register () was often used to check for the existence of sessions, and if you used $_session[' XXX ']=xxx to register session variables, Session_is_register () function no longer works. You can use
Isset ($_session[' xxx ') to replace.

2.5 Change session_id session_regenerate_id ([bool$delete_old_session]) returns True if the change succeeds, and returns False if it fails.

You can use this function to change session_id for the current session, but the default does not change other information for the current session unless $delete_old_session is true. For example:
<?php
Session_Start ();
$old _sessionid = session_id ();
SESSION_REGENERATE_ID ();
$new _sessionid = session_id ();
echo "Original SessionID: $old _sessionid<br/>";
echo "New SessionID: $new _sessionid<br/>";
echo "<pre>";
Print_r ($_session);
echo "</pre>";
?>

The

2.6 session_name () returns the name of the current session or changes the name of the current session. If you want to change the name of the current session, you must call the function before Session_Start (). Note: Session_name cannot consist of only numbers, it contains at least one letter. Otherwise, a new session ID will be generated at every moment.
Session Rename Example:
<?php
$previous _name = Session_name ("WebSiteID");
Echo "New session Name: $previous _name <br/> ";
?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.