简体   繁体   中英

How to redirect back to the previous page in PHP?

I have a PHP site which have a login page. I have also an order.php page. When the user is not login then the order page redirect the user to the login page first. I want when the user redirect from the order page to the login page then on successfully login the user should redirect back to the order page. And When the user comes from other pages to the login page then on successfully login the user should redirect to the index.php page. How will I do this? Any Idea?

order.php

<?php 
if (!logged_in()){
   header("Location: login.php?referrer=order");
}
?>

login.php

<?php 
if (login_successful()){
   switch($_GET['referrer'])){
      case 'order':
          header("Location: order.php");
      break;
      case 'other':
          header("Location: other.php");
      break;
      default:
          header("Location: index.php");
   }
}
?>

form

<form method="post" action="login.php">
    //
    // Form content
    // 
</form>

---replace to---

form phpself

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    //
    // Form content
    // 
</form>

Simply use a URL in your link. The url from order.php would be:
http://somesite.com/login.php?url=order.php
In your login page you can then read the $_GET["url"] and decide whether to redirect the user back, or redirect him to index.php.

When the user is not login then the order page redirect the user to the login page first.

Why extra redirect?
Why not to show the login form right in place and upon successful login just redirect to the same page?

here is a brief example of the auth.php page

<?
if (isset($_POST['auth_name'])) {
  $name  = mysql_real_escape_string($_POST['auth_name']);
  $pass  = MD5($_POST['auth_name'].$_POST['auth_pass']);
  $query = "SELECT * FROM users WHERE name='$name' AND pass='$pass'";
  $res   = mysql_query($query) or trigger_error(mysql_error().$query);
  if ($row = mysql_fetch_assoc($res)) {
    session_start();
    $_SESSION['user_id'] = $row['id'];
  }
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  exit;
}
if (isset($_GET['action']) AND $_GET['action']=="logout") {
  session_start();
  session_destroy();
  header("Location: http://".$_SERVER['HTTP_HOST']."/");
  exit;
}
if (isset($_REQUEST[session_name()])) session_start();
if (empty($_SESSION['user_id'])) {
  return;
} else {
  include 'top.php';
?>
<form method="POST">
<input type="text" name="auth_name"><br>
<input type="password" name="auth_pass"><br>
<input type="submit"><br>
</form>
<? 
  include 'bottom.php';
}
exit;
?>

now you can just use one following line to protect any page

require "auth.php";

You can set a cookie, or session value, that tracks the return page. Upon successful login, check if there is a return page. If so, go to that page. If not, go to the default welcome page.

Store the login target to the session, before redirecting to the login page.

Example:

  1. The order.php page checks if the user is already logged in and calls login.php if necessary. Before it does the redirect to the login.php page it stores the login target as itself $_SESSION['loginTarget'] = 'order.php' ;
  2. After the login page has finished successfully, it calls the login target. header('Location: '.$_SESSION['loginTarget'], true, 303); exit;

Because the login target is stored in the session, you can do error handling on the login page easily, or even create a new user account instead of login (no need to pass on the target in the url to every page).

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