How to forward to the correct URL after login

2019-03-22 04:21发布

I have a website which has a web page containing list of logs. Now I usually give the link to access that page for some user eg : http://172.20.22.77/someapp/results.htm?id=45

Now when some user clicks on this .It would give him a login screen .But after login it does not go to the page that was intented . I am using sessions to implement the website and it has many pages, hence session is used to track the user browsing the page.

kindly let me know how can i redirect the URL being asked after user logs in .

4条回答
闹够了就滚
2楼-- · 2019-03-22 04:38
<?php
header('Location: http://www.example.com/');
?>

http://php.net/manual/en/function.header.php

查看更多
够拽才男人
3楼-- · 2019-03-22 04:46

You can save the URI in the session and before the login simply make the redirection:

$_SESSION['URI'] = $_SERVER['REQUEST_URI'];

// ...

header('Location: http://mysite.com ' . $_SESSION['URI'];
查看更多
ら.Afraid
4楼-- · 2019-03-22 04:51

Put the URL the user was accessing in the session before redirecting to the login page. The login page can then redirect back to that stored URL after authenticating the user.

On the page that needs logging in:

session_start();
$_SESSION['after_login'] = $_SERVER['REQUEST_URI'];
header("Location: login.php");

On the login page:

session_start();
if (user has entered correct username and password) {
    header("Location: http://example.com" . $_SESSION['after_login']);
}
查看更多
老娘就宠你
5楼-- · 2019-03-22 04:51

Use this on protected page to save the current page url and query string to the session.

<?
function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }

    return $pageURL;
}


session_start();
$_SESSION['url_attempt'] = curPageURL();

Use this after successfull login to redirect the user to the page stored in the session.

<?php
    session_start();
    header('Location: '.$_SESSION['url_attempt']);
?>
查看更多
登录 后发表回答