Hi,
I have created Signup and Login systems for my gallery website. When a user tries to login into system, their user and password and admin privilege is checked. If it was successful, the username is appeared on top left corner of the home page and Login turns to Logout. The problem is that when I refresh the page, the user is logged out.
Login code:
<?php
session_start();
class User
{
public function CheckUser()
{
require "../app/core/database.php";
if (isset($_POST['username']) && isset($_POST['pass'])) {
$username = $_POST['username'];
$password = $_POST['pass'];
//to prevent sql injection
$username = stripcslashes($username);
$password = stripcslashes($password);
$username = mysqli_real_escape_string($connection, $username);
$password = mysqli_real_escape_string($connection, $password);
$sql = "SELECT * FROM signup WHERE username = '$username' and password = '$password'";
$sql2 = "SELECT admin FROM signup WHERE username = '$username' and password = '$password' limit 1";
$log_result = mysqli_query($connection, $sql);
$count = mysqli_num_rows($log_result);
if ($count == 1) {
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
$_SESSION['is_admin'] = mysqli_query($connection, $sql2);
header("Location: ../home/index");
} else {
echo "<script>Invalid()</script>";
}
}
}
}
?>
<script>
function Invalid() {
alert("Invalid user/password");
}
</script>
part of home page code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
include "../app/model/loadImages.php";
include "../app/core/config.php";
include "../app/model/login.php";
?>
I did not used $_SESSION in the index.php because it has been included from Login.php
How can I fix this issue and avoid unwanted session end?