Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP - Writing to a File Issue.

Posted on 2003-10-29
10
Medium Priority
?
298 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 400 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

Independent Software Vendors: 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

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 article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

704 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