How to redirect back to previous page in PHP?

I have a PHP site that has a login page. I also have an order.php page. When the user is not logged in, the order page first redirects the user to the login page. I want the user to redirect from the order page to the login page, and then upon successful login, the user must redirect back to the order page. And when the user comes from other pages to the login page, and then upon successful login, the user must redirect to the index.php page. How do i do this? Any idea?

+4
source share
5 answers

When the user is not logged in, the order page first redirects the user to the login page.

Why do I need additional redirection? Why not show the login form in place and after a successful login just redirect to the same page?

here is a short 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 simply use the next one line to protect any page

 require "auth.php"; 
+1
source

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

The form

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

--- replaced by ---

phpself form

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

Just use the url in your link. The order.php url will be as follows:
http://somesite.com/login.php?url=order.php
On the login page, you can read $_GET["url"] and decide whether to redirect the user back or redirect him to index.php.

+1
source

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

0
source

Save the sign-in goal before redirecting to the sign-in page.

Example:

  • The order.php page checks if the user has already been registered and calls login.php , if necessary. Before it redirects to the login.php page, it saves the login target as $_SESSION['loginTarget'] = 'order.php' ;
  • Upon successful completion of the login page, it invokes the login target. header('Location: '.$_SESSION['loginTarget'], true, 303); exit;

Since the login purpose is saved in the session, you can easily handle errors on the login page or even create a new user account instead of logging in (there is no need to transfer the target in the URL to each page).

0
source

Source: https://habr.com/ru/post/1382439/


All Articles