Hi folks,
Going through this tutorial on Youtube - http://www.youtube.com/watch?v=OY5OxqMI3WA&list=ECE134D877783367C7
Is my database, there is a field called user_active that will have a 1 is the user is active, 0 is not active. As seen in the video, we are using an existing function (see below) to check if a user is active while browsing the site, and if the user isn't active..logging them out, and bring them back to the login page.
My problem is that when I implement this code in my init.php file, and hit F5, it logs my legit user account (which is active, is set to 1 in the database) out when it shouldn't, it should keep me logged in.
Here is the user_active function: (which is located in another file called user.php)
function user_active( $username,mysqli $DB ){
# sanitize $username (to avoid SQL injection attacks/errors)
$username = $DB->real_escape_string( $username );
# this is the SQL query we'll use.
$sql = "SELECT 1 FROM `users` WHERE `username`='$username' AND `active` = 1";
# execute the query.
$result = $DB->query( $sql );
# check if there were any rows in the result (if there were no rows, the username does not exist).
return (bool)$result->num_rows;
Here is where I call the function to check if the user is logged when they are browsing the site. This is located in init.php
session_start();
require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';
//we are calling the user_active function to see if the user is logged in.
if(user_active($username,$DB ) === false) {
session_destroy();
header('Location: index.php');
exit();
}
Any ideas?
Many thanks,