Intercepting requests
Moderator: General Moderators
Intercepting requests
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?
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?
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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?
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
@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:
Do you mean custom error documents, or something else?Ambush Commander wrote:you can set Apache to use a PHP page to handle 404 errors
What is mod_rewrite and what do you mean by forwarding to PHP?Ambush Commander wrote:you can use mod_rewrite to ensure all requests get forwarded to PHP.
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)
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)
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.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?