1

I'm trying to check if a username is available for registering by doing async validation with AngularJS on the client side and PHP on the server side.

So I do something simple like this to check if the user exists:

$user = User::find_by('username', $_GET['username']);
if ($user) {
    header("HTTP/1.1 200 OK");
}
else {
    header("HTTP/1.1 404 Not Found");       
}

and I try to return some status codes if it finds the user or not.

But when I try to use something like postman to send GET requests the returning status code is always 200. It doesn't look like I'm able to send different status codes at all.

Alex
  • 5,671
  • 9
  • 41
  • 81
  • so what is $user being set to? – deowk Apr 25 '15 at 01:11
  • 1
    header() is only valid before php creating any output. Did you check that? – Shiji.J Apr 25 '15 at 01:11
  • You'd generally return data, for instance JSON, that has a property that indicates if the user was found or not, I don't think it's a good idea to trigger the ajax error handler by returning a 404. – adeneo Apr 25 '15 at 01:12
  • User::find_by('username', $_GET['username']); is returning true/false? can you paste a var_dump($user) ? – Catalin Sterian Apr 25 '15 at 01:15
  • I have checked both occurances even if the User is found or not by "echoing" inside those conditionals. Also I have removed everything from the page and to just return a 404, still I get 200. – Alex Apr 25 '15 at 01:20
  • As @Shiji.Jiang said, maybe you are sending something to the output before setting the header. Set logging report to warning level and check if you are not getting this warning http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – mchurichi Apr 25 '15 at 01:37
  • 1
    Press F12 in your browser, select the network tab then see what headers (if any) are actually being sent. And use: header("HTTP/1.1 200 OK",true,200); http://php.net/manual/en/function.header.php – Cerad Apr 25 '15 at 02:28

0 Answers0