天天看点

PHP中session(报错Notice: Undefined index)无法获取值的原因(原因之一)与分析

PHP中的SESSION问题:

本文主题: 在页面之间传递session时,两页面文件不在同文件夹中而导致session无法传递,报错Notice: Undefined index…

此问题测试:

  • 文件结构:

    test文件夹:

    —gameIndex.php

    —out.php

    —php文件夹:in.php

    如图:

  • PHP中session(报错Notice: Undefined index)无法获取值的原因(原因之一)与分析
    PHP中session(报错Notice: Undefined index)无法获取值的原因(原因之一)与分析
  • 文件内容:

gameIndex.php:

<?php
  session_start();
  echo $_SESSION['userid'];
?>      

out.php:

<?php
  session_start();
  $_SESSION['userid'] = 4;
  echo "<script>window.location.href = 'gameIndex.php'</script>";
?>      

in.php:

<?php
  session_start();
  $_SESSION['userid'] = 4;
  echo "<script>window.location.href = '../gameIndex.php'</script>";
?>      
  • 测试:

1.浏览器访问:http://localhost:8081/dashboard/test/php/in.php

PHP中session(报错Notice: Undefined index)无法获取值的原因(原因之一)与分析

2.浏览器访问:http://localhost:8081/dashboard/test/out.php