How to forward to the correct URL after login

浪子不回头ぞ 提交于 2019-12-04 13:49:56

问题


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 .


回答1:


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']);
}



回答2:


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'];



回答3:


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']);
?>



回答4:


<?php
header('Location: http://www.example.com/');
?>

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



来源:https://stackoverflow.com/questions/4907321/how-to-forward-to-the-correct-url-after-login

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!