简体   繁体   中英

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. 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.

I'm running PHP in IIS 8.5 on Server 2012 R2.

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

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.

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.

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