search

Home  >  Q&A  >  body text

"Using PHP session variables on second page"

<p>Hello. I'm trying to use session variables but can't seem to get it to work. I've attached my script. I started the session in both scripts. The first script Test.php looks like this</p> <pre class="brush:php;toolbar:false;"><?php // Start session session_start(); ?> <!DOCTYPE html> <html> <body> <?php //Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variable set. "; ?></pre> <p>The second script test1.php looks like this</p> <pre class="brush:php;toolbar:false;"><?php // Start session session_start(); ?> <!DOCTYPE html> <html> <body> <?php echo "My favorite color is" . $_SESSION["favcolor"] . ".<br>"; echo "My favorite animal is" . $_SESSION["favanimal"] . ". "; ?> </body> </html></pre> <p>When I run the first script I get the message that the variable has been set. But when I run the second script it doesn't show the variables. It's just blank. </p> <p>I would really appreciate any help</p>
P粉186904731P粉186904731455 days ago466

reply all(1)I'll reply

  • P粉458725040

    P粉4587250402023-09-04 10:39:55

    This PHP code works fine for me. There may be something wrong with your environment settings. You can check the settings in your .ini file, or your browser settings, to ensure that the browser stores and sends the session ID cookie on every request. Without this cookie, the server does not know which session to start.

    Try running a file with phpinfo() and search for all settings under session. This may point to a problem with the .ini file. You can also open your browser's developer tools and view your site's Applications tab. There should be a cookie stored on your site associated with the session name, probably PHPSESSID.

    reply
    0
  • Cancelreply