gpt4 book ai didi

php - 执行代码后重定向

转载 作者:行者123 更新时间:2023-11-28 16:05:20 92 4
gpt4 key购买 nike

我希望这段 PHP 代码重定向到上一页并自动刷新...我知道使用 JavaScript 我可以回溯历史,但它不会刷新。请帮忙。

我的代码:

<?php
$con=mysqli_connect("host","user","pass","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//post result to db
$result_set = mysqli_query($con,"SELECT points FROM total WHERE id = 1");
$row = mysqli_fetch_assoc($result_set);
$old_total = $row['points'];
$new_total = $old_total + $_REQUEST['total'];
mysqli_query($con,"UPDATE total SET points = $new_total WHERE id = 1");
mysqli_close($con);
?>

最佳答案

获取 session 中的 url,当您想要重定向时,只需使用该 url 并重定向用户并取消设置 session 变量

//Track this on the page you want to redirect your code
$_SESSION['prev_url'] = $_SERVER['REQUEST_URI'];

在下一页使用这个

//When you want to redirect to previous page
header('Location: '.$_SESSION['prev_url']);
exit;

确保在页面顶部声明 session_start()

关于php - 执行代码后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15321042/

92 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com