简体   繁体   中英

Session sharing in PHP

Consider the following scenario:

  1. There are 2 pages in a server, namely: parent.php and child.php
  2. parent.php sets a session containing highly-secretive information
  3. parent.php then calls child.php via Ajax then process whatever information is received

The enigma: child.php needs to have the same session state as parent.php in order to give the information parent.php requested.

Is this possible?

只要您在每个脚本的顶部调用session_start() ,两个脚本都在同一个域中,并且会话cookie并不局限于一个子目录,则在第一个请求中在$_SESSION设置的变量仍将存在于第二。

Sessions are usually assigned through cookies being set and recognized. GET variables are also used sometimes, but is considered an elevated security threat, as the GET variable is visible in the URL.

PHP can be set to a certain level of strictness when it comes to when to re-use an existing session. You can, for example, fiddle with:

  • cookie lifetime
  • cookie domain settings
  • cookie path
  • if the IP needs to be the same every time
  • if the user agent needs to be the same every time

The default settings are not very strict.

When all conditions are met, PHP will automatically re-use the existing session when session_start() is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM