Solved

PHP - Writing to a File Issue.

Posted on 2003-10-29
10
293 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Search Filter 2 71
How to remove a section of the main menu from Magento 1.9.3 3 30
How do I speed up this PDO query 4 15
php output utf-8 problem 6 24
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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