-2

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

applemavs
  • 503
  • 3
  • 9
  • 22

1 Answers1

1

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

http://www.w3schools.com/php/php_sessions.asp

http://www.w3schools.com/php/php_cookies.asp

Homberto
  • 165
  • 1
  • 4
  • 14
  • *"The session data is stored in a cookie called PHPSESSID that expires after the browsing session ends."* only some what correct, the session id is in the in the cookie (or the url) the session data is on the server –  Aug 31 '14 at 03:05
  • *"You start or resume a session using the session_start() function. This must be called before the if the page contains non-PHP generated HTML."* again not very accurate, it simply needs to be called before any output –  Aug 31 '14 at 03:06
  • Thank you for the in-depth response, I'm going to try this out and see how it works. – applemavs Aug 31 '14 at 03:19
  • It looks like it worked! Thank you so much. I should probably ask this in a separate question, but just incase there is a quick solution: is there a way to modify the navbar based on if the user is logged in? (make certain elements be replaced by other elements) For example, I want my "Log In" button to be replaced by the user's name. – applemavs Aug 31 '14 at 04:20
  • applemavs no problem. @Dragon, thanks for correcting that. ;) – Homberto Aug 31 '14 at 05:20