Page 1 of 1

Intercepting requests

Posted: Wed Jul 11, 2007 1:50 pm
by smudge
I was wondering if it is possible to handle a request (preferably with php) before Apache deals with it, or even after, ie:
1. User requests pageA.php
2. PHP script picks it up and does something, then Apache continues to deal with it

OR

1. User requests pageThatDoesNotExist.php
2. Apache returns 404
3. PHP picks it up, does something like try to find the appropriate page, then either
> Returns apache's response
or
> Returns its own page

I know something like this can basically be done with custom error pages in apache, but i'm looking for a PHP back end.
Does anyone know how I would go about doing this?

Posted: Wed Jul 11, 2007 2:16 pm
by TheMoose
Is there a way to intercept it? Not easily. Apache binds to the port, so you would have to have an app to monitor all incoming connections and freeze the request before it hits Apache. You would then launch your app to do it's thing, and then have it give it to Apache instead.

Doable? Not sure, I don't know what all is involved in doing it other than the theory behind it.

Easy way:
Set Apache's default 404 to be your own custom PHP page. That page does a file search for the requested page, and either redirects to that page, or returns a 404 error.

Posted: Wed Jul 11, 2007 2:50 pm
by Chris Corbyn
Not really a decent way to do it no. Apache needs to accept the request itself in order for PHP to it. Ok, not 100% true. You can write a web server purely in PHP, but getting it to pass requests to apache on condition would be a pain and just smells of bad design. Can you elaborate on your reasoning?

Posted: Wed Jul 11, 2007 2:53 pm
by Ambush Commander
Note that you can set Apache to use a PHP page to handle 404 errors, and you can use mod_rewrite to ensure all requests get forwarded to PHP.

Posted: Wed Jul 11, 2007 5:06 pm
by smudge
@d11wtq: I don't really have a reason right now, but it occurred to me that it would be a good thing to know how to do for things like statistics, where I would intercept a request and add data like browser & version, date/time, ip address, referrer, etc to a database, then allow the original requested page to be returned.

@Ambush Commander:
Ambush Commander wrote:you can set Apache to use a PHP page to handle 404 errors
Do you mean custom error documents, or something else?
Ambush Commander wrote:you can use mod_rewrite to ensure all requests get forwarded to PHP.
What is mod_rewrite and what do you mean by forwarding to PHP?

Posted: Wed Jul 11, 2007 7:21 pm
by TheMoose
All that statistical information can be gotten at runtime through PHP, you don't need to intercept the request to get that stuff. (http://www.php.net/reserved.variables)

And yes, he is referring to using custom error documents. Mod_rewrite just makes it so that when you set a custom error 404 to something like "my404.html", Apache loads up the rewrite rules and sees that anytime someone requests "my404.html", it should really return "my404.php". (http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html)

Posted: Thu Jul 12, 2007 10:46 am
by smudge
I meant that I have one php page to handle statistics, so even pictures and plain html pages can be logged. I know you can access those vars, but not if it's a picture. Am I making any sense?

Posted: Thu Jul 12, 2007 11:48 am
by TheMoose
smudge wrote:I meant that I have one php page to handle statistics, so even pictures and plain html pages can be logged. I know you can access those vars, but not if it's a picture. Am I making any sense?
You can easily set the log format in Apache to log the requests for any and all files that are handled and delivered through Apache. (http://httpd.apache.org/docs/1.3/logs.html). Look at the section under Combined Log Format, and it will show you an example of a log format that adds the referrer and user agent to the log file.

Posted: Thu Jul 12, 2007 2:56 pm
by smudge
Thank you for the advice. It is much appreciated.