Reputation: 819
I'm new to PHP and I'm facing some problems when working with PHP sessions
Let's say I have a file (index2.php
) with this code in it.
<?php
session_start();
$_SESSION['name'] = 'The User';
?>
<a href="index3.php">Click</a>
And this is index3.php
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>
For some reason I don't understand, index3.php
doesn't show anything. What am I doing wrong?
Thanks!
Upvotes: 1
Views: 344
Reputation:
Make sure you also have session_start(); in all php pages where you want to retain and work with sessions;
make sure index3.php contains session_start();
Upvotes: 2
Reputation: 26066
In index3.php
you need to start the session as well. As per the official PHP docs:
When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.
Using your example, just initiate session_start()
as follows:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>
Upvotes: 4