johnsmith153 Posted June 3, 2012 Share Posted June 3, 2012 I want to get the HTTP status code of the current request in PHP (e.g. 301, 303, 404, 200 etc.) I don't want to use cURL for other websites, just use PHP to get the status code of that request on the domain PHP is on. Also, I'm considering doing this on every page request, so an idea of how expensive the call to get the status code is would be good (so if I am using cURL I guess it's not a good idea). Obviously I'll only do it on every request if it's a quick process. Thanks. Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/ Share on other sites More sharing options...
requinix Posted June 3, 2012 Share Posted June 3, 2012 It's always 200 unless you change it yourself... You're talking about the status code of the page that PHP is currently rendering, right? Of the script that's executing? Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/#findComment-1350955 Share on other sites More sharing options...
johnsmith153 Posted June 3, 2012 Author Share Posted June 3, 2012 Yes, the page that is currently executing, so this would be ideal: $status_code = get_php_status_code(); if($status_code == "301") { echo "this was a redirect"; } //this is just an example Thanks Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/#findComment-1350956 Share on other sites More sharing options...
johnsmith153 Posted June 3, 2012 Author Share Posted June 3, 2012 So, can you do it? Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/#findComment-1350958 Share on other sites More sharing options...
requinix Posted June 3, 2012 Share Posted June 3, 2012 Can I do what? If you're using 5.4 then you have http_response_code, otherwise you'll have to write something yourself. Assuming you'd be writing PHP code for this, you wouldn't be able to use header() directly as your new code wouldn't know of the new response code. Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/#findComment-1350960 Share on other sites More sharing options...
ignace Posted June 4, 2012 Share Posted June 4, 2012 http://www.php.net/manual/en/function.apache-request-headers.php returns the headers of the current request. Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/#findComment-1351031 Share on other sites More sharing options...
requinix Posted June 4, 2012 Share Posted June 4, 2012 http://www.php.net/manual/en/function.apache-request-headers.php returns the headers of the current request. But the request, not the response. (Which is what OP meant since requests don't have status codes.) Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/#findComment-1351034 Share on other sites More sharing options...
ignace Posted June 4, 2012 Share Posted June 4, 2012 http://www.php.net/manual/en/function.headers-list.php then. Link to comment https://forums.phpfreaks.com/topic/263600-simply-get-the-http-status-code-in-php/#findComment-1351166 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.