Cookies are disabled, can the Session still be used?

WBOY
Release: 2016-07-28 08:28:21
Original
1005 people have browsed it
Cookie and Session are generally considered to be two independent things. Session uses a solution that maintains state on the server side, while Cookie uses a solution that maintains state on the client side. But why can't I get the Session if I disable cookies? Because the Session uses the Session ID to determine the server Session corresponding to the current conversation, and the Session ID is passed through Cookie, disabling Cookie is equivalent to losing the Session ID, and thus the Session is lost.
Is it true that if Cookie is disabled, Session will definitely not be usable?
1. ASP
In ASP, Session must rely on Cookie to be available. Session is stored on the server side, while Cookie is stored on the client side. Relatively speaking, Session is more secure and reliable than Cookie.
2. PHP
In PHP, through relevant configuration, Session can exist without relying on cookies. This is because:
Session is stored on the server side (the Session is stored as a file by default). The user's file is obtained according to the Session ID provided by the client, and the value of the variable is obtained. The Session ID can use the client's Cookie or Http1.1 protocol. Query_String (the part after the "?" of the accessed URL) is sent to the server, and then the server reads the Session directory... In other words, Session ID is the ID card for obtaining the Session variable stored on the service. When the code session_start(); is run, a Session file is generated on the server, and a Session that uniquely corresponds to it is also generated. ID, defines the Session variable to be stored in the Session file just generated in a certain form. Through the Session ID, the defined variables can be retrieved. After crossing the page, in order to use the Session, you must execute session_start() again; another Session file will be generated, and the corresponding Session ID will be generated accordingly. Using this session ID, you cannot retrieve the first Session file mentioned above. variable in because this Session ID is not the "key" to open it.If you add the code session_id($session id); before session_start();, a new Session file will not be generated, and the Session file corresponding to this id will be read directly.
     Session in PHP uses the client’s cookie to save the session ID by default, so when there is a problem with the client’s cookie, it will affect the session. It must be noted that Session does not necessarily have to rely on Cookie, which is also the advantage of Session compared to Cookie. When the client's cookies are disabled or there is a problem, PHP will automatically attach the Session ID to the URL, so that the Session variable can be used across pages through the Session ID. But this attachment also has certain conditions, namely "session.use_trans_sid in php.ini" = 1", or the "--enable-trans-sid" option is turned on when compiling.
Friends who have used the forum know that when entering the forum, you are often prompted to check whether cookies are turned on. This is because most Forums are all based on cookies, which are used to save user information such as user names and passwords for ease of use. Moreover, many friends think that cookies are not safe (this is not the case) and often disable them in PHP programs. Use Session to replace Cookie, which does not depend on whether the client turns on Cookie
So, we can put aside Cookie and use Session, that is, assuming that the user turns off Cookie, use Session. There are several ways to achieve this:
1. Set "session.use_trans_sid = 1" in the php.ini configuration file, or turn on the "--enable-trans-sid" option when compiling to let PHP automatically pass the Session ID across pages
2. Manually pass the value through the URL. , hide the form to pass the Session ID.
3. Save the Session ID in a file, database, etc., and call it manually during the cross-page process.
Example 1:
---------------- -------------------------------------------------- --------------------------------------------------
                                 iguowei2004.blog. 163.com/blog/.""s2.php">Next page";
              echo $url;
        ?>
                                                                                 -------------------------------------------------- --------------------------------------------------
------------------------------------------------ -------------------------------------------------- ----------------
                                                                                                                                                                                                                                        // s2.php var1'];
?>
---------------------------------------- -------------------------------------------------- ------------------------
Run the above code, and if the client cookie is normal, you should be able to get the result "People's Republic of China".
Now if you manually close the cookie on the client and run it again, you may not get the result. If you cannot get the result, set "session.use_trans_sid = 1" in the php.ini file, or turn on the "--enable-trans-sid option" when compiling, and you will get the result "People's Republic of China".
      Path 2 Example:
    ------------------------------------------------ -------------------------------------------------- -----------------------
                                                                                                                                                                         Republic";
            $sn = session_id();
          $url="Next page";
                   echo $url;
                                                                                                      -------------------------------------------------- ---------------------------------------
-------- -------------------------------------------------- -------------------------------------------------- ---- l & lt;? Php
session_id ($ _ get ['s']);
session_start ();
the value of the session variable var1 transmitted by echo "is:". $ _ Session ['var1'];
?>
------------------------------------------------ -------------------------------------------------- ------------------
                                                                                                                                                                                                              -------------------------------------------------- --------------------------------------------------
          login.html
                                           HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                                                        ="Content- Type" c/html; charset=gb2312">
                                                                                                                                                                                                                                               
                                                              use using using using  ="submit" value="Login">
                                                                                                                                                                              -------------------------------------------------- ----------------------------------------
---- -------------------------------------------------- -------------------------------------------------- -----------
mylogin1.php
|| !$pass) {
                                                                                                                            | "; E die ();
}
if (! ($ Name ==" laigw "&& $ pass ==" 1234 ") {
echo" user name or password is incorrect, please & lt; a href = "http ://laiguowei2004.blog.163.com/blog/login.html">Re-login";
            die();
      }
              //Registered user
                                                            using use using ’ ’ s ’       ‐         ‐                              phpsid.txt"," w+"); r fwrite ($ fp, $ psid);
fclose ($ fp);
// authentication is successful, and related operations

& & & lt;";
echo "& lt; a href = "http://laiguowei2004.blog.163.com/blog/mylogin2.php">Next page";
?>
---------------- -------------------------------------------------- -------------------------------------------------- -
------------------------------------------------ -------------------------------------------------- ------------------
mylogin2.php
$fp=fopen("D:tmpphpsid.txt","r");
$sid=fread ($fp,1024);
                        fclose($fp);                                                                                                                                                                                                                                                           ​"{H Echo" has been logged in! ";
} else {
// Successfully log in for related operations






" "



& & lt; a href =" http://laiguowei2004.blog .163.com/blog/login.html">LoginBrowse";
         die();
                     
                                               -                                      -------------------------------------------------- --------------------------------------------------
Please also turn off Cookie testing, username: laigw; password: 1234; this is to save the Session ID through a file, the file is: D:/tmp/phpsid.txt, please decide the file name or path according to your own system.
To summarize, the above methods have one thing in common, which is to obtain the Session ID on the previous page, and then find a way to pass it to the next page. Add the code Session ID (the passed Session ID) before the session_start(); code on the next page. ).

The above has introduced that if Cookie is disabled, can Session still be used? , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!