Php session variables not saving


#1

I am trying to use session variables in php but they don’t seem to be saved across page loads. Maybe I am misunderstanding something, if so please point it out. To test I created these two pages:

page1.php

<?php session_start(); $_SESSION["key"]="value"; ?> key: <?=$_SESSION["key"]?>
page2

page2.php

key: <?=$_SESSION["key"]?>

The output of page1 is as expected, but when I click on the link to go to page2 I lose the session variable and the page just looks like "key: " Any assistance is appreciated, thanks.


#2

OK I figured it out. I guess I need to add session_start() at the beginning of page2 as well and then I have access to the variable on page1. My mistake!


#3

Also you should avoid using the short tag “<?” in your PHP scripts. Some default installations may not have them enabled, and they interfere with XML processing instructions.

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#4

You’ve got a few problems with that code - you might want to look at this post, which has a very simple example of string and reading session information.

–rlparker

Oops! Never mind … didn’t see the responses before I replied … sorry about that!