retaining session variable in php
i want开发者_开发知识库 to retain the session variable while browsing from one page to the other could some one help me that i could i achieve that?? thanks
Ok in the most basic fashion
<?php
//index.php
session_start();
$_SESSION['name'] = "Fred";
header("Location:displayname.php");
?>
<?php
//displayname.php
session_start();
echo $_SESSION['name'];
?>
Its made for it. I think you are missing the session_start()
part of your script.
Then you can register a var as following: $_SESSION['var_name'] = 'value';
and you are done, if the session doesn't expires, that variable will be available everywhere on your domain.
Remember that session, by default, expires after 24 minutes of inactivity.
Basically the code should look like this:
session_start();
$_SESSION['var'] = 'var';
and you should be able to get the variable in another page:
$var = $_SESSION['var'];
Here it is. Just don't forget the session_start()
at the top of each page where you need to use sessions.
This way,
<?php
session_start();
$_SESSION['views'] = 1;
echo "Pageviews = ". $_SESSION['views'];
?>
The data in _SESSION
will be persisted across multiple pages until you invoke session_destroy() function.
精彩评论