I have a my navbar script on a file called navbar.php. I include this file at the top of all other pages of my website. What I am trying to do now is to customize the navbar with the name of whoever is logged in. Let's say I have a php file named account-process.php with a variable $name = 'Bob'
. I'm having trouble making this variable show up in the navbar.
Here's my account-process.php:
<?PHP
//VARS: gets user input from previous sign in page and assigns it to local variables
//$_POST['signin-email'];
//$_POST['signin-pass'];
$signin_email = $_POST['signin-email'];
$signin_pass = $_POST['signin-pass'];
// connects to MySQL database
include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
// checking for user input in the table
$result = mysql_query("SELECT * FROM table WHERE email = '$signin_email' AND password = '$signin_pass'");
if (mysql_num_rows($result) > 0) { // if the username and password exist and match in the table
echo "Account Found";
$account_arr = mysql_fetch_array($result); // contains all the data from the user's MySQL row
echo print_r($account_arr);
}
else { // if username and password don't match or they aren't found in the table
echo "The email or password you entered is incorrect.";
}
?>
I'm trying to access the $account_arr
variable in navbar.php so I can display the user's name on the top. I have tried including account-process.php in navbar.php with <?php include('account-process.php') ?>
and then accessing the variable in the html of the navbar, but the page simply turns out blank when I attempt this.
navbar.php simply has basic scripting for a fixed navbar with some information. Why does it turn blank when I try to include the php file in it?
Thanks
Change the navbar a PHP file and use sessions. -EDIT- Took a long time to post. Keep it as PHP.
account-process.php:
<?php
session_start();
//VARS: gets user input from previous sign in page and assigns it to local variables
//$_POST['signin-email'];
//$_POST['signin-pass'];
$signin_email = $_POST['signin-email'];
$signin_pass = $_POST['signin-pass'];
// connects to MySQL database
include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
// checking for user input in the table
$result = mysql_query("SELECT * FROM table WHERE email = '$signin_email' AND password = '$signin_pass'");
if (mysql_num_rows($result) > 0) { // if the username and password exist and match in the table
echo "Account Found";
$account_arr = mysql_fetch_array($result); // contains all the data from the user's MySQL row
echo print_r($account_arr);
$_SESSION['name']=$account_arr['username'];
}
else { // if username and password don't match or they aren't found in the table
echo "The email or password you entered is incorrect.";
}
?>
navbar.php:
<?php
session_start();
echo "<div><p>Hello, my name is " . $_SESSION['name'] . ".</p></div>";
?>
The session data is stored in a cookie called PHPSESSID
that expires after the browsing session ends.
You start or resume a session using the session_start()
function. This must be called before the <!DOCTYPE html>
if the page contains non-PHP generated HTML.
The data is stored in a superglobal associative array called $_SESSION
. Info can be sent to/from and modified from this variable on any page with session_start called.
If you don't want to use a session you can create your own cookie and use the $_COOKIE
superglobal.
Further Info:
http://php.net/manual/en/function.session-start.php