How can I access my session variable in a different page with php?
Solution 1
Make sure that you use
session_start();
In the start of every page, or any PHP file that needs to have access to the session.
The easiest way to do this, is have something like a header.php file and include/require this at the top of every page of your site or common pages.
In this header.php you would have something like
<?php
session_start();
if (isset($_SESSION['username'])) {
// This session already exists, should already contain data
echo "User ID:", $_SESSION['id'], "<br />"
} else {
// New PHP Session / Should Only Be Run Once/Rarely/Login/Logout
$_SESSION['username'] = "yourloginprocesshere";
$_SESSION['id'] = 444;
}
?>
The simply have your page like this
<?php require "header.php"; ?>
<!doctype html>
<head></head>
<body>
<?php
if (isset($_SESSION["username"])) {
$loggenOnUser = $_SESSION["username"];
echo "Found User: ", $loggenOnUser, "<br />"
} else {
$loggenOnUser = " a public user";
}
?>
<div class="gridContainer clearfix">
<div id="div1" class="fluid">
This page is being called by my login.php file.
</div>
<div id="LoggedInUser" class="fluid ">
Hi. I'm <?php echo $loggenOnUser; ?>
</div>
<img id="homeImage" src="images/home.gif" /> </div>
</div>
</body>
</html>
Solution 2
*edit
- You forget
echo
-
You need to start session before accessing session variables
put
session_start()
on top of your page put echoHi. I'm <?php echo $loggenOnUser; ?>
Solution 3
Check this site: http://php.net/manual/en/function.session-start.php
You need to write session_start()
in all your php files, best at the top.
Solution 4
You should put session_start()
before you use session array.
Also, instead of <?php $loggenOnUser?>
try <?=$loggenOnUser?>
.
Solution 5
If you have done echo and session start, which absolutely need to be done, and still nothing, then looks like you have 'nothing' in the $found_admin array against that user name
$_SESSION["username"] = $found_admin["username"];
Are you sure there is something in that array? Can you print the $found_admin array?
Related videos on Youtube
Comments
-
CloudyKooper about 4 years
everyone I started the session variable at the top of the login.php page. the session is then set and I call the second page using the include statement. I tested with an if statement and it seemed to pass ok. then I try to echo the contents of the variable in the newly displayed page. The page displays meaning that it passes the if block but the contents of the session variable does not show. It's as if the session variable has gone out of scope. How can I access my session variable in a different page.
<?php session_start(); $username = ""; if (isset($_POST['submit'])) { $username = $_POST["username"]; $password = $_POST["password"]; $_SESSION["username"] = $found_admin["username"]; if (isset($_SESSION["username"])); { redirect_to("index.php"); } else { $_SESSION["message"] = "Username/password not found."; } } ?> <?php include("login.html"); ?>
Here's the html page that's called by my php file:
<!doctype html> <head> </head> <body> <?php echo $_SESSION["username"]; $loggenOnUser=$_SESSION["username"]; ?> <div class="gridContainer clearfix"> <div id="div1" class="fluid"> This page is being called by my login.php file.</div><div id="LoggedInUser" class="fluid ">Hi. I'm <?php $loggenOnUser?> </div> <img id="homeImage" src="images/home.gif" /> </div> </div> </body> </html>
Can't seem to get why I can't get access to the session variable on another page. Any help would be greatly appreciated. Thanks!
-
Kamil Karkus over 9 yearsmissing
session_start()
in second file and<?php echo $loggedOnUser; ?>
this is how it should look like
-
-
CloudyKooper over 9 yearsAdded the session_start and <?php echo $loggedInUser?> but still does not display anywhere on the new page.
-
CloudyKooper over 9 yearsI started the session on the first page. I thought it only needed to be started once where ever it's initiated right?
-
banana_Ai over 9 yearsNop. You should start it on every page. It is not like really "start", but more like "create new or load".
-
Angry 84 over 9 yearsYour welcome ;) just a hint.. with the require "header.php"... expand that to multiple includes/requires... sessions.php/database.php/user.php/content.php or which ever.. its a handy use with php.