Home  >  Article  >  Backend Development  >  Solution to the problem that PHP Session variables cannot be transferred to the next page_PHP Tutorial

Solution to the problem that PHP Session variables cannot be transferred to the next page_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:191021browse

I think the reasons for this problem are as follows:
1. Cookies are disabled on the client
2. There is a problem with the browser and cookies cannot be accessed temporarily
3. Session in php.ini. use_trans_sid = 0 or the --enable-trans-sid option was not turned on when compiling

Why is this happening? Let me explain below:

Session is stored on the server side (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 the Query_String of the Http1.1 protocol (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 id uniquely corresponding to it is generated. The session variable is defined 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 earlier. 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.

The 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 cookies, which is also the brilliance of session compared to cookies. 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 = 1 in php.ini or the --enable-trans-sid option is turned on during compilation".

Friends who have used forums know that when entering the forum, you will often be prompted to check whether cookies are turned on. This is because most forums are based on cookies, and the forum uses them to save usernames and passwords. and other user information for ease of use. And many friends think that cookies are not safe (in fact, they are not) and often disable them. In fact, in PHP programs, we can use SESSION instead of Cookie, which does not depend on whether the client turns on Cookie.

So, we can put aside the cookie and use the session, that is, assuming that the user closes the cookie, use the session. There are several ways to achieve this:

1. Set the session in php.ini .use_trans_sid = 1 or enable the --enable-trans-sid option when compiling to allow PHP to automatically pass session ids across pages.
2. Manually pass the value through the URL and pass the session id through the hidden form.
3. Save the session_id in a file, database, etc., and call it manually during the cross-page process.

An example of path 1:

s1.php

Copy code The code is as follows:

session_start();
$_SESSION['var1']="People's Republic of China";
$url="Next page";
echo $url;
?>

s2.php
Copy Code The code is as follows:

session_start();
echo "The value of the session variable var1 passed is: ".$_SESSION[' var1'];
?>

Run the above code. 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 can't get the result, then "set session.use_trans_sid = 1 in php.ini or turn on the --enable-trans-sid option when compiling", and you will get the result "People's Republic of China"

2 Example:

s1.php
Copy code The code is as follows:

session_start();
$_SESSION['var1']="People's Republic of China";
$sn = session_id();
$url="Next page";
echo $url;
?>

s2.php
Copy code The code is as follows:

session_id($_GET['s']);
session_start();
echo "The value of the session variable var1 passed is: ".$_SESSION['var1'];
?>

The basic principle of hiding the form is the same as above.

An example of path 3:

login.html
Copy code The code is as follows:




Login</title> ; <BR><meta. http-equiv="Content-Type" content="text/html; charset=gb2312"> <br></head> <br><body> <br>Please log in : <br><form. name="login" method="post" action="mylogin1.php"> <br>Username:<input type="text" name="name">< br> <br>Password:<input type="password" name="pass"><br> <br><input type="submit" value="Login"> <br></ form> <br></body> <br></html> <br> </div> <br>mylogin1.php <br><div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code2945')"><u>Copy code</u></span> The code is as follows: </div> <div class="codebody" id="code2945"> <br><?php <br><br>$name=$_POST['name']; <BR>$pass=$_POST['pass']; <BR> if(!$name || !$pass) { <BR>echo "The username or password is empty, please<a href="login.html">log in again</a>"; <br>die (); <br>} <br>if (!($name=="youngong" && $pass=="123") { <br>echo "The username or password is incorrect, please<a href=" login.html">Re-login</a>"; <br>die(); <br>} <br>//Registered user<br>ob_start(); <br>session_start(); <br> $_SESSION['user']= $name; <br>$psid=session_id(); <br>$fp=fopen("e:tmpphpsid.txt","w+"; <br>fwrite($fp,$ psid); <br>fclose($fp); <br>//Authentication successful, perform related operations<br>echo "Logged in<br>"; <br>echo "<a href="mylogin2. php">Next page</a>"; <br><br>?> <br> </div> <br>mylogin2.php <br><div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code29648')"><u>Copy code</u></span> The code is as follows: </div> <div class="codebody" id="code29648"> <br><?php <BR>$fp=fopen("e:tmpphpsid.txt","r"; <BR>$sid=fread($ fp,1024); <BR>fclose($fp); <BR>session_id($sid); <BR>session_start(); <BR>if(isset($_SESSION['user']) && $_SESSION[' user']="laogong" { <br><br>echo "Already logged in!"; <BR>} <BR>else { <BR>//Successfully logged in to perform related operations<BR>echo "Not logged in, no rights Visit"; <BR>echo "Please<a href="login.html">log in</a> and browse"; <br>die(); <br>} <br><br>?> ; <br> </div> <br> Please also turn off the cookie test, username: youngong Password: 123 This is to save the session id through a file, the file is: e: mpphpsid.txt, please decide the file name according to your own system or path. <br><br>As for the database method, I won’t give an example, it is similar to the file method. <br><br>To summarize, the above methods have one thing in common, which is to get the session id on the previous page, and then find a way to pass it to the next page. Add the code session_id (pass it) before the session_start(); code on the next page. coming session id); <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/320796.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/320796.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">I think the reasons for this problem are as follows: 1. Cookies are disabled on the client 2. The browser A problem occurred and the cookie cannot be accessed temporarily. 3. session.use_trans_sid =...</span> </div> in php.ini <div class="art_confoot"></div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>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</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="PHP string == comparison operator side effects_PHP tutorial" href="http://m.php.cn/faq/310287.html">PHP string == comparison operator side effects_PHP tutorial</a></span><span>Next article:<a class="dBlack" title="PHP string == comparison operator side effects_PHP tutorial" href="http://m.php.cn/faq/310289.html">PHP string == comparison operator side effects_PHP tutorial</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai" >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>