Php: whether the Session must depend on Cookie_PHP

Source: Internet
Author: User
Php: whether the Session must depend on cookies. The session in php can use the cookie of the client by default (in order to distinguish it from the cookie in the general sense, I call it sessioncookie, and the cookie in the general sense is cookie) to save the session in sessi php, the client cookie can be used by default (so as to be different from the cookie in the general sense, I call it session cookie, and the cookie in the general sense is a cookie) to save the session id, but does the session in php only use session cookies?

Of course not. Otherwise, it is better to use the cookie directly to get a session. A major advantage of session is that when the client's cookie is disabled, the session id will be automatically attached to the url, so that the session variable can be remembered through the session id.

Next, I will write two files to verify that the cookie is disabled in the browser.


[Php]
// The file name is test1.php.
Session_start ();
Session_register ("url ");
$ Url = "test2.php ";
Echo "goto test2.php ";
?>

// The file name is test1.php.
Session_start ();
Session_register ("url ");
$ Url = "test2.php ";
Echo "goto test2.php ";
?> [Php]
Session_start ();
If (session_is_registered ("url ")){
Echo "congratulations .";
$ Url = "test1.php ";
Echo "goto test1.php ";
} Else
Echo "failed .";
?>

Session_start ();
If (session_is_registered ("url ")){
Echo "congratulations .";
$ Url = "test1.php ";
Echo "goto test1.php ";
} Else
Echo "failed .";
?>


Enter "http: // localhost/test1.php" in the browser and move the mouse over the link to check the address on the status bar. this is not a simple "http: // localhost/test2.php ", but in this form: "http: // localhost/test2.php? Phpsessid = 6e3610949f7ded3784bc4a4dd10f879b ".
You can also view the html source file in this form:
Goto test2.php
Therefore, this is entirely the credit of php and has nothing to do with the browser. that is to say, no matter what browser session you use, it is valid, not what some people think is only useful for ie.
However, our hyperlink is the statement output by the echo statement. if the hyperlink is not included in the php tag What will happen? Let's write an example to verify it and make some modifications to test1.php:


[Html]
Session_start ();
Session_register ("url ");
$ Url = "test2.php ";
Echo "goto test2.php";?>
(Html form) goto test2.php

Session_start ();
Session_register ("url ");
$ Url = "test2.php ";
Echo "goto test2.php";?>
(Html form) goto test2.php
Enter "http: // localhost/test1.php" in the browser and move the mouse over the two links to see if they are different? As you can see, the two links are exactly the same, and the session id will be automatically attached later. so you don't have to worry that the link not included in the php tag will expire, and php will not be so stupid.
However, you must first use the session_start () function to tell php to start using session, even if you only have html code in this file, such:




Gogogo
............

I remember someone said that this advantage can only be used in linux/unix. However, I used win2000p + apache1.3.17 + php4.0.4pl1 and php as the apache module. on the contrary, I switched to linux for testing, but it didn't work. in fact, an option -- enable-trans-sid at compilation controls whether this function is useful. by default, php does not enable this function during compilation. you only need to add this function during re-compilation. my configuration is apache1.3.17 + php4.0.4pl1, and php is an apache Module. After linux is re-compiled, netscape navigator4.7 can be used to pass the test (this proves that it is not related to the browser ).
Session alone cannot be used across windows. even if you enable cookie, when you have a valid session id in a window (recorded in session cookie, not in url ), when you open a new window to enter the same page, you will have a new session id, which does not affect the previous window.
To use the same session id across Windows, you can only specify the session id after the url. that is to say, if you copy the url of the window with the session id, paste it in the new window, still used. it is not difficult to know the principle of session id to implement cross-Window sessions. we can combine cookie with session to obtain the current valid session id and record it in the cookie, read the cookie in other Windows to obtain the current session id.


Encrypt (so as to be different from cookie in the general sense, I call it session cookie, and cookie in the general sense is cookie) to save sessi...

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.