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!
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>
*edit
echo
You need to start session before accessing session variables
put session_start()
on top of your page
put echo Hi. I'm <?php echo $loggenOnUser; ?>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With