Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fopen fwrite help! 1

Status
Not open for further replies.

Ante0

Programmer
Apr 19, 2007
98
SE
I need some help with this...
Well, I'm currently just testing some things with writing and reading files on my website, so far it's been good.
But I'm kinda stuck right now.

Here's my code:

<?php
$ipi = getenv("REMOTE_ADDR");
$counter = "test.txt";
$fd = fopen($counter, "rw");
$oldipi = fread($fd, filesize( $counter ));
$useripi = $ipi;
fwrite($fd, $oldipi . "\n" . $useripi);
fclose($fd);
?>

This started as a counter, first it used fopen($counter, "r") and "w" to first read the file then to add new values using $num = $num + 1;
but I'm stuck right now.
It wont read the file then write the new info on a new line with the old info still in it.
How would I go by to accomplish this?
 
Code:
$file = 'test.txt';

$ips = readCounter();

if (!in_array($_SERVER['REMOTE_ADDR'], $ips)){
  $ips[] = $_SERVER['REMOTE_ADDR'];
  writeFile($ips);
}

function readFile(){
 $ip = array();
 global $file;
 $c = file_get_contents($file);
 $pattern = '/(\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})/ms';
 preg_match_all($pattern, $c, $matches);
 if (!empty($matches)) {
   foreach ($matches as $match){
     $ip[] = $match[1];
   }
 }
 return $ip;
}

function writeFile($data){
 global $file;
 file_put_contents($file, implode('\r\n', $data));
}

you don't really need the preg_match but it's a protection against the file being garbled inadvertently.
 
Code:
$file = 'test.txt';

$ips = readCounter();

if (!in_array($_SERVER['REMOTE_ADDR'], $ips)){
  $ips[] = $_SERVER['REMOTE_ADDR'];
  writeFile($ips);
}

function readFile(){
 $ip = array();
 global $file;
 $c = file_get_contents($file);
 $pattern = '/(\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})/ms';
 preg_match_all($pattern, $c, $matches);
 if (!empty($matches)) {
   foreach ($matches as $match){
     $ip[] = $match[1];
   }
 }
 return $ip;
}

function writeFile($data){
 global $file;
 file_put_contents($file, implode('\r\n', $data));
}

Tried, and did not work.
When you use function readFile() it gives an error:
Fatal error: Cannot redeclare readfile() in /home/ante0co/public_html/test/test.php on line 23

So I changed it to readCounter()
that doesn't give an error but all it does is add \n\r\n\r... and the current IP.
So it doesn't add any new one :(
I also tried removing the If statement at the beginning, but that does not add anything either.
i'll keep on tweaking it though :)
 
the two references should be to readfile(). apologies.

it will only add a new IP address if the IP address is not already in the list. i.e. the IP addresses are a unique list. if you do not want a unique list, but in fact a list of every visit, then change the global scope as follows
Code:
$ips = readFile();
$ips[] = $_SERVER['REMOTE_ADDR'];
writeFile($ips);

and please change the writefile function as follows (change the single quotes to double quotes).

Code:
function writeFile($data){
 global $file;
 file_put_contents($file, implode([red][s]'[/s]"[/red]\r\n[red][s]'[/s]"[/red], $data));
}
 
I actually tried changing both to readFile()
but it still gives me the error:
Fatal error: Cannot redeclare readfile() in /home/ante0co/public_html/test/test.php on line 21

And I also removed the IF as you posted, but it's not making an array of IP's.

the thing it's complaining about is the } after return $ips;

I'll try some more tomorrow, thanks for your help :)
 
oops. sorry again. readfile is a built in function. please rename the function as you wish.
 
Changed both of the function names, did exactly as you said.
Still only saves the last IP :(
Could it be something that's not enabled in my php.ini?
I don't have direct access to it, as I use a webhost, but I'm sure I could ask them to enable it :)

Or if you could think of any other way to save multiple entries to a txt.
I wll try some more later, maybe adding a String that will keep the current ip then just add the new one.
But I need to figure out how to read a file and save that to a string :p
 
there may be a problem with the format of the IP address. for example, on my local server, the ip address is reported from the SERVER super global as ::1 (clearly incorrect). this code checks for badly formatted IP addresses and replaces it with a localhost equivalent.

Code:
<?php
$file = 'test.txt';

$ips = readData();
$ips[] = preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})/ms', $_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
writeData($ips);


function readData(){
 $ip = array();
 global $file;
 if (file_exists($file)){
	 $c = file_get_contents($file);
	 $pattern = '/(\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})/ms';
	 preg_match_all($pattern, $c, $matches);
	 if (!empty($matches[1])) {
	   $ip = $matches[1];
	 }
 }
 return $ip;
}

function writeData($data){
 global $file;
 file_put_contents($file, implode("\r\n", $data));
}
?>

I can confirm that this code is working fine on my system.
 
Yay, thank you!
That works just as I wanted it.
My knowledge has improved some now :)
A star for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top