Question

I'm using AJAX for a login form, upon a user supplying the correct credentials it should redirect to a landing page.

This is the AJAX layout (login and register hit different functions in accounts.js):

login.html                        login.php
               -> accounts.js ->
register.html                     register.php

In both login.php and register.php there is code that checks the success like so:

if($success == 1)
{
     echo '$success';
     $_SESSION['id'] = $id;
         header(201);
     exit();
}

Register returns 201 if it successful, however login always returns 200 (but does echo the successful message)

The JS code:

xHRObject.open("GET", "login.php?id=" + Number(new Date) + options, true);
xHRObject.onreadystatechange = function() {
if (xHRObject.readyState == 4 && xHRObject.status == 200)
   {
    alert(xHRObject.status);        
   }
   else if(xHRObject.status == 201)
   {
        window.location.href = 'landing.html';
   }
}
Was it helpful?

Solution

header takes a string as its parameter (not integer).

header("HTTP/1.0 201 Created")

or use http_response_code

http_response_code(201)

OTHER TIPS

You have to output headers before any content.

Try moving header(201); to before the echo call and make sure you don't have anything else outputting even earlier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top