I'm trying to set up a really basic login system where the user logs in, but I want to set up some pages in the site so that there's a redirect to the login page if someone tries to access a protected page without being logged in.
So, this is how I'm checking the username and password:
And at the top of the protected pages, I've added this to check for session details:
But when I try to login, I just get redirected back to the login page. It seems like the authentication isn't communicating with the $_SESSION. Where am I going wrong here?
So, this is how I'm checking the username and password:
Code:
<?php
session_start();
include ('includes/DbCon.php');
// username and password sent from form
$user=$mysqli->real_escape_string($_POST['user']);
$password=$mysqli->real_escape_string($_POST['password']);
$sql="SELECT * FROM xyz WHERE user='$user' and password='$password'";
$result=$mysqli->query($sql);
// Mysqli_num_rows is counting table rows
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
// Register $user, $password and redirect to admin area
$_SESSION['user']=="admin";
$_SESSION['password']=="password";
echo '<script type="text/javascript">';
echo 'document.location.href = "/pc.v.2/admin.php";';
echo '</script>';
}
else {
echo '<script type="text/javascript">';
echo 'alert("Invalid Username or Password");';
echo 'history.back();';
echo '</script>';
}
mysqli_close($mysqli);
?>
Code:
<?php
session_start();
if(!isset($_SERVER['PHP_AUTH_USER'])) {
header("location:login.php");
}
else{
echo '<script type="text/javascript">document.getElementsByClassName("login").innerHTML="Logout";</script>';
echo "</script>";
}
?>
Comment