B
B
BuBux2020-05-07 12:42:05
PHP
BuBux, 2020-05-07 12:42:05

How to organize a redirect?

Personal account website. If they throw me a link to what's inside the personal account and when I click on the link I'm not logged in, then I'm redirected to the login page. But after I log in, it throws me to the main page, and not to the link I clicked on. How to solve it?
PS I get the transition link from $_SERVER['REQUEST_URI']
Let's say the transition link domen.ua/task/view/85
I have a TaskController class that is inherited from the BaseController base class. BaseController:

class BaseController {
    public function __construct() {
        $_SESSION['adress'] = $_SERVER['REQUEST_URI']; // тут я и записываю в сессию ссылку, по которой перешли. Понимаю что в сессию записывать не правильно
        User::checkAuth();
    }
}

checkAuth function:
function checkAuth() {
    	include 'config/config.php';
        //если не авторизован, переадресовываем на страницу авторизации
        if(!User::isAuth()) {
           header("Location: ".$s_prefix."/user/login");
        } else {
            Registry::set('user', $_SESSION);
            return true;
        }
}

isAuth function:
function isAuth() {

        if(!isset($_SESSION['is_auth'])) return false;
        if(!($_SESSION['is_auth'] === TRUE)) return false;

        User::checkLive(); //проверяем, нужно ли убить сессию
        return true;
    }

Authorization function:
public function actionLogin() {
    // проверки всякие...
    
    // если всё хорошо
    if ($update) {
        header("Location: " . $_SESSION['adress']);
        unset($_SESSION['adress']);
      } else {
           $msg_title = "Ошибка";
            $msg_cont = "Error 111";
      }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2020-05-07
@zkelo

When redirecting to the login page, you can pass a GET parameter to it with a link to the page to which the user will need to be redirected after logging in

F
felony13twelve, 2020-05-07
@felony13twelve

As I understand it, when you follow the links in the personal account and you are not auto-removed, then you need to be redirected to the login page. Make a check, if there is no session, then throw it on the login page

A
Alexander, 2020-05-07
@efir

Add a hidden field to the authorization form in which write the link from which the user was redirected to the form, and after successful authorization, do a reverse redirect.
input type="hidden" name="AuthRedirect" value="<?php echo $_SERVER['HTTP_REFERER']; ?>"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question