0

Sorry, this is a bit of a noob question, but...

I am creating a login page and I am having difficulty getting the login page to send me back to the web page that prompted the login redirect...

Can the previous page be stored via POST variable and accessed on the next page (login.php)?...I am having trouble just keeping that webpage url...if someone could show me how and explain why that would be amazing!

If I could just see how it looks to store variables to post so they can be viewed on the next page that would solve all my problems

original webpage:

<?php //how do I post current url so it can be accessed on login require_once('./../User_Auth/includes/authenticate.php'); ?>

login webpage:

`header("Location:");`

4 Answers4

1

Use Sessions to pass data to next page!

<?php  session_start(); ?>  // session starts with the help of this function 

<?php

if(isset($_SESSION['use']))   // Checking whether the session is already there or not if 
                              // true then header redirect it to the home page directly 
 {
    header("Location:home.php"); 
 }

if(isset($_POST['login']))   // it checks whether the user clicked login button or not 
{
     $user = $_POST['user'];
     $pass = $_POST['pass'];

      if($user == "Ank" && $pass == "1234")  // username is  set to "Ank"  and Password   
         {                                   // is 1234 by default     

          $_SESSION['use']=$user;


         echo '<script type="text/javascript"> window.open("home.php","_self");</script>';            //  On Successful Login redirects to home.php

        }

        else
        {
            echo "invalid UserName or Password";        
        }
}
 ?>
Gurtej Singh
  • 225
  • 1
  • 9
  • I think you are closest....I have been trying to put $_SERVER['REQUEST_URI'] into a session variable but I think logging in clears that variable – thecomissar Apr 03 '17 at 23:04
1

What I've understood from your question is that you are on page that a certain point will redirect you to the login page, and after the login you would come back to the previous page, right?

If so:

From the php $_SERVER var you can get this information.

Practice example:

file loginpage.php

<?php 
//at the beginning you place the code that catch the POST data if a login request was sent


if(!empty($_POST["username"]) && !empty($_POST["password"])){

//HERE DO LOGIN TRY


  if(LOGIN_SUCCESSFULL){
    $page = $_SERVER["HTTP_REFERER"]; //this contain the previous page
    header("Location: $page");
  }
  else{
  // show an error
  }

}     
else{
   //if you arrive at this point of the code, this means that the we are    visiting login page, so we have to rendere the page
   require_once 'login_page_body_with_login_form.php'
}

Of course there are more advanced and secure technique, but this should give both an answer and an idea of how make things together.

user2548436
  • 915
  • 2
  • 16
  • 35
0

Use this code on the landing page to redirect to the login page, passing the landing page URL as a $_GET variable.

if ( USER_NEEDS_TO_LOGIN ) {
  $login = "/path/to/login/page";
  $login = $login . '?tgturl=' . $_SERVER[ "REQUEST_URI" ];
  header("Location: $login");
}
0

You could pass your previous page value from current page to authenticate.php.

<?php
    $previousPage = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

    //how do I post current url so it can be accessed on login
    require_once('./../User_Auth/includes/authenticate.php');
?>

You can then access $previousPage in authenticate.php.

cosmoonot
  • 2,161
  • 3
  • 32
  • 38