Jump to content


Photo

Global.asa equivelent


  • Please log in to reply
11 replies to this topic

#1 RyanSmith345

RyanSmith345
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationFort Collins, CO

Posted 28 September 2006 - 10:58 PM

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.asa

How can I accomplish this in PHP.

Thanks.

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 29 September 2006 - 12:15 AM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 RyanSmith345

RyanSmith345
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationFort Collins, CO

Posted 29 September 2006 - 05:08 PM

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_Start
Session_Start

and the big one that I'm interested in is

Begin_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.

#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 29 September 2006 - 07:55 PM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 RyanSmith345

RyanSmith345
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationFort Collins, CO

Posted 29 September 2006 - 08:33 PM

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.

#6 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 29 September 2006 - 08:52 PM

maybe you can get the cource code for something like webalizer and make the nessecary adjustments. I think that it is c++.

Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#7 RyanSmith345

RyanSmith345
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationFort Collins, CO

Posted 29 September 2006 - 08:54 PM

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.

#8 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 29 September 2006 - 08:57 PM

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 here

me!

#9 RyanSmith345

RyanSmith345
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationFort Collins, CO

Posted 29 September 2006 - 08:59 PM

EXCELLENT!  I think this is exactly what I was looking for.  Thanks.

#10 RyanSmith345

RyanSmith345
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationFort Collins, CO

Posted 29 September 2006 - 09:45 PM

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 .pdf

This 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.

#11 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 29 September 2006 - 10:36 PM

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 would
2. 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!

<?

// 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!

?>

nice and simple...

updated to add strtolower() for $type

me!

#12 RyanSmith345

RyanSmith345
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationFort Collins, CO

Posted 30 September 2006 - 06:06 PM

Unbeliveable!

These forums are great.  Thanks for all your help. That would have taken me forever to figure out.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users