0

I have a modal for my login form. I submit the login form using Ajax. If username and password were invalid, I display a message to inform user about failure But if login data was valid, I'd like to redirect the user to dashboard.php Here is what I wrote in php file for response:

$action = $_GET['action'];
if($action=="checkLogin")
{
    $result = check_login();
    if($result=="failure")
    {
        echo "login failed";
    }
    else
    {
        echo 'success
        <script>
        window.location = "dashboard.php";
        </script>
        ';

    }
}

But it is not working as I expect. "failure" and "success" are shown, but redirect does not happen in case of success Please help me were I am wrong? Thanks in advance

Javad
  • 61
  • 7
  • Kindly Check this. It might help you. http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – Princess Aug 12 '16 at 05:45

4 Answers4

2

Your PHP can't control what the client does from an AJAX response; It can't set headers or redirect the client. Instead, you need to return an appropriate success or failure HTTP status code and redirect based on that. A 200 status means success, a 401 means a unauthorized. Click here to learn more about HTTP status codes.

In PHP, response codes can be set in a variety of ways, depending on your version. My example below is the most bare-bones way and should work in PHP4 but Click here for more ways to set them.

if($result == "failure") {
    header("HTTP/1.1 401 Unauthorized");
    echo "failure";
}
else {
    header("HTTP/1.1 200 OK");
    echo "success";
}

You can then capture those status codes in your javascript client and redirect if the login is successful. If you were using JQuery it might look something like this:

$.ajax("example.php")
  .done(function() {
    // done() is called on any 200 response codes (200, 201, etc)
    window.location = "dashboard.php";
  })
  .fail(function() {
    // fail() is called on any 400 or 500 response codes (400, 401, 404, 500, etc)
    alert("error");
  });

It's worth noting that because of the status codes, you do not need to check the response body in your javascript for the words "success" or "failure". Browsers and other clients are already designed to gracefully handle HTTP response codes, so use them as much as possible.

Community
  • 1
  • 1
Soviut
  • 88,194
  • 49
  • 192
  • 260
  • Thanks for you informative response. just please check my post below. I still have problem. Thanks – Javad Aug 12 '16 at 06:35
0

Try this:

else
{
    header('location:your_location.php');
}

or if you are using ajax than return false or something else from here and for this put this in js:

window.location.href = "your_location";
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

This is what I have done to redirect page:

else{

    echo "<meta charset='utf-8'/>";
    //direct after five seconds 
    echo "<meta content='5; url=http://".$_SERVER['HTTP_HOST']."/dashboard.php"."' http-equiv='refresh'> ";

}

What I think about is you don't need 'success' as response , If the login valid , just redirect the page. If fail , OK , let us know with echo "login failed";

Carr
  • 2,691
  • 1
  • 19
  • 27
0

If you can redirect using php you could do this:

header('location : your_php_page.php');

the problem with your code is that you're calling it from ajax. meaning the code is most probably returning text. not html or javascript. if this was coming back to an HTML page, sure it'd print the "Script" part and your js would run. but here, you'd have to do something like this:

in your ajax callback('success' or 'then'. whichever you used):

success: function(data){
    if(data == "success")
        window.location.redirect("path.php");
}
Mridul Kashyap
  • 704
  • 1
  • 5
  • 12