简体   繁体   中英

Redirect user to a specific route based on the referring route after login

In the AuthController.php I have a function:

 
 
 
 
  
  
  public function authenticated( \\Illuminate\\Http\\Request $request, \\App\\User $user ) { return redirect()->intended($this->redirectPath()); }
 
 
  

I have login popup on every page so if the user logins from a page: /demo-page and logins, they will be redirected back to the same page( /demo-page ) after login. This works absolutely as expected. No problem here.

My Question: How can I redirect user to specific pages based on their current route from which the login was invoked.

Logic something like this:

 if referring route('indexpage'){ redirect to route('homepage'); } else any other route other than index page{ return redirect()-> to previous url } 

So previously I was looking at my AuthController file. But I forgot that I had changed the LoginController for the redirect

 public function __construct() { $this->redirectTo = url()->previous(); //Redirect the user to previous page after login $this->middleware('guest')->except('logout'); } 

What I need to do:

Suppose there are 3 routes. A, B and C

First the user lands on page A If he logins there he have to be redirected to page B .

If the user is on page B and he logins there, he have to be redirected back to page B .

If the user is on page C and he logins there, he have to be redirected back to page C .

Note: the login is a common form on header of every page.

What's currently working?

With return redirect()->intended($this->redirectPath()); this, the user from A is redirect to A, B to B and C to C.

This should work:

public function authenticated(\Illuminate\Http\Request $request, \App\User $user)
{
    if ($to = $request->input('redirect_route')) {
        $to = URL::route($to);
    }

    $to = $to ?: $this->redirectPath();

    return redirect()->intended(
        $to
    );
}

Read the API here .

What you can do on the page where you restrict access to autorised users, use this code to set a url paramater called accesscheck:

$restrictGoTo = "login.php";
if (!((isset($_SESSION['Username'])) && 
(isAuthorized("",$authorizedUsers, $_SESSION['Username'], 
$_SESSION['UserGroup'])))) {   

  $qsChar = "?";
  $referrer = $_SERVER['PHP_SELF'];
  if (strpos($restrictGoTo, "?")) $qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 
  0) 
  $referrer .= "?" . $_SERVER['QUERY_STRING'];
  $restrictGoTo = $restrictGoTo. $qsChar . "accesscheck=" . 
  urlencode($referrer);
  header("Location: ". $restrictGoTo); 
}

And then on your login page. First thing you need to do is check if the url parameter accesscheck is present. It it is, redirect to the page after login, otherwise, go to the page specified.

if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}


//run your login script and if the user is authenticated proceed to the 
previous url or the success page:

    $redirectLoginSuccess = "yourpage.php";
  if (isset($_SESSION['PrevUrl']) && true) {
      $redirectLoginSuccess = $_SESSION['PrevUrl']; 
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }

This works even if you use parameters in your url's

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