1

I am trying to pass PHP session variables from page to page. I am receiving an undefined index error. I am using PHP 8.12 on apache 2.48 on localhost.

I have tried with a couple of very simple code snippets, and I am getting the same error, for example:

page1.php:

<?php
    session_start();
    $_SESSION["name"]="xxx";
    header ("location:page2.php");
    exit;
?>

If I echo $_SESSION["name"] on page1.php, the correct value is displayed.

page2.php:

<?php
    session_start();
    echo $_SESSION["name"];
?>

On page 2 I am receiving an undefined index "name" error.

I have a feeling there maybe something I am missing in the php.ini file as I can't see any reason why such simple code should be failing.

veryreverie
  • 2,871
  • 2
  • 13
  • 26
  • `session_start()` returns a boolean value. Be sure it's not `false`. – user1597430 Aug 21 '22 at 05:59
  • Make sure you have not disabled session . See [link](https://stackoverflow.com/questions/32356373/how-to-disable-session-in-php) – Ken Lee Aug 21 '22 at 06:03
  • A few randoms ideas: 1) Your browser doesn't store cookies; 2) you edit a wrong file; 3) you doesn't save your file changes; 4) you made a typo in your `$_SESSION` variable; 5) your server is misconfigured and wipes session files or can't write files (check your `session_save_path()`); 5) your HDD/SSD is full // Also, your `Location` word needs a capital `L` char and you need a space after the `:` char. – user1597430 Aug 21 '22 at 06:09
  • 6) check the path of the session cookies in your browser if page 2 is in a different dir. [See this](https://stackoverflow.com/a/9885543/1685196). – Michel Aug 21 '22 at 06:18
  • But seriously, fix your header syntax first and try again. Here's your brother that made same typos in HTTP headers: https://stackoverflow.com/a/6777970/1597430 – user1597430 Aug 21 '22 at 06:18
  • 7) you mix HTTP/HTTPS; 8) you set `session.cookie_secure = On`, but use HTTP; 9) the code you show us here is not the code you actually use; 10) you set invalid cookies - check you browser's Console output, all cookie policy violation warnings should be there; 11) you have `.htaccess` in the same dir or global Apache settings that mess with your cookies. – user1597430 Aug 21 '22 at 06:26
  • 1
    I have checked through the 11 points to no avail... as an fyi there is no need for location in the header to be Location and there is no need for a space after the : the page redirects without issue, changing header() as suggested makes no difference the $_SESSION variables still don't get passed – TheOtherDave Aug 21 '22 at 07:11
  • Check dev tools and inspect the cookie. When does it expire? does it change value between 2 pages? does it exist? – IT goldman Aug 21 '22 at 20:12
  • It works for me. Are you sure that the files you posted are what you are using ? It looks like your Sessions are disabled. Have a look at the answer here https://stackoverflow.com/questions/42596339/php-session-disabled-with-enabled-session-support – Rohit Gupta Aug 29 '22 at 14:57

0 Answers0