Solved

Get HTTP response code in server side

Posted on 2013-06-04
15
502 Views
Last Modified: 2013-07-21
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
Comment
Question by:drupal_100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +1
15 Comments
 
LVL 7

Expert Comment

by:Robert Saylor
ID: 39220189
Try $_SERVER['REDIRECT_STATUS']
0
 

Author Comment

by:drupal_100
ID: 39220237
$_SERVER['REDIRECT_STATUS'] seems always 200 there.
0
 
LVL 7

Expert Comment

by:Robert Saylor
ID: 39220253
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:drupal_100
ID: 39220300
Could you give more details and an example ?
0
 
LVL 7

Expert Comment

by:Robert Saylor
ID: 39220330
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
 
LVL 7

Expert Comment

by:Robert Saylor
ID: 39220336
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
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39220414
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39220792
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
 

Author Comment

by:drupal_100
ID: 39222074
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39222127
$_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
 

Author Comment

by:drupal_100
ID: 39314980
I've requested that this question be deleted for the following reason:

answered
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39314981
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39315773
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question