RyanSmith345 Posted September 28, 2006 Share Posted September 28, 2006 Hello PHP communtiy.I know that I have found the answer to this before, but I need to figure out how to make a PHP call on every webserver request. I can do this in ASP/ASP.NET with the Begin_Request method in the Global.asaHow can I accomplish this in PHP.Thanks. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 29, 2006 Share Posted September 29, 2006 Well welcome to phpfreaks.. .Now what do you mean by 'call on every webserver request'. Explain a little more into detail on what exactly you are trying to accomplish...Tom Quote Link to comment Share on other sites More sharing options...
RyanSmith345 Posted September 29, 2006 Author Share Posted September 29, 2006 For example, in ASP.NET, you have a file called Global.asax. There are several methods in this file that get called on certain actions.Application_StartSession_Startand the big one that I'm interested in isBegin_Request - this gets fired everytime a request comes in, well at least a reqeust that will be processed with the ASP.NET worker process.What I'm looking to do is build a stats tracer. With the method above, I can assign PDF's and other files that don't execute any server side code to be processed with ASP.NET and this event will get fired on every web request. That way I can track statistics for every file, not just PHP page requests. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 29, 2006 Share Posted September 29, 2006 I am not sure but I don't think that php has a function for this.. Maybe you should use javascript (ajax) to send results to a back end php file for processing. You could have an onClick event, which would call the processing function, for each file or page. Of course this would not work if the client directly accesses the file..Maybe someone else would be of more use on this..Good Luck,Tom Quote Link to comment Share on other sites More sharing options...
RyanSmith345 Posted September 29, 2006 Author Share Posted September 29, 2006 Funny, I have had that exact suggestion from WebTrends before.That's the issue is I really need to be able to track on every web request, and JavaScript is a poor solution to statistics gathering as there are just too many people out there with JS diabled, Spiders, Mobile Devices, the list goes on and on.I'm starting to think the way to go is to create an ISAPI filter for IIS and an Apache Module that you can simply plugin. Unfortunally, I don't know how to code either of those off the top of my head so that could be some serious research time.Regardless, I would still like to know if this is possible with PHP and hear any other possible solutions that people might have.Thanks for your suggestion. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 29, 2006 Share Posted September 29, 2006 maybe you can get the cource code for something like [url=http://www.mrunix.net/webalizer/download.html]webalizer[/url] and make the nessecary adjustments. I think that it is c++.Good Luck,Tom Quote Link to comment Share on other sites More sharing options...
RyanSmith345 Posted September 29, 2006 Author Share Posted September 29, 2006 I think I actually have the source code for Webalizer, but I HATE C++. If I have to write it in C++, I'll probably not do it at all.I'm actually very excited for the new IIS 7 in vista to come out. It looks like it is going to be extreamly easy to code up extensions. Too bad I'll still be supporting all of these legacy 2000 boxes we have around. Quote Link to comment Share on other sites More sharing options...
printf Posted September 29, 2006 Share Posted September 29, 2006 Just use the php.ini settings! (per_dir via .htaccess or globally via php.ini), acts just like a include file, it has global scope of all SUPER GLOBALS before any script it executed!auto_prepend_file =auto_append_file =info [url=http://us2.php.net/ini.core]here[/url]me! Quote Link to comment Share on other sites More sharing options...
RyanSmith345 Posted September 29, 2006 Author Share Posted September 29, 2006 EXCELLENT! I think this is exactly what I was looking for. Thanks. Quote Link to comment Share on other sites More sharing options...
RyanSmith345 Posted September 29, 2006 Author Share Posted September 29, 2006 This is working the way I want it, however I have run into another issue. I want to append this code to PDF requests as well so I can track the so I added the following to the httpd.config file:AddType application/x-httpd-php .pdfThis works as expected, however PHP chokes when it tries to process the PDF file.Is there a way to tell PHP to stop processing the file after a certain point and just output the rest like normal HTML?Thanks. Quote Link to comment Share on other sites More sharing options...
printf Posted September 29, 2006 Share Posted September 29, 2006 You have to process the document your self, because a PDF file is not a PHP file, so all you do is...in the global prepend...1. do your statistics process as you normally would2. get the SUPER GLOBAL => $_SERVER['SCRIPT_NAME']3. substr() it to get the script extension (ie: pdf, php)!4. if it is a (pdf), serve it then exit!5. if it's not a pdf, let PHP continue!simple working example![code=php:0]<?// do your tracking (statistics) process here/* then for pdf(s) */$script = basename ( $_SERVER['SCRIPT_NAME'] );$type = strtolower ( substr ( $script , ( strrpos ( $script, '.' ) + 1 ) ) );if ( $type == 'pdf' ){ header ( 'Cache-control: max-age=31536000' ); header ( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); header ( 'Content-Length: ' . filesize ( '.' . $_SERVER['SCRIPT_NAME'] ) ); header ( 'Content-Disposition: filename="' . $script . '"' ); header ( 'Content-Type: application/pdf; name="' . $script . '"' ); readfile ( '.' . $_SERVER['SCRIPT_NAME'] ); exit ();}// let the script call continue if it not a pdf!?>[/code]nice and simple...updated to add strtolower() for [b]$type[/b]me! Quote Link to comment Share on other sites More sharing options...
RyanSmith345 Posted September 30, 2006 Author Share Posted September 30, 2006 Unbeliveable!These forums are great. Thanks for all your help. That would have taken me forever to figure out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.