Forum Moderators: coopster

Message Too Old, No Replies

php value auto prepend file - write to file

how to write into flat file via auto_prepend_file

         

smallcompany

7:43 am on Nov 19, 2008 (gmt 0)

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



I run a PHP script via .htaccess in order to get variables and write them to a cookie.

I use simple two lines in .htaccess

php_value include_path
php_value auto_prepend_file

The script works fine.

I wanted to improve it by adding a few lines that would write some of the data into a flat file. The testing code is this:

$fp = fopen("log.txt", "a");
$test = "test";
fwrite($fp, $test);
fclose($fp);

I have CHMOD 777 on log.txt. If I run this as a separate PHP file, just by typing it into my browser’s address bar, it works fine. If I include it or put it straight into my PHP script that loads via .htaccess, nothing happens.

I wonder about difference between auto_prepend_file and regular run of any PHP file.
.htaccess is set to parse HTML as PHP and all of my pages are static HTML.

My test tells me the problem is not about code itself but how certain PHP functions work.

The bottom question is: can you write to a file straight from PHP script being setup in this way?

Thanks

willis1480

2:54 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



Nevermind...one last edit here.

It is working fine for me...I had my .htaccess for parsing html pages as php pages.

PHP ran as module:
AddType application/x-httpd-php .html .htm

PHP ran as CGI:
AddHandler application/x-httpd-php .html .htm

Then everything worked fine for me.

eelixduppy

3:07 pm on Nov 19, 2008 (gmt 0)



The directives you are setting in your .htaccess file are not correct. Try something like this:

php_value include_path ".:/path/to/includes"
php_value auto_prepend_file "/path/to/file.php"

Note the "." in the include path. This allow relative includes.

smallcompany

8:27 pm on Nov 19, 2008 (gmt 0)

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



Thanks.

The directives you are setting in your .htaccess file are not correct.

The way how I have them works fine for all what the script does, except the new part that should write to a file.

Nevertheless, I did per your suggestion, and saw no change. Script still works fine, but my test part does not write into the file.

Finally, I run my script just as any page, from within the browser, and writing worked fine.

It is something about that PREPEND and writing to a file. But what? Some condition for that writing part is missing.

eelixduppy

8:35 pm on Nov 19, 2008 (gmt 0)



I don't know off the top of my head. Take a look at any errors PHP may be giving you.

willis1480

10:40 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



check if it works when you look at a *.php page. If it works for PHP pages, then the .htaccess prepend is working.

Then make sure you are using the correct directive for your server for evaluating htm, html, shtml, etc. as PHP files.

Otherwise the server will drop the php stuff that it is supose to auto-prepend.

goodluck...I tried your exact code and got it working for my server so it is very possible.

Here is my .htaccess code(work for php, html, and htm pages):

AddType application/x-httpd-php .html .htm
php_value auto_prepend_file test.php

smallcompany

6:11 am on Nov 20, 2008 (gmt 0)

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



It was the path to which the script was writing! I kept looking into the folder where script was residing while the log file was being written into the root of the site.

Typical for a guy without right and basic coding manners.

Thanks for inputs and for simulating the case.