Whether the session must rely on cookies

Source: Internet
Author: User
Tags copy goto html form html tags ini session id root directory linux
Does the cookie|session session have to rely on cookies?





www.ouryh.net Galactic Technology online ║ Finishing Time: 2001-8-7║ Author: unknown ║ Source: Unknown ║ read: 36







The session in
PHP can be saved by default by using the client's cookie (to distinguish it from the common cookie, which I call the session cookie and the cookie in the ordinary sense) to save the session ID. But can the session in PHP only use the session cookie? Of course not, otherwise why do you have a session to come out, It's better to use cookies directly. One of the great advantages of the session is that when the client's cookie is disabled, it automatically attached to the URL, so that it can be remembered by the session ID.


below I write two files to verify that you first set the Disable cookie in the browser.


;? File name is test1.php


session_start ();


session_register ("url");


$url = "test2.php";


echo "<a href= $url >goto test2.php</a>


";


?>





<?//file name is test2.php


session_start ();


if (session_is_registered ("url")) {


echo "Congratulations.


";


$url = "test1.php";


echo "<a href= $url >goto test1.php</a>


";


}


else echo "Failed.


";


?>


now enter "http://localhost/test1.php" in the browser, move the mouse over the link to see the address on the status bar, not simply "http://localhost/test2.php", but this form: "http:// Localhost/test2.php? phpsessid=6e3610749f7ded3784bc4a4dd10f879b ". You can also view the HTML source file, the source file is this form:


<a href= "test2.php? phpsessid=6e3610749f7ded3784bc4a4dd10f879b ">goto test2.php</a>


so this is completely PHP credit, and browser-independent, which means that no matter what browser you use the session is effective, rather than some people think only for IE useful.


However, our hyperlink is that the statement is output by the Echo statement, if the hyperlink is not included in the PHP tag. What happens within?>? or write an example to verify that the test1.php slightly modified:


;?


session_start ();


session_register ("url");


$url = "test2.php";


echo "<a href= $url >goto test2.php</a>


";





?>


<a href= "test2.php" > (HTML form) goto test2.php</a>


In the browser type "http://localhost/test1.php", the mouse moved to the two links to see if there is no difference? As you can see, two links are exactly the same, followed automatically by a session Id. So don't worry that the links that aren't included in the PHP tags will fail, and PHP won't be so stupid.


but note that you must first use the Session_Start () function to tell PHP to start using the session, even if you have only HTML code in this file, such as:


;? Session_Start ();? >


<html>


<head>


<body>


<a href=test2.php>gogogo</a>


............





Remember someone said that this advantage can only be played under the Linux/unix, and I use the win2000p+apache1.3.17+php4.0.4pl1,php as the Apache module way, but it can. On the contrary, I'm not going to go to Linux to test it. is actually at compile time an option--ENABLE-TRANS-SID control whether this function is useful. and PHP defaults to compile when this feature is not turned on, Just add it when you recompile. My configuration is apache1.3.17+php4.0.4pl1,php as Apache module, after the Linux recompile with Netscape Navigator4.7 test can pass (this is more proof of browser-independent ).


Only session can not be used across windows, even if you enable cookies, when you have a valid session ID in a window (recorded in the session cookie, not the URL), and then open a new window into the same page, You'll have a new session ID, which will not affect the previous window. To use the same session ID across Windows, you can specify the session ID only after the URL, that is, if you copy the URL of a window with the session ID, Paste it in a new window, or use it anyway. Know the session ID of this principle to achieve a cross window session is not difficult, you can combine cookies with session, first get the current legal session ID, It is then recorded in a cookie, and the current session ID is obtained by reading the cookie in another window. Specific implementation I remember there was an article on Phpuser that was devoted to discussion.


finally say:


① often asked "Why copy you write code, but you will be wrong, you are too ...", and then take a look at the error tip:


Warning:open (/TMPSESS_ECA1DA208748DB2E9C6BEC1FCCC182B4, O_RDWR) failed:m (2) in c:/www/test1.php to line 2


is actually his own problem: the path stored in the session/TMP does not exist. There are two ways: one is to create a directory named TMP in the root directory (typically C:); the second is to modify the php.ini file


Session.save_path =/tmp;


assigns the/tmp directory to a directory with an absolute path (must exist, of course), as in my php.ini


Session.save_path = g:phptempsession;


② There is also a situation where the error message is:


Warning:cannot Send session cache Limiter-headers already sent (output started at


c:/www/test1.php:1) in c:/www/test1.php to line 2


This is because you have to use the session_start () before the data output to the client, such as HTML tags, text or even a blank space, so it is best to use the first sentence of the program session_start ().


Good, said all these, I personally tested through, if there is wrong or inaccurate place welcome discussion (xcloudy@china.com).


 





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.