Session
What is Session
??Session starts from the time when the user accesses the page and ends when the user disconnects from the website, forming a life cycle of the session. During the session, the client is assigned a unique SessionID to identify the current user and distinguish it from other users.
?? During a Session, the Session ID will be saved in two locations: the client and the server. For the client, a temporary cookie is used to save it (the cookie name is PHPSESSID) or passed through a URL string. The server is also saved in the form of a text file. In the specified Session directory.
??Session accepts each access request through ID, thereby identifying the current user, tracking and maintaining user specific information, and Session variables (during Session activity, numeric or text information can be stored in Session), such as session_name, etc., these Variable information is saved on the server side.
??SessionID can be saved to the database as session information for session persistence, so that each user's login times, online status, online time, etc. can be tracked.
The difference between cookies and sessions in php:
Both cookies and sessions can temporarily save variables used in multiple pages, but they have essential differences.
??Cookies are stored in the client browser,
??session is stored on the server.
??The connection between them is the session ID, which is usually stored in a cookie or placed on the URL.
??How to disable cookies: (Click "Tools" - "Internet Options" in IE, click "Security" - "Custom Level" item in the pop-up dialog box, and set "Allow per conversation COOKIE" to Disabled)
Implementing a simple session
The basic steps for using a session are as follows:
??Start a session
??Register session variables
??Use session variables
??Unregister variables and destroy the session
1 Start a session
session_start(): Start a session or return an existing session.
Note: This function has no parameters and the return value is true. If you use a cookie-based session, the browser must not have any output before using session_start(), otherwise the following error will occur:
Warning: Cannot send session cache limiter -headers already sent (output started at/usr/local/apache /htdocs/cga/member/1.php:2)…………
2 Register a session variable
PHP5 uses $_SESSION['xxx']=xxx to register the SESSION global variable. The usage methods of GET, POST and COOKIE are similar.
??Note: session_register(), session_unregister(), session_is_registered() are no longer used under php5, unless register_globle is set to on in php.ini. However, for security reasons, it is strongly recommended to turn off register_globle. The use of HTTP_SESSION_VARS is no longer recommended, and the official recommendation is to use $_SESSION instead.
For example: echo $_SESSION[‘myvar’];
?? Before use, determine whether the variable is a registered session variable.
if(isset($_SESSION[‘myvar’]))
<?<span>php session_start(); </span><span>//</span><span>使用SESSION前必须调用该函数。</span>$_SESSION[‘name’]=”我是黑旋风李逵!”; <span>//</span><span>注册一个SESSION变</span><span>量 $_SESSION[‘passwd’]</span>=<span>”mynameislikui”; $_SESSION[‘time’]</span>=<span>time(); echo ‘<br</span>/><a href=<span>“page2.php”>通过COOKIE传递SESSION <</span>/a>’; <span>//</span><span>如果客户端支持cookie,可通过该链接传递session到</span><span>下一页。 echo ‘<br</span>/><a href=“page2.php?<span>’. SID . ‘”>通过URL 传递SESSION<</span>/a>’;<span>//</span><span>客户端不支持cookie时,使用该办法传递</span><span>session. </span>?>
??If session_id() has a specific value, it will replace the current SESSIONID value.
When cookies are not disabled, if a session_id() value is specified, a cookie value will be sent to the client every time session_start() is started. Regardless of whether the current SESSIONID is equal to the specified value.
??If session_id() does not specify a value, the current SESSIONID is returned. If the current session is not started, an empty string is returned.
??You must use this function before starting the session: session_start();
<span>查看当前SessionID </span><?<span>php session_start(); echo “当前的session id 为:”.session_id(); </span>?><span>??设置SessionID </span><?<span>php session_id(“ABC2008”);</span><span>//</span><span>必须在session_start()之前</span><span>session_start(); echo “修改的session id 为:”.session_id(); </span>?>
??Using this function allows the current session to replace the SESSIONID, but does not change other information of the current session.
<?<span>php session_start(); $old_sessionid</span>=<span> session_id(); session_regenerate_id(); $new_sessionid</span>=<span> session_id(); echo </span><span>"</span><span>原始SessionID: $old_sessionid<br/></span><span>"</span><span>; echo </span><span>"</span><span>新的SessionID: $new_sessionid<br/></span><span>"</span><span>; echo</span><span>"</span><span><pre></span><span>"</span><span>; print_r($_SESSION); echo</span><span>"</span><span></pre></span><span>"</span><span>; </span>?>
??If you want to change the name of the current session, you must call this function before session_start()
. Note: session_name cannot be composed of only numbers, it must contain at least one letter. Otherwise, anew session id will be generated every moment.
Session rename example:
<?<span>php $previous_name</span>= session_name(<span>"</span><span>WebsiteID</span><span>"</span><span>); session_start(); echo </span><span>"</span><span>新的session名为:$previous_name<br/></span><span>"</span><span>;</span>?>
Session cross-page transfer needs to consider three situations:
??The client is disabled cookies.??There is a problem with the browser and cookies are temporarily unable to be accessed
??session.use_trans_sid= 0 in php.ini orthe --enable-trans-sid option is not turned on when compiling
Three tips to solve the problem of session cross-page transfer Way
1、设置php.ini中的session.use_trans_sid= 1或者编译时打开打开了--enable-trans-sid选项,让PHP自动跨页传递session id。
??2、手动通过URL传值、隐藏表单传递session id。
??3、用文件、数据库等形式保存session_id,在跨
页过程中手动调用。
1 解决会话传递问题
<span>page1.php ??<</span>?<span>php session_start(); $_SESSION[‘var1’]</span>=<span>“中华人民共和国”; $url</span>=“<a href=‘s2.php’>下一页</<span>a>”; echo $url; </span>?<span>><br></span>
<span>??page2.php ??<</span>?<span>php session_start(); echo “传递的session变量var1的值:”.$_SESSION[‘var1’]; </span>?<span>> ??现在你手动关闭客户端的cookie,再运行就得不到结果了</span>
2 第一种途径
设置php.ini中的session.use_trans_sid= 1
??编译时打开打开了--enable-trans-sid选项”
??注:Linux适用,Windows不适用
以上就介绍了PHP会话跟踪二(42),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。