I have a login function on my website (using MySQL & PHP).
The problem I am having is that I am getting an error where I shouldn't be.
When the user logs in, I would like their username to be shown In the navbar using a variable I have called user_data
, however, when I try to run the code, I get the error:
Notice: Undefined variable: user_data in C:\xampp\htdocs\exampledirectory\includes\prefs\header.php on line 31.
Now, I have checked all my code, and It all seems to be correct... It just doesn't want to work!
I have the header.php
or navbar included into my index.php
like this:
INDEX.php:
<?php
require_once 'core/init.php'; <!-- notice the init file !-->
?>
<html>
<?php
include 'includes/prefs/header.php';
?>
<!-- body of html !-->
</html>
and this is my HEADER.php:
<li style="cursor:pointer;">
<?php
if(!logged_in()){
?>
<a>USER</a>
<ul>
<li><a href="./login">SIGN IN</a></li>
<li><a href="./register">REGISTER</a></li>
</ul>
<?php
}else{
?>
<a><?php echo $user_data['username']; ?></a> <!-- this is line 31 !-->
<ul>
<li><a href="./profile">PROFILE</a></li>
<li><a href="./settings">SETTINGS</a></li>
</ul>
<?php
}
?>
</li>
now, the user_data
variable comes into play once the user has logged in from a form on my login page which redirects all the data to another login page in a redirection folder
LOGIN.php:
<form action="./redir/login" method="post">
<input type="text" class="input-style" placeholder="Username" name="username"><br><br>
<input type="password" class="input-style" placeholder="Password" name="password"><br><br>
<input type="submit" value="Login"><br>
</form>
REDIR/LOGIN.php:
<?php
include 'core/init.php';
if (empty($_POST) === false){
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true) {
$errors[] = 'That user does not exist.';
} else if (user_exists($username) === false) {
$errors[] = 'That user does not exist.';
} else if (user_active($username) === false) {
$errors[] = 'This user is currently inactive. If you would like to know more, please click <a href="./help/9141320">here.';
} else {
$login = login($username, $password);
if ($login === false) {
$errors[] = 'The username or password you entered are incorrect.';
} else {
// query if credentials = true return (home)
$_SESSION['user_id'] = $login;
header('Location: ../index');
exit();
}
}
} else {
header('Location: index.php');
}
if (empty($errors) === false) {
?>
<!-- error html !-->
all the login data goes to my login function on my users.php
USERS.php:
function user_data($user_id) {
$data = array();
$user_id = (int)$user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields = '`' . implode('`, `', $func_get_args) . '`';
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"));
return $data;
}
}
function logged_in() {
return (isset($_SESSION['user_id'])) ? true : false;
}
/* exists */
function user_exists($username){
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
return (mysql_result($query, 0) == 1) ? true : false;
}
function email_exists($email){
$email = sanitize($email);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'");
return (mysql_result($query, 0) == 1) ? true : false;
}
/* active */
function user_active($username){
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1");
return (mysql_result($query, 0) == 1) ? true : false;
}
/* misc login */
function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT (`user_id`) FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}
function user_id_from_email($email) {
$email = sanitize($email);
return mysql_result(mysql_query("SELECT (`user_id`) FROM `users` WHERE `email` = '$email'"), 0, 'user_id');
}
function login($username, $password) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1) ? $user_id : false;
}
?>
and if the login details are correct it returns the user_id or if not it returns false.
and finally this is my INIT.php file:
<?php
session_start();
//error_reporting(0);
require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';
$current_file = explode('/', $_SERVER['SCRIPT_NAME']);
$current_file = end($current_file);
if (logged_in() === true) {
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id, 'username', 'password', 'email', 'first_name', 'last_name', 'CCNo', 'desc', 'avatar', 'type', 'group', 'active');
$errors = array();
?>
the INIT.php is what creates the user_data
variable from the user_data
function (if that makes sense).
I hope I have explained it well enough for people to understand and help me with.
The basic outline is: I want my user_data
variable function to work (so I can use it to echo out information).
Cheers