How to redirect the correct URL after login

I have a website that has a webpage containing a list of magazines. Now I usually give a link to access this page for some user, for example: http://172.20.22.77/someapp/results.htm?id=45

Now when some user clicks on this. He will give him the login screen. But after entering the system, he does not go to the page that was intended. I use sessions to implement a website, and it has many pages, so a session is used to track a user's viewing of a page.

kindly tell me how I can redirect the URL given after the user logs in.

+3
source share
4 answers

URL- . URL .

, :

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

:

session_start();
if (user has entered correct username and password) {
    header("Location: http://example.com" . $_SESSION['after_login']);
}
+6

URI , :

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

// ...

header('Location: http://mysite.com ' . $_SESSION['URI'];
+1

Use this on a secure page to save the current url page and query string in 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 a successful login to redirect the user to the page stored in the session.

<?php
    session_start();
    header('Location: '.$_SESSION['url_attempt']);
?>
0
source
<?php
header('Location: http://www.example.com/');
?>

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

-2
source

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


All Articles