如果缺少某些变量,则重定向会产生一个错误,指出该变量丢失

redirection if some variable is missing creates an error saying the variable is missing

如果 url 中没有 id 我想从数据库中获取 id 然后使用 id[=17= 重定向到同一页面]

if(!isset($_GET['id'])){
    $st = $db->query("select id from video where ind = 0 limit 1");
    $st->execute();
    $id = $st->fetchColumn();
    header('location: index.php?id='.$id);
}

$urid = $_GET['id']; // line 15

一切正常,页面被重定向并且 id 在那里,但系统创建了一个 error.log 文件说:

Undefined array key "id" ... on line 15

那么为什么系统会产生这个错误
重定向应该发生在 line 15

之前

重定向旁边应该有 exit;

<?php
    header("Location: http://www.example.com/"); /* Redirect browser */
    
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    ?>