重定向到登录作为会话检查的一部分

时间:2018-10-09 20:25:59

标签: php

在我生命中无法获得标头重定向或java重定向在此lock.php中工作

<?php
include 'config.php' ;
session_start();
$user_check=$_SESSION['login_user'];

$ses_sql=mysqli_query($db,"select username from users where username='$user_check' ");

$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC); //confirm username from users table

$login_session=$row['username'];

if ($login_session ==""){
    // echo "the variable is empty"; //when i check this works here.
    //header ("Location: login.php"); //redirect to login -tried this, fail
    echo "<script> location.href='login.php'; </script>"; // tried this, fail
    exit(); //stop execution of any further script
}

?>

1 个答案:

答案 0 :(得分:1)

我建议:

if (empty($login_session)) { // You're checking for any empty/null value
    header("Location: login.php");
    exit; // Per php docs, header does not halt execution
}

您的代码有问题。

  • 检查值是否为空白与检查值是否为“ null”或否则未分配相同。