tags:

views:

47

answers:

3

I have the following pages:

page1.php

<?php 
if (isset($_GET['link'])) { 
session_start(); 
$_session['myvariable'] = 'Hello World'; 
header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php'); 
exit; 
} 
?> 
<a href="<?php print $_SERVER['REQUEST_URI'] . '?link=yes';?>">Click Here</a>

page2.php

<?php 
print 'Here is page two, and my session variable: '; 
session_start(); 
print $_session['myvariable']; //This line could not output.
exit; 
?>

When I try output $_session['myvariable'] I did not get the result hello world message.

I could not find out the solution to fix it?

+4  A: 

$_SESSION not $_session. Uppercase.

error_reporting(E_ALL); at the top of the script always helps in such case

Col. Shrapnel
Unlike function names, variable names are case-sensible in PHP.
Álvaro G. Vicario
+1  A: 

session_start() has to be called before you send any output as it relies upon cookies to store the ID.

Also $_SESSION is uppercase

Neil Aitken
Although in this very case it isn't an issue
Col. Shrapnel
In page2 he is printing before calling session_start. the incorrect casing of $_SESSION is probably the main issue here, but this could still be affecting him.
Neil Aitken
Affecting what? what would prevent from reading session value?
Col. Shrapnel
+1  A: 
<?php 
session_start(); 
echo $_SESSION['myvariable'];

echo 'Here is page two, and my session variable: '; 
exit;
?>

HTTP headers must be the very first output, so session_start() must be at the top of your code.

Other notes: * $_SESSION should be uppercase. * Echo > print

Coronatus
What do you mean "Echo > print"?
Col. Shrapnel