Solved

PHP - Writing to a File Issue.

Posted on 2003-10-29
10
288 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
Comment Utility
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
Comment Utility
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
Comment Utility
have you done chmod 0777 on the folder the file's being placed in?
0
 
LVL 13

Expert Comment

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

Author Comment

by:NeoTek
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:NeoTek
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you, all of your comments help me out.

NeoTek
0
 
LVL 6

Expert Comment

by:aolXFT
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now