简体   繁体   English

PHP中的会话共享

[英]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和child.php
  2. parent.php sets a session containing highly-secretive information parent.php设置一个包含高度机密信息的会话
  3. parent.php then calls child.php via Ajax then process whatever information is received parent.php然后通过Ajax调用child.php,然后处理接收到的所有信息

The enigma: child.php needs to have the same session state as parent.php in order to give the information parent.php requested. 谜题 child.php必须具有与parent.php相同的会话状态,以便提供所请求的信息parent.php。

Is this possible? 这可能吗?

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

Sessions are usually assigned through cookies being set and recognized. 通常通过设置和识别cookie来分配会话。 GET variables are also used sometimes, but is considered an elevated security threat, as the GET variable is visible in the URL. 有时也使用GET变量,但是由于GET变量在URL中可见,因此被认为是较高的安全威胁。

PHP can be set to a certain level of strictness when it comes to when to re-use an existing session. 关于何时重新使用现有会话,可以将PHP设置为一定程度的严格性。 You can, for example, fiddle with: 例如,您可以摆弄:

  • cookie lifetime Cookie寿命
  • cookie domain settings Cookie域设置
  • cookie path Cookie路径
  • if the IP needs to be the same every time 如果每次IP需要相同
  • 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. 当满足所有条件时,PHP将在调用session_start()时自动重用现有会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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