jnerotrix Posted November 30, 2008 Share Posted November 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/ Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 Use an IF statement to check for there ip in the file first. Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702383 Share on other sites More sharing options...
dezkit Posted November 30, 2008 Share Posted November 30, 2008 Why not just use a database? Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702387 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702388 Share on other sites More sharing options...
PFMaBiSmAd Posted November 30, 2008 Share Posted November 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702389 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 the (FREE) host i am using doesnt support mysql for free users but is it possible to do what i ask with a text file Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702394 Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 Yes but if the text file contains a lot of results then it will be slow and cause slow loading times for users. Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702396 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702400 Share on other sites More sharing options...
dezkit Posted November 30, 2008 Share Posted November 30, 2008 Can you tell us the layout of the ip.txt? is it just ip ip ip ? Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702402 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 yea but it actually shows up like this 76.118.110.255 <br> Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702403 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702406 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 would the <br> conflict with the code for checking for duplicate ip's Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702415 Share on other sites More sharing options...
dezkit Posted November 30, 2008 Share Posted November 30, 2008 So it is like 76.118.110.255 <br> 76.118.110.255 <br> 76.118.110.255 <br> 76.118.110.255 <br> ? Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702418 Share on other sites More sharing options...
PFMaBiSmAd Posted November 30, 2008 Share Posted November 30, 2008 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702419 Share on other sites More sharing options...
dezkit Posted November 30, 2008 Share Posted November 30, 2008 if yes, just replace the spaces and the <br> with a comma. edit: use pfmab's code! Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702422 Share on other sites More sharing options...
DSGameMaker Posted November 30, 2008 Share Posted November 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702423 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702424 Share on other sites More sharing options...
PFMaBiSmAd Posted November 30, 2008 Share Posted November 30, 2008 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702428 Share on other sites More sharing options...
jnerotrix Posted November 30, 2008 Author Share Posted November 30, 2008 Nice Everything is fully Functional Thanks Guys Quote Link to comment https://forums.phpfreaks.com/topic/134888-solved-need-simple-php-coding-help/#findComment-702439 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.