“标头"的行为在if陈述中 [英] Behavior of "header" in if-statement

查看:51
本文介绍了“标头"的行为在if陈述中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于检查访问权限的代码.

This is my code for checking access.

$query = "SELECT user_table.status, expire FROM user_table WHERE username = ?";

if($stmt = $mysqli->prepare($query)){
    $username = phpCAS::getAttribute('uid');
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $stmt->store_result();
    $returned_amount = $stmt->num_rows;


    if($returned_amount>1)
        die("To many user names exists for you!");
    else if(empty($returned_amount))
        header("Location: /101/index.php?type=nouser");


    $stmt->bind_result($status, $expire);
    $stmt->fetch();
    $stmt->free_result();
    $stmt->close();

    if($expire != '0000-00-00 00:00:00' && strtotime($expire) <= time())
        header('Location: /101/index.php?type=expired');

    $access = $status;

}else die("Failed to prepare!");

?>

但是,当 $ returned_amount == 0 时.

它没有命中 header("Location:/101/index.php?type=nouser");

如果将代码更改为以下代码,则可以解决此问题,但是我看不出为什么更改它会有所帮助.

If I change the code to the following, it fixes the problem, but I don't see why changing it would help.

if($returned_amount>1)
    die("To many user names exists for you!");
else if(empty($returned_amount)){
    header("Location: /101/index.php?type=nouser");
    exit();
}

如果我删除 exit(); ,则标头将不会执行.

If I remove the exit();, the header won't be executed.

推荐答案

仅使用 header()不会 表示代码停止执行.每当使用 header()重定向时,您需要显式调用 exit()来停止脚本的执行.

Just using header() does not mean the code stops executing. Whenever using header() to redirect you need to explicitly call exit() to stop execution of the script.

这篇关于“标头"的行为在if陈述中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆