Solved

Get HTTP response code in server side

Posted on 2013-06-04
15
498 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
  • 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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 109

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 109

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 109

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 109

Expert Comment

by:Ray Paseur
ID: 39315773
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 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