Solved

PHP - Writing to a File Issue.

Posted on 2003-10-29
10
291 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 …

810 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