Login com redirect

Asked

Viewed 49 times

1

I have a login form running with Jquery + PHP, I’m implying the option to redirect based on the url I want to receive via GET, as below:

https://pt-br.example.com/?redirect=https://blog.example.com/

Problem is that I do not receive via GET the redirect variable

Login method

    private function Login() {

$Session -> session_token  = (string) $Functions -> ClearVariable($_POST['tokenForm']); 
$Session -> CheckSessionTokenForm();

$this->Email = (string) mb_strtolower($Functions -> ClearVariable($_POST['loginEmail']));
$this->Password = (string) $Functions -> ClearVariable($_POST['loginPsw']);

if(isset($_GET['redirect']))
{
    $this->Location = $Functions -> ClearVariable($_GET['redirect']);
}
else
{
    $this->Location = 'dashboard/'; 
}

... }

Jquery Post

$('button[type="submit"]').click(function(event){
    event.preventDefault();
    var url = $(this).closest('form').attr('data-url');
    var formId = $(this).closest('form').attr('id');
    var iBtn = $(this).children('i').attr('id');

    $.ajax({            
        url: url,           
        data: $('#'+formId).serialize(),
        dataType:"json",
        type:"post",
        success: function(data){
            switch(data.status) {
                case 'location':  
                    window.location.href = data.flag;
                    break;  
            }
        }
    }); 
});

PHP usage with Jquery post, may be because of Jquery post?

  • What appears when doing the echo? You are handling all the characters in the value of redirect? 'Cause it should be something like redirect=https%3A%2F%2Fblog.example.com%2F

  • Nothing appears, the $this->Location attribute is empty

  • You can put the JS code and the full PHP in the question then?

  • $this->Location is always equal to dashboard/ then?

  • No, I have other pages that the user can click on the login link and be directed to the login page with the origin url, when the user access the direct login page will be directed to Dashboard/

  • But if you do not receive the data with the $_GET, the isset($_GET['redirect']) would always return False. Then you must be receiving the value correctly, but treating it wrong. What is $Functions -> ClearVariable?

  • I removed the isset condition to check if I was getting any value, and yet I received nothing. Clearvariable is to clear user input, avoid attack.

  • What happens if you put echo json_encode($_GET) and in JS, instead of doing the window.location, do console.log(data). Remember to finish running PHP after this echo, with return or exit().

  • I will test the way you put it.. I just created it like this: $Location = $_SERVER['REQUEST_URI']; if($Location !== '/') { $Location = str_replace("/?redirect=","",$Location); } Else { $Location = 'Dashboard/'; } and created an Hidden input in the form with Location.. and I’ll test.. Working or not I’ll be the way you put it, it’s for knowledge. :)

Show 4 more comments

1 answer

1

takes the page passed by the url with $_SERVER['PHP_SELF'] stores in a variable, if you pass by its conditions you take the user to the page you want

  • With echo $_SERVER['REQUEST_URI'], aparece /?redirect=https://blog.example.com/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.