将页面转移到新页面 URL 如果不正确 URL 参数是手动添加的 - PHP

Divert A Page To A New URL If Incorrect URL Parameter is manually Added - PHP

我有一个删除页面,显示来自 MySQL 数据库的用户记录。

删除功能是通过 PHP 准备好的语句完成的,执行此语句后,用户将被定向回 index.php(显示所有用户的 table)

在下面代码中处理记录删除的 $_POST 请求上方,我有一个 if 语句检查 'id' url 参数,如果它不存在它会将您送回 index.php。我已经这样设置了,所以如果有人将 id=45 url 参数更改为 delete=45 出于安全原因,它会将他们从页面发送出去。

有什么方法可以让用户输入不同的 url 参数值(数据库中不存在),例如 id=49,它会将用户发送到使用添加了位置字符串的 header() 方法的错误页面/不同页面?

我已经尝试了 header("Location: " . $_SERVER["HTTP_REFERER"]);,我认为这可以有效地将页面重新生成到更改 url 参数之前的状态,但我收到错误消息 Undefined index: HTTP_REFERER

如有任何帮助,我们将不胜感激。

注意:代码下方delete.php页面的截图有link。

安娜

<?php 

    if (isset($_GET['id'])) {

        $the_id = $_GET['id'];


    } else {

        // BELOW IS THE ORIGINAL RE-ROUTING TO INDEX.PHP AND THE OTHER IDEA I HAD
        // header("Location: index.php");
        // header("Location: " . $_SERVER["HTTP_REFERER"]);

        exit;

    }

?>

<?php

    if (isset($_POST['submit'])) {

        $stmt = $connection->prepare("DELETE FROM users WHERE ID = ? ");

        $stmt->bind_param('s', $the_id);

        $stmt->execute();

        $stmt->close();
        
        $connection->close();
        
        header("Location: index.php");
        exit;

    } 
?>

下面截图中的 table 是如何创建的。

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>firstname</th>
            <th>email</th>
        </tr>
    </thead>
    <tbody>
    <?php 

        $query = "SELECT * FROM users WHERE ID = $the_id";
        $select_posts = mysqli_query($connection, $query);

        while ( $row = mysqli_fetch_assoc($select_posts) ) {

            $user_id = htmlspecialchars($row['ID']);
            $firstname = htmlspecialchars($row['firstname']);
            $email = htmlspecialchars($row['email']);

            echo "<tr>";
            echo "<td>$user_id</td>";
            echo "<td>$firstname</td>";
            echo "<td>$email</td>";
            echo "</tr>";
        }
    ?>
    </tbody>
</table>

Screenshot of delete page

!php 中的第一个方法 在 PHP 中,您可以像这样在 header() 语句中使用 $_SERVER["HTTP_REFERER"] 变量。

header('Location: ' . $_SERVER["HTTP_REFERER"] );
exit;

在 header() 语句后跟一个退出或代码执行的好主意将照常继续执行脚本的其余部分。

第二种方法 javscript

<button onclick="goBack()">Go Back</button>
function goBack() {
window.history.back();
}

希望您能得到问题的答案。

首先解决重定向问题:

如果浏览器不提供“Referer”header,您的重定向将失败并出现“未定义索引:HTTP_REFERER”错误。

你可以做的是:

  1. 检查header是否可用(使用isset());
  2. 如果存在就使用它;
  3. 在另一种情况下使用默认错误 link。

如果您正在使用 PHP 现代版本的 PHP,我希望您是,您可以使用 ?? 运算符:

<?php 

    if (isset($_GET['id'])) {
        $the_id = $_GET['id'];
    } else {
        // redirect to google if $_SERVER['HTTP_REFERER'] is not set
        header("Location: " . $_SERVER["HTTP_REFERER"] ?? 'http://google.com');
        exit;
    }

?>

注意:我个人更喜欢将用户发送到错误 url 而不是“返回”,但这取决于您。

第二次用non-existent ID解决问题:

一旦你得到一个 ID,你想检查它是否存在,你只需要执行一个 SELECT 并查看它是否返回任何记录,使用类似 mysqli_num_rows 或其OO对应。 完整的代码片段类似于:

<?php
    if (isset($_GET['id'])) {
        $the_id = $_GET['id']; // I'd personally use filter_input here
    } else {
        // redirect to google if $_SERVER['HTTP_REFERER'] is not set
        header("Location: " . $_SERVER["HTTP_REFERER"] ?? 'http://google.com');
        exit;
    }

    // assuming you already have the $connection object available here
    $stmt = $connection->prepare("SELECT * FROM users WHERE ID = ?");
    $stmt->bind_param('i', $the_id); // note that this is the safe way to pass user input to a SQL query to avoid SQL injection
    $stmt->execute();
    $stmt->store_result(); // required to use "num_rows" below
    if ($stmt->num_rows === 0) {
        // no user record was found with the given ID
        // redirect to google if $_SERVER['HTTP_REFERER'] is not set
        header("Location: " . $_SERVER["HTTP_REFERER"] ?? 'http://google.com');
        exit;
    }

    // at this point you're sure that a user with id $the_id exists, so you can continue your code flow as expected :-)

“引荐来源网址”是怎么说的?它一定会指向您的页面吗?

  • 黑客复制了 URL,对其进行了修改,然后将其粘贴到新的 window 中。没有可“返回”的页面。

  • 黑客以某种方式让“推荐人”进入他的页面。

你需要假设任何错误都可能意味着一切都是错误的。除了类似 404 的页面之外,黑客不应该被发送到任何地方。

如果我没理解错的话...如果 $get[] 与 table 中的任何内容都不匹配,您就是在试图转移用户的注意力...您可以使用 mysqli_num_rows ... 下面的代码将转移用户,或者如果没有行与 id 匹配,您可以打印一些其他结果..

<?php 

        $query = "SELECT * FROM users WHERE ID = $the_id";
        $select_posts = mysqli_query($connection, $query);
        $num_rows = mysqli_num_rows($select_posts);

        if($num_rows==0) {
                echo "something else"; OR header("Location: errorpage.php");
                         }
            else {

       //your while() code here 
                 }
    ?>