Link to home
Start Free TrialLog in
Avatar of drupal_100
drupal_100Flag for United States of America

asked on

Get HTTP response code in server side

Is there a way to get HTTP response code in server side ?

For example response code 200 OK.
Could we get it in server side, saying use PHP?
Avatar of Robert Saylor
Robert Saylor
Flag of United States of America image

Try $_SERVER['REDIRECT_STATUS']
Avatar of drupal_100

ASKER

$_SERVER['REDIRECT_STATUS'] seems always 200 there.
might be better to use custom error documents with .htaccess

ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html

Instead of html files you could use php files. Have the php files store or email the info you are wanting to capture then display a response to the user.
Could you give more details and an example ?
Here you go. I did this on a 404

404.php
<?php

print "Sorry the file you were looking for does not exist.<br>\n";

// Now we can capture info
$visitor_ip = $_SERVER['REMOTE_ADDR'];

// Get browser
$browser = $_SERVER['HTTP_USER_AGENT'];

// Send an email to admin
$subj = "404 on my domain";
$msg = "A 404 error has on my site<br>
Visitor: $visitor_ip<br>
Browser: $browser<br>";

mail("robert@**********.com",$subj,$msg);
?>

Open in new window


.htaccess
ErrorDocument 404 /404.php

Open in new window


Log File sending email:
root@mx [/home/*****/www]# tail -f /var/log/exim_mainlog | grep robert
2013-06-04 16:47:59 1Ujy99-002IIz-1J <= rsaylor@mx.*******.com U=rsaylor P=local S=595 T="404 on my domain" for robert@*******.com
2013-06-04 16:47:59 1Ujy99-002IIz-1J => robert <robert@*******.com> R=virtual_user T=virtual_userdelivery

Note: For this test I did not pass email headers but you would want to define headers. If you need some I can post a good working set later.
Sorry forgot: then goto http://www.yourdomain.com/fakefile.html and a 404 should be generated. You can do this on all except actual response codes that would return a proper file. IE: 200 you would not want to use the error handling but if your file is displayed then it got a 200 and you could put code in to track your visitors.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why do you want to do this?  What's the big-picture issue?  It is a very, very rare pursuit!  If you can tell us the business logic issues, we might be able to help with a solution.
I have a API application.
When users call/use the API, sometimes something goes wrong, and we can figure out where/why.
So I want to log the API call.
I want to log:
user request
our response
response code
.....

Usually the client/user application gets the response and also can get the response code.
I want to get this response code in server side when we send the response back to client/user.

$_SERVER['REDIRECT_STATUS'] gives the response code, but I am not sure if this is the response code the client/user application program will get in their side.
From internet discuss, someone mentioned that $_SERVER['REDIRECT_STATUS']  always 200.

If $_SERVER['REDIRECT_STATUS']  is really the response code that the client/user gets, then this is the solution and will use this. So just need to confirm about this.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've requested that this question be deleted for the following reason:

answered
Instead of deleting the question, please post the answer that you found.  If the answer was one that an expert helped with, it's appropriate to award points.  If you originated the answer without any help from the Experts-Exchange community, it's fine for you to accept your own answer.

Thanks, ~Ray