Jump to content

PHP File Writing Help


Papa-Beans

Recommended Posts

Hello! 

 

  l was wondering if you might be able to help me. l am trying to find out how to make PHP read a file, and then check if that text is already there, and if so don't re-write it. l'll give out my code for an example.

<?php
//text file that counts the peeps that visit the site
 $lne = "line.txt";
 $File = "counter.txt"; 


 $handle = fopen($File, 'r+') ; 
//file open

 $data = fread($handle, 512) ; 
 //file read

 $count = $data + 1;
 //Adds one person :DDDDD
 
 fseek($handle, 0) ; 

 fwrite($handle, $count) ; 
 //saves the new person

echo 'Count #'.$count;

//
//
//
//
//
//


$File2 = "ips.txt";
fclose($handle) ; 
if (isset($_POST['ip'])) {
	$ip = $_POST['ip'];
	$port = $_POST['port'];
	echo 'Connecting to ' . $ip . ':' . $port;
	exit();
}

$ip = getenv("REMOTE_ADDR");
echo '<br>'.$ip;

 $handle2 = fopen($File2, 'r+') ; 
//file open

 $data2 = fread($handle2, 512) ; 
 //file read

fwrite($handle2,"\n" . $ip .':' . $count);
 //saves the new ip


?>

<html>
<form method="POST">
<input type="hidden"  name="ip">
<input type="hidden" name="port">
</html> 

lt just re-writes the lP with the new counter, very annoying. l can filter it out with VB but l want it all to be server sided. Thank you for your time ~Papa Beans

Link to comment
Share on other sites

It sounds like you are asking how to not write the visitor's IP Address to the second file if it is already there. This would be a simple matter except for the fact that you are appending the counter to the IP address, why is that?

 

Anyway, without the counter added, it would go something like this:

$ip = $_SERVER["REMOTE_ADDR"];

if (file_exists($File2)) $ipList = file($File2, FILE_IGNORE_NEW_LINES);
else $ipList = array();

if (!in_array($ip, $ipList)) {
  file_put_contents($File2, $ip . "\n", FILE_APPEND | LOCK_EX);
}
If you really need/want the :$counter appended to the IP Address, you will have to walk the array ($ipList) and compare the IP address part with the user's IP address. If you get through the entire array and don't find the user's IP, then you would add it.

 

Note: the code above requires PHP 5.0 (for some of the flags).

 

Note: Using file storage like this is not best practice. You are single-threading through the file, which means if multiple users hit the code at the same time, they will have to wait for the others to complete. Also, if two users from the same IP address read the file at the same time, and neither finds their IP in the file they will BOTH add it, causing duplication. You can attempt to use fopen, fread, fwrite, and fclose, to keep the file locked, but again that leads to delays for the user. This type of thing is best done with a database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.