PHP 从删除页面重定向
PHP Redirect from delete page
我有两个页面:index.php
和 stats.php
这两个页面都包含来自数据库的相同查询,并且都有一个删除按钮。当按下 delete-button
时,它会像这样重定向:https://example.com/delete.php?id=50;
其中 50 是行 ID。
delete.php
看起来像这样:
<?php
include "db.php"; // Using database connection file here
$id = $_GET['id']; // get id through query string
$del = mysqli_query($link,"DELETE FROM table WHERE id = '$id'"); // delete query
if($del)
{
mysqli_close($link); // Close connection
header("location:index.php"); // redirects to all records page
exit;
}
else
{
echo "Error deleting record"; // display error message if not delete
}
?>
现在的问题是,当用户按下删除键时,它删除了该行,并重定向回 index.php。但是我怎样才能检查它是从哪个页面删除的,并重定向回那个页面呢?
编辑:
出于测试目的,我在下面尝试了类似的操作,但是当从两个页面中删除时,它们都给了我相同的输出:“2”:
if($del)
{
if($_SERVER['HTTP_REFERER'] == "index.php")
{
echo "1";
} else {
echo "2";
}
}
您可以使用 $_SERVER["HTTP_REFERER"]
.
它包含上一页的URL。
$_SERVER['HTTP_REFERER']
包含完整的 URL.
例如:http://localhost/my-site/index.php
所以你可以这样做:
header("location:".$_SERVER['HTTP_REFERER']);
正如@Bazaim 所说。使用 $_SERVER['HTTP_REFERER']
.
为了您的目的,它可能是这样的:
if ($del) {
...
header('location: ' . $_SERVER['HTTP_REFERER'])
}
其他:您在 msql 查询中使用了 $_GET['id']
。如果我要在您的 URL 中添加 ?id=0' OR '1=1
。 (id=0%27%20OR%20%271=1) 这可能会删除您的整个 table。您可能想阅读有关 mysql 注入的内容。
我有两个页面:index.php
和 stats.php
这两个页面都包含来自数据库的相同查询,并且都有一个删除按钮。当按下 delete-button
时,它会像这样重定向:https://example.com/delete.php?id=50;
其中 50 是行 ID。
delete.php
看起来像这样:
<?php
include "db.php"; // Using database connection file here
$id = $_GET['id']; // get id through query string
$del = mysqli_query($link,"DELETE FROM table WHERE id = '$id'"); // delete query
if($del)
{
mysqli_close($link); // Close connection
header("location:index.php"); // redirects to all records page
exit;
}
else
{
echo "Error deleting record"; // display error message if not delete
}
?>
现在的问题是,当用户按下删除键时,它删除了该行,并重定向回 index.php。但是我怎样才能检查它是从哪个页面删除的,并重定向回那个页面呢?
编辑:
出于测试目的,我在下面尝试了类似的操作,但是当从两个页面中删除时,它们都给了我相同的输出:“2”:
if($del)
{
if($_SERVER['HTTP_REFERER'] == "index.php")
{
echo "1";
} else {
echo "2";
}
}
您可以使用 $_SERVER["HTTP_REFERER"]
.
它包含上一页的URL。
$_SERVER['HTTP_REFERER']
包含完整的 URL.
例如:http://localhost/my-site/index.php
所以你可以这样做:
header("location:".$_SERVER['HTTP_REFERER']);
正如@Bazaim 所说。使用 $_SERVER['HTTP_REFERER']
.
为了您的目的,它可能是这样的:
if ($del) {
...
header('location: ' . $_SERVER['HTTP_REFERER'])
}
其他:您在 msql 查询中使用了 $_GET['id']
。如果我要在您的 URL 中添加 ?id=0' OR '1=1
。 (id=0%27%20OR%20%271=1) 这可能会删除您的整个 table。您可能想阅读有关 mysql 注入的内容。