执行代码后重定向

Redirect after executing code

本文关键字:重定向 代码 执行      更新时间:2023-09-26

我希望这个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);
?>

在会话中获取url,当你想要重定向时,只需使用该url并重定向用户并取消会话变量

//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:

重定向
<? header("location: http://.........."); ?>

请注意,在此指令之前,你不能打印html,如果一些html打印你的标题将不会发送

add

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

可以使用javascript代码:

  • forward: window.history.go(+1)
  • 反向:window.history.go(-1)

或jquery代码

  • forward: history.go(1);
  • backward: history.go(-1);