Solved

PHP - Writing to a File Issue.

Posted on 2003-10-29
10
296 Views
Last Modified: 2006-11-17
Hi,

I am running a php script located at http://www.airsep.com/test/index.php4.  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";
$ip = $_SERVER['REMOTE_ADDR'];
$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);

fclose($fh);

I am not sure what I am doing wrong.

NeoTek
0
Comment
Question by:NeoTek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:rstorey2079
ID: 9643959
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.
0
 
LVL 1

Author Comment

by:NeoTek
ID: 9644110
Hi,

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

http://www.airsep.com/test/index.php4

Then if you go to..

http://www.airsep.com/test/view.php4

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

NeoTek
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9644334
have you done chmod 0777 on the folder the file's being placed in?
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 13

Expert Comment

by:lozloz
ID: 9644372
im probably being thick so feel free to ignore me
0
 
LVL 1

Author Comment

by:NeoTek
ID: 9644759
Hi,

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

Ryan
0
 
LVL 1

Author Comment

by:NeoTek
ID: 9644801
Hi,

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?

Ryan
0
 
LVL 6

Accepted Solution

by:
aolXFT earned 100 total points
ID: 9645643
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>
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9647548
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.

Red010Knight
0
 
LVL 1

Author Comment

by:NeoTek
ID: 9653349
Thank you, all of your comments help me out.

NeoTek
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9659782
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.
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question