search
HomeBackend DevelopmentPHP ProblemWhat should I do if php cannot pass session?

Solution to the problem that php cannot pass session: 1. Enable cookies in the client; 2. Check browser issues and access cookies; 3. Enable session.use_trans_sid in php.ini.

What should I do if php cannot pass session?

##The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Solution to the problem that SESSION cannot be passed across pages in PHP

Friends who have used SESSION in PHP may encounter such a problem, the SESSION variable cannot be passed across pages. This troubled me for several days, and I finally thought about and solved the problem by looking up information. 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. The session in php.ini. use_trans_sid = 0 or the --enable-trans-sid option was not turned on during compilation

Why is this happening? Let me explain below:

Session is stored on the server side (session is stored as a file by default). According to the session id provided by the client, the user's file is obtained 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".

Understanding the above principles, now let’s put aside cookies and use sessions. There are three main ways:

1. Set session.use_trans_sid = 1 in php.ini or open it during compilation. The --enable-trans-sid option is added 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 session_id in a file, database, etc., and call it manually during the cross-page process.

Let’s illustrate with an example:

s1.phps2.php

## php

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


php
session_start ();                                             echo ​ " The value of the passed session variable var1 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"


This This is the approach 1 mentioned above.

Let’s talk about approach 2:

The modified code is as follows:

s1.php


php

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


s2.php method 3: login.html

  php
  session_id   (   $_GET   [   '   s   '   ]);                              
  session_start   ();                                     
  echo       "   传递的session变量var1的值为:   "   .   $_SESSION   [   '   var1   '   ]; 
  ?>  

 

       
                

      Login     title   >                                                   
   
    head   >                                                                
     请登录:                                                 
 

   action   =   "   mylogin1.php   "   >                                                 
用户名   :                              
口 令   :                          
                                       
    form   >                                                                
    body   >                                                                
    html   >      

 

mylogin1.php

<?   php

   $name   =   $_POST   [   &#39;   name   &#39;   ];
   $pass   =   $_POST   [   &#39;   pass   &#39;   ];
   if   (   !   $name       ||       !   $pass   ) {
   echo       "   用户名或密码为空,请<a href="login.html">重新登录</a>   "   ;
   die   ();
}
   if    (   !   (   $name   ==   "   laogong   "       &&       $pass   ==   "   123   "   )) {
   echo       "   用户名或密码不正确,请<a href="login.html">重新登录</a>   "   ;
   die   ();
}
   //   注册用户  
   ob_start   ();
   session_start   ();
   $_SESSION   [   &#39;   user   &#39;   ]   =       $name   ;
   $psid   =   session_id   ();
   $fp   =   fopen   (   "   e:/tmp/phpsid.txt   "   ,   "   w+   "   );
   fwrite   (   $fp   ,   $psid   );
   fclose   (   $fp   );
   //   身份验证成功,进行相关操作  
   echo       "   已登录<br>   "   ;
   echo       "   <a href="mylogin2.php">下一页</a>   "   ;

   ?>

mylogin2.php

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 " ) {
echo ​ " Has logged! " ;
}
else {
// Successfully log in to perform related operations
echo ​ " Not logged in, no access rights " ;
echo ​ " Pleaselog in to browse " ;
Die ();
}

?>

Please also turn off the cookie test. Username: laogong Password: 123 This is to save the session id through a file. The file is: e:/tmp/phpsid.txt. Please decide the file name or path according to your own system. .

As for the database method, I will not give an example, it is similar to the file method.

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. Session id that came over);

Recommended study: "PHP Video Tutorial"

The above is the detailed content of What should I do if php cannot pass session?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.