Write file on php

你说的曾经没有我的故事 提交于 2019-12-02 11:16:02

问题


I want to keep ips from visitors and place them on a file.
I tried fwrite() function but I think it is rewrite on the previus ip on file.

Example.

ip.txt is empty.

when I run the write.php Script, on ip.txt I have x.x.x.x ip (my ip)

If My friend runs the write.php Script, on ip.txt I have a.a.a.a ip (friend's ip only)

where is my ip? I want to have on ip.txt file the following:

x.x.x.x   ip1  
a.a.a.a   ip2

Code on write.php is the following.

<?php
$file = fopen("ip.txt","w");
$ip=$_SERVER['REMOTE_ADDR'];
echo fwrite($file,$ip);
fclose($file);
?> 

回答1:


<?php
$file = fopen("ip.txt","a");
$ip=$_SERVER['REMOTE_ADDR'];
echo fwrite($file,$ip);
fclose($file);
?> 

Look at the manual

Check what the 2nd parameter means.

Youve chosen w mode which is an overrwrite mode. Try a mode instead (append)




回答2:


Change "w" to "a"

W means write (over), a means append.




回答3:


The 'advantage' of the database version is that no one can view the data. If neccesary, you can avoid access to the file by using a .htaccess file:

For apache 2.2

# Protect log.txt
<Files ./inscription/log.txt>
Order Allow,Deny
Deny from all
</Files>

For apache 2.4

# Protect log.txt
<Files ./inscription/log.txt>
Require all denied
</Files>


来源:https://stackoverflow.com/questions/14087789/write-file-on-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!