[Reposted] Solutions in PHP in PHP cannot transder problems across page

xiaoxiao2021-03-06  16

Friends who have used session in PHP may encounter such a problem, and the session variable cannot be passed across page. This makes me feel a lot of days, and I finally think about it and solve this problem. I think that the reason for this problem has the following points: 1, the client is banned with cookie 2, the browser has problems, temporarily unable to access the cookie 3, the session.use_trans_sid = 0 in php.ini is not opened when compiled - -ENABLE-Trans-SID option

Why is this so? Let me explain it:

SESSION is stored in the server side (default in file mode storage session), obtain the user's file, obtain the value of the variable, the session ID can use the client's cookie or HTTP1.1 protocol Query_String (Accessible) The "?" "?" Of the URL is transmitted to the server, and then the server reads the session directory ... That is, the Session ID is an ID card that acquires the session variable stored on the service. When the code session_start (); running, a session file is generated on the server, and there is also a SESSION ID that is unique, defining the session variable in a certain form of SESSION files you have just generated. With the session ID, you can take out the defined variables. After cross-page, in order to use session, you must perform session_start (); will generate a session file, with the corresponding session ID, with this session ID, is not a first session file mentioned earlier The variables of this session ID are not turning its "key". If you are in session_start (); before adding code session_id ($ session id); will not generate a new session file, directly read the session file corresponding to this ID.

The session in PHP is by default, using the client's cookie to save the session ID, so it will affect the session when the client's cookie occurs. It must be noted that session does not necessarily depend on cookie, which is also the SESSION compared to cookie's high. When the client's cookie is disabled or a problem, PHP automatically attaches the session ID in the URL, so that you can use the session variable across the page through the session ID. However, this adhesion is also a condition, ie "session.use_trans_sid = 1 in php.ini, opens up the --enable-trans-sid option" when compiled.

I have known the forum. When I enter the forum, I will tend to check if cookies open, because most forums are based on cookies, forums use it to save username, password and other user information, easy to use . And many friends think that cookie is not safe (in fact, not this), often disabled. In fact, in the PHP program, we can use the session instead of cookies, which can do not depend on whether the client is open to cookie.

So, we can leave the cookie using the session, which means that the user shuts down the cookie, using sessions, and the current path has the following:

1. Set session.use_trans_sid = 1 in php.ini or to turn on the -enable-trans-sid option when compiling, let PHP automatically pass the session ID. 2, manually pass the URL pass value, hide the form to pass the session ID. 3, save the session_id in the form of files, databases, manually calling during the cross page. Way 1 Example:

S1.php

Next page "; Echo $ URL ;?>

S2.php

Run the above code, in the case of the client cookie, you should be able to get the result "People's Republic". Now you manually turn off the client's cookie, run again, may not get the result. If you don't get the result, "Set the session.use_trans_sid = 1 in php.ini to open the --enable-trans-sid option", and get the results of the "People's Republic"

Way 2 Example:

S1.php

Next page "; Echo $ URL;?>>

S2.php

The method of hiding the form is basically the same.

Way 3 Example:

Login.html

login </ title> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312> </ head> <body> please login: <form name = "login" method = "post" action = "myLogin1.php"> Username: <input type = "text" name = "name"> <br> Password: <input type = "password" name = "pass"> <br> <input type = "submit" value = "login"> </ form> </ body> </ html></p> <p>MyLogin1.php</p> <p><? PHP</p> <p>$ Name = $ _ post ['name']; $ pass = $ _ post ['pass']; if (! $ name ||! $ pass) {echo "username or password is empty, please <a href =" login .html "> Re-login </a>"; die ();} if (! ($ name == "123") {echo "username or password is incorrect, please HREF = "login.html"> Re-login </a> "; Die ();} // Registered user ob_start (); session_start (); $ _SESSION ['user'] = $ name; $ psid = session_id () ; $ fp = fopen ("e: /tmp/phpsid.txt", "w "; FWRITE ($ fp, $ psid); fclose ($ fp); // Authentication success, performing Echo "has been logged in < Br> "; echo" <a href="mylogin2.php"> Next </a> ";</p> <p>MyLogin2.php</p> <p><? php $ fp = fopen ("e: /tmp/phpsid.txt", "r"; $ sID = fread ($ fp, 1024); fclose ($ fp); session_id ($ sID); session_start (); IF (isset ($ _ session ['User']) && $ _SESSION ['User'] = "laogong" {</p> <p>Echo "Logged in!";} Else {// Successfully logged in to perform related operations ECHO "Not logged in, no right to access"; echo "Please <a href="login.html"> Log in </A> After browsing"; DIE ();</p> <p>?></p> <p>Similarly, please close the cookie test, username: YOUNGONG password: 123 This is to save the session ID through the file, the file is: E: MPPHPSID.TXT, please determine the file name or path according to your own system.</p> <p>As for the method of using a database, I don't give an example, similar to the document method.</p> <p>Summarize, the above method has a common point, that is, get the session ID in the previous page, then find a way to pass to the next page, on the next page session_start (); before the code session_id (passing the session ID);</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-41514.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="41514" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.042</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'VJ4OZeXFfa2wJ4JPn36ch_2FU8RWV3oZibjvJROHLcs8CkPf2UkDI_2BoagcbWLFaHsdTkXWmJNOjtdJaddqLsgw5A_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>