简体   繁体   English

PHP会话变量和身份验证

[英]PHP Sessions Variables & Authentication

I have two pages which I need to pass session information between them. 我有两个页面,我需要在两个页面之间传递会话信息。 Here's page one. 这是第一页。

<?php
session_start();
echo session_id()."<br>";
echo $_SESSION['test']."<br>";

Page two. 第二页。

<?php
session_start();
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];

Page two is in a different directory (same domain), and has Windows Authentication on. 第二页位于不同的目录(相同的域)中,并且启用Windows身份验证。 Page one is using anonymous authentication. 第一页正在使用匿名身份验证。 From the output I can see the session ID is the same, but page one doesn't echo the test variable set from page two. 从输出中可以看到会话ID是相同的,但是第一页没有回显第二页中的测试变量集。

I'm running PHP in IIS 8.5 on Server 2012 R2. 我在Server 2012 R2的IIS 8.5中运行PHP。

Any help is appreciated. 任何帮助表示赞赏。

To clarify, I am calling page two first, and page one will not show the variable. 为了澄清,我先调用第二页,而第一页将不显示该变量。

You need to initialize the variable with a value first before using it. 您需要先使用值初始化变量,然后再使用它。

First run page2.php so, that value of $_SESSION['test'] is set to test 
using $_SESSION['test'] = 'test';
Now, run page1.php there you can see the value of $_SESSION['test'] 
using echo $_SESSION['test']."<br>";

you can also follow these steps to make use of session in a simple way so that you don't have to do session_start() again and again 您还可以按照以下步骤以简单的方式使用会话,从而不必一次又一次地使用session_start()

1. config.php having session_start();
2. page1 having include_once('config.php');
   echo session_id()."<br>";
   echo $_SESSION['test']."<br>";
3. page2.php having include_once('config.php');
   echo session_id()."<br>";
   $_SESSION['test'] = 'test';
   echo $_SESSION['test'];

Try closing your session in the first page, it's possible that your session file is still open when the second page is called, so you're not seeing the data. 尝试在第一页中关闭会话,调用第二页时,您的会话文件可能仍处于打开状态,因此您看不到数据。 Try adding session_write_close() to the first page called. 尝试将session_write_close()添加到所调用的第一页。

http://php.net/manual/en/function.session-write-close.php http://php.net/manual/en/function.session-write-close.php

The issue was with permissions on the session save path. 问题出在会话保存路径上。 I changed the path to where both directories could write and it works. 我将路径更改为两个目录都可以写入且可以正常工作的位置。

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

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