简体   繁体   中英

Redirect after executing code

I want this PHP code to redirect to the previous page and refresh automatically... I understand that with JavaScript I can go history back but it won't refresh. Please help.

My code:

<?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);
?>

Get the url in a session, when you want to redirect, just use that url and redirect the user and unset the session var

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

On the next page use this

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

Be sure you are declaring session_start() at the top of the page

To redirect with php:

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

Please note that before this instruction you mustn't print html, if some html is printed your header will not be sent

add

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

you can use javascript code :

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

or jquery code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM