天天看點

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