Jump to content

Recommended Posts

ok when ever some1 visits my website it logs there ip to a textfile

 

but is there a way to make it so the same ip doesnt get logged twice

 

Like say you went to this page and it logs your ip i want it so if you click refresh or go back to the page it wont log your ip again because it is already there

 

heres the codes so far on the website

 

index.php (When you enter this page it writes your ip to the "ip.txt")

ip.txt (ip's are logged here)

 

I want it so when you enter index.php multiple times it doesnt log your ip more than once like it

 

reads ip.txt and checks if your ip is already in there and if it is it just doesnt log it

 

Thanks,

Jnerotrix

Link to comment
https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/
Share on other sites

umm im not that good with php the if commands confuse me

 

this is what it does right now

 

 

How do i make it check if ip is already in there before it writes it

 

index.php

<?php

$ip = $_SERVER['REMOTE_ADDR'];

$myFile = "ip.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$ip  <br> \r\n";
fwrite($fh, $stringData);
fclose($fh);

?>

 

You would need to write some slow parsed/tokenized/interpreted php code to scan through the file and test all the values.

 

Is there some reason you are not using a database for this? The code to do the scanning and comparison is already done in the database engine and it is complied so it executes at least 100 times faster than php code performing the same task.

well thats ok i will deal with that problem later but does any1 know the code to do this that can attach it to this

 

<?php

$ip = $_SERVER['REMOTE_ADDR'];

$myFile = "ip.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$ip  <br> \r\n";
fwrite($fh, $stringData);
fclose($fh);

?>

The <br> is there because i have another file called:

 

view.php (Where i Can View The Logged Ip)

Here are the Contents

<h1> Logs </h1>

<?php

$myFile = "ip.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;


?>

would the <br> conflict with the code for checking for duplicate ip's

Yes and it would slow down the code (to log the ip addresses.) Only add HTML formatting if/when you output the information.

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];

$myFile = "ip.txt";

$lines = file($myFile); //get the lines into an array
$lines = array_map('trim',$lines); // remove newlines

if(!in_array($ip,$lines)){
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$ip\r\n";
fwrite($fh, $stringData);
fclose($fh);
}
?>

I wouldn't store your stuff like that.

 

I store Song Lyrics in my database like this:

 

A-ha

A-ha

A-ha

 

But when I display them, they come up like:

 

A-haA-haA-ha

 

To fix it:

 

$content = str_replace("\n","<br />",$ahas);
echo $content;

 

Replaces all instances of the newlines with displayed BR's.

No it comes up like this

 

76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 
76.118.110.255  <br> 

 

Because of the

"\r\n"

in the code makes it skip a line in the text file

There is a function to do that - http://us3.php.net/manual/en/function.nl2br.php

 

Edit: Here is code to display the contents of the file that goes along with the code I posted above that logs new IP addresses into the file -

<?php
$myFile = "ip.txt";
echo nl2br(file_get_contents($myFile));
?>

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.