Forum Moderators: coopster

Message Too Old, No Replies

run PHP script from htaccess

         

smallcompany

11:19 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Once you get to our site and browse through it, all what we care about is what has brought you to the site.
Based on the above, if we want to write a cookie that will keep variables that were derived from the referring URL, we need to do that only once per session, at the first site access attempt.
Now, since the first access can be through any page of the site, and we do that cookie exercise in PHP, we have to call PHP script by include at the very top of HTML every page.

After all, since we have no need to parse HTML as PHP except to run this script, I wonder if we could make a call to that PHP script from .htaccess. That would leave our HTML with no PHP inside, plus no dirty URLs.

Thanks

topr8

11:24 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you cannot call a php script from htaccess

topr8

11:26 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



although i do wonder ...

>>After all, since we have no need to parse HTML as PHP except to run this script.

then why are you writing a cookie? if you are never going to use the info in it?

smallcompany

11:34 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you click onto outgoing link, you call PHP script that reads a cookie and applies variables to affiliate links.

topr8

12:20 am on May 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



oh, that makes sense

smallcompany

2:31 am on May 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So, is it possible to run it that way (straight from .htaccess)?

topr8

7:54 am on May 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>>you CANNOT call a php script from htaccess

sonjay

11:19 am on May 29, 2008 (gmt 0)

10+ Year Member



Actually, you can, by using
php_value auto_prepend_file "/path/to/file.php"

I frequently use this method to include a master config file, which in turn includes a database connection file and a class/functions file.

smallcompany

5:32 pm on May 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually, you can, by using
php_value auto_prepend_file "/path/to/file.php

Thanks so much. Is it that simple, or it requires more than that line? Do you do any changes on the server level or you can use it on shared hosting?

I wonder why this method is so unknown. Search on Google always gives "parsing HTML as PHP".

There are so many cases where you want to load something once only.

sonjay

3:13 am on May 30, 2008 (gmt 0)

10+ Year Member



Yeah, it's that simple. That directive will call the specified file before the requested file. If it's a php script, it will run the script. If it's a header, it'll put the header at the top of the page. And so forth.

There's a similar directive to automatically.... um... "append" a file? I disremember the name or syntax, but it calls the specified file after the initial request is processed. The two together are sometimes used to include a header and footer on every page.

topr8

7:09 am on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hmm, excellent, glad you got an answer - i never knew that!

smallcompany

10:24 am on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not that easy though. Configuration through .htaccess gave 500 Internal Server Error.
Hosting company advised to rather use php.ini. When I did that, I got "failed to open stream: No such file or directory in Unknown on line 0" no matter which path I try.

Now, about this directive in general, would you expect it to work on a load of static HTML files or PHP only?

sonjay

11:08 am on May 30, 2008 (gmt 0)

10+ Year Member



Oh, I forgot about hosts using safe mode or whatever they call it, where you have to put php directives in a php.ini file.

It should work in php.ini. There's more info in the php manual [us.php.net]

jianliangwang

11:54 am on May 31, 2008 (gmt 0)

10+ Year Member



Try,

REWRITE

chorny

2:35 pm on Jun 2, 2008 (gmt 0)

10+ Year Member



Use 1x1 image or mod_perl handler

smallcompany

4:20 pm on Jun 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It finally worked from within php.ini. First time I tried it, I couldn’t get it work because of path issue. Shared hosting package does not show you the real path.

Anyhow, I resolved that, but the nature of PHP cookie (not written on first page access) is something that creates an obstacle for me. I need cookie to be loaded right away. Here is the post:

[webmasterworld.com...]