PHP <=5.3
The header()
function has a parameter for status code. If you specify it, the server will take care of it from there.
header('HTTP/1.1 401 Unauthorized', true, 401);
PHP >=5.4
See Gajus’ answer: https://stackoverflow.com/a/14223222/362536