“标题”的行为在if语句中 [英] Behavior of "header" in if-statement

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

问题描述

  $ 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 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();
$ b $ if($ expire!='0000-00-00 00:00:00'&& strtotime($ expire)< = time())
header('Location :/101/index.php?type=expired');

$ access = $ status;

} else else(Failed to prepare!);

?>

然而当 $ returned_amount == 0 时。

不会打到标题(位置:/101/index.php?type=nouser\");



如果我将代码更改为以下代码,它会修复问题,但我不明白为什么更改它会有帮助。


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

$ / code>

如果我删除 exit(); ,头文件不会被执行。

解决方案

使用 header() does not 意味着代码停止执行。每当使用 header()重定向时,您需要显式调用 exit()来停止脚本的执行。 p>

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!");

?>

However when $returned_amount == 0.

it doesn't hit 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();
}

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

解决方案

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天全站免登陆