Redirect if not logged in

ساخت وبلاگ
I am using the following PHP to validate a login and set a session variable (named id) to the returned ID value that was queried:
PHP Code:

if(password_verify($_POST['password'], $row['password'])) {
  
// Assign the session [id] to the username's id
  
$_SESSION['id'] = $row['id'];
  echo 
'{"valid": true}';
} else {
  
// Return a JSON object to indicate the invalid login
  
echo '{"valid": false}';


Then in the page that gets redirected to if the login is a success through the login validations AJAX request, I'm using the following inline PHP code prior to the <body> tag to check if the session variable is not set and if it isn't then redirect the user to the login page:
PHP Code:

<?php
  
if(!isset($_SESSION['id'])) header("Location: ../../index.php");;
?>

However, what is happening now is for whatever reason, the page is always redirecting the user back to the index.php page even if the user just successfully logged in. Why would this happen?
CodingForums...
ما را در سایت CodingForums دنبال می کنید

برچسب : نویسنده : codingforums بازدید : 197 تاريخ : پنجشنبه 30 شهريور 1396 ساعت: 17:45