Reputation: 85
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';
}
}
Upvotes: 0
Views: 1052
Reputation: 32402
header takes a string as its parameter (not integer).
header("HTTP/1.0 201 Created")
or use http_response_code
http_response_code(201)
Upvotes: 3
Reputation: 5723
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.
Upvotes: 1