PHP - Writing to a File Issue.


I am running a php script located at  What this does is basically logs users information (IP address, User Agent, and Date) and writes it to a database.  For some reason when I access this page, the information is not written on the first visit, I must refresh the page for the data to show up in the database.  If I open a new browser window and input the URL, the same thing happens.  Here is my php code:

$filename = "data.txt";
$useragent = $_SERVER['HTTP_USER_AGENT'];
$date = date("F j, Y, g:i a");

$fh = fopen($filename, "a") or die("Could not open file!");

$data = $ip . "|" . $useragent . "|" . $date . "\n";

fwrite($fh , $data);


I am not sure what I am doing wrong.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I tested your code on my server and it works fine for me.  The only problem I encountered was making sure that the file data.txt is world-writeable.
NeoTekAuthor Commented:

I still don't believe it is working.  If you visit...

Then if you go to..

Nothing will change, unless while on the index.php4 you refresh the page.

have you done chmod 0777 on the folder the file's being placed in?
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

im probably being thick so feel free to ignore me
NeoTekAuthor Commented:

CHMOD is not an issue because I can write to the file, but I need to refresh the page to do so.

NeoTekAuthor Commented:

I think this is just an issue from my browser because I tried it from another computer and it worked fine.  Anyone know why that is?  Also, is there a way to call a php function from a button click?

I think it may be because the page is cached on your local machine. When you browse to the page, it stores the page in cache. When you refresh it tells the server to send a new copy.

Try sending some anti-caching headers(find them on google, I can't think of them off hand). They include something like Pragma: no-cache, and Expires : (insert_early_date_here), or something like that.

PHP is processed on the server, and the php process is finished by the time the page gets to the script. The only thing you can really do, in something like use a form button, like

<form action="page.php"><input type="hidden" name="function_onClick"  value="name_of_function"><input type="submit" value="Caption"></form>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PHP cannot be run on a page once it is loaded. For that functionality you need either a client side script or spawn a new page where the function is.

That's the only way I know, if its changed, its news to me.

NeoTekAuthor Commented:
Thank you, all of your comments help me out.

Thanks for accepting the answer, but out of curiosity, could you please tell me if the page was actually cached on the Browser?

I'd also like to point that the ability to call arbitary functions, from an onClick type thing, would be a massive security headache.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.