[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

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?
0
drupal_100
Asked:
drupal_100
  • 4
  • 4
  • 4
  • +1
2 Solutions
 
Robert SaylorSenior DeveloperCommented:
Try $_SERVER['REDIRECT_STATUS']
0
 
drupal_100Author Commented:
$_SERVER['REDIRECT_STATUS'] seems always 200 there.
0
 
Robert SaylorSenior DeveloperCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
drupal_100Author Commented:
Could you give more details and an example ?
0
 
Robert SaylorSenior DeveloperCommented:
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.
0
 
Robert SaylorSenior DeveloperCommented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
If you are still trying to get info in PHP from another web site, curl can give you the return code.   http://us2.php.net/manual/en/function.curl-getinfo.php
0
 
Ray PaseurCommented:
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.
0
 
drupal_100Author Commented:
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.
0
 
Ray PaseurCommented:
$_SERVER["REDIRECT_STATUS"] is a PHP variable, set by PHP on behalf of your script.  To quote the man page, "There is no guarantee that every web server will provide any of these..."  Executive summary: This is not the right tool.  Response codes are for HTTP things like server errors (500), missing resources (404), security checks (403), etc.  If your client request gets to the API script, the likely response code is only 200, since the HTTP request succeeded.

Here is what I would do instead.  Create a standard form of response from the API that includes an error indicator.  Set the error indicator to zero if the API succeeded.  Set the error indicator to an error code if the API failed.  When you publish the API, you will publish a versioned interface document.  In the documentation, you can list the error codes and their meaning.  If your API returns XML, the XML might start with something like this.
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <error>0</error>
  <body> ... data here </body>
</response>

Open in new window

The code snippet below is a semi-related example.  It shows the errors that can occur when PHP attempts to upload a file in response to an HTTP POST-method request.  The response code from HTTP will be the 200, but the error code from PHP will make sense of anything that went wrong.
// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;

Open in new window

Logging the calls to the API is a very good idea, whether or not the API succeeded.  Depending on what you're publishing it may even be a legal requirement.
0
 
drupal_100Author Commented:
I've requested that this question be deleted for the following reason:

answered
0
 
Ray PaseurCommented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now