tvercoe Posted March 5, 2015 Share Posted March 5, 2015 please can tel me the code to check against a log file for allready entered ip address below is the code i have to log the ip<?php$iplogfile = '../logs/ip-address-contactform.txt';$ipaddress = $_SERVER['REMOTE_ADDR'];$webpage = $_SERVER['SCRIPT_NAME'];$timestamp = date('m/d/Y h:i:s');$browser = $_SERVER['HTTP_USER_AGENT'];$fp = fopen($iplogfile, 'a+');chmod($iplogfile, 0777);fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n");fclose($fp);?> Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/ Share on other sites More sharing options...
joel24 Posted March 5, 2015 Share Posted March 5, 2015 you'll want to do a file_get_contents and then search through the entire file with strpos which is a lot less efficient than if you were to store the IP address in the database. Why do you want to know if they've been there before? Set a cookie if it's not too important... what if someone is at a university and has a shared IP address? Setup a database table, add the email address and other details and do a SELECT query to see if that IP address, or email exists. Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507605 Share on other sites More sharing options...
tvercoe Posted March 5, 2015 Author Share Posted March 5, 2015 because i get aalot of bots/spammer/hackers i want to be able to log their ip's (Done) now have to find sum code to look into logs and not log ip if its there (noob so done know much) i cant find code anywhere for this people tell me it easy but yet they cant supply code either can someone supply the code needs be every lil bit of cade as no point in just giving parts to noobs and would like the best way at the mo i can only log to files as dont know how create the database dont know what any of it means (noob) Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507712 Share on other sites More sharing options...
CroNiX Posted March 5, 2015 Share Posted March 5, 2015 <?php $iplogfile = '../logs/ip-address-contactform.txt'; $ipaddress = $_SERVER['REMOTE_ADDR']; //load the file $file = file_get_contents($iplogfile); //check to see if the ipaddress is already in the file if ( ! preg_match("/$ipaddress/", $file )) { //nope, log it! $webpage = $_SERVER['SCRIPT_NAME']; $timestamp = date('m/d/Y h:i:s'); $browser = $_SERVER['HTTP_USER_AGENT']; $fp = fopen($iplogfile, 'a+'); chmod($iplogfile, 0777); fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n"); fclose($fp); } ?> Haven't tested it Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507720 Share on other sites More sharing options...
tvercoe Posted March 6, 2015 Author Share Posted March 6, 2015 my log exist at www.nzquakes.maori.nz/logs/ip-address-mainsite.txt & www.nzquakes.maori.nz/logs/ip-address-contactform.txt they are there but no work is my code wronge? PHP Error MessageWarning: file_get_contents(../logs/ip-address-contactform.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/a1582367/public_html/index.html on line 52Free Web Hosting PHP Error MessageWarning: fopen(../logs/ip-address-contactform.txt) [function.fopen]: failed to open stream: No such file or directory in /home/a1582367/public_html/index.html on line 60Free Web Hosting PHP Error MessageWarning: chmod() [function.chmod]: No such file or directory in /home/a1582367/public_html/index.html on line 61Free Web Hosting PHP Error MessageWarning: fwrite(): supplied argument is not a valid stream resource in /home/a1582367/public_html/index.html on line 62Free Web Hosting PHP Error MessageWarning: fclose(): supplied argument is not a valid stream resource in /home/a1582367/public_html/index.html on line 63Free Web Hosting Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507724 Share on other sites More sharing options...
CroNiX Posted March 6, 2015 Share Posted March 6, 2015 I don't know what the path to the file is. I used what you wrote. Try using a full path Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507725 Share on other sites More sharing options...
tvercoe Posted March 6, 2015 Author Share Posted March 6, 2015 i know i have change order of code around maybe but the coding seems right-ish <!DOCTYPE html> <html> <head> <meta name="author" content="evilbudz"> <html lang="en-NZ"> <meta charset="UTF-8"> <meta name="keywords" content="NZ Quakes,nz quakes,Earthquake,earthquake,Christchurch,christchurch,Shake,shake,Quakes,quakes"> <meta name="msapplication-TileColor" content="#ffffff"> <meta name="msapplication-TileImage" content="/ms-icon-144x144.png"> <meta name="theme-color" content="#ffffff"> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta name="NZ Quakes, nz quakes, Earthquake, earthquake, Christchurch, christchurch, Shake, shake" content="NZ Quakes - Earthquake Info For New Zealand"> <link rel="manifest" href="/manifest.json"> <link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png"> <link rel="apple-touch-icon" sizes="60x60" href="apple-icon-60x60.png"> <link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png"> <link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png"> <link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png"> <link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png"> <link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png"> <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png"> <link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png"> <link rel="icon" type="image/png" sizes="192x192" href="/android-icon-192x192.png"> <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"> <link rel="icon" type="image/png" sizes="96x96" href="/favicon-96x96.png"> <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"> <!-- Below Code Logs ONLY User's IP Address & Time Stamp & Browser Info To logs/ip-address-mainsite.txt --> <?php $iplogfile = 'logs/ip-address-mainsite.txt'; $ipaddress = $_SERVER['REMOTE_ADDR']; $webpage = $_SERVER['SCRIPT_NAME']; $timestamp = date('d/m/Y h:i:s'); $browser = $_SERVER['HTTP_USER_AGENT']; $fp = fopen($iplogfile, 'a+'); chmod($iplogfile, 0777); fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n"); fclose($fp); $iplogfile = '../logs/ip-address-contactform.txt'; $ipaddress = $_SERVER['REMOTE_ADDR']; //load the file $file = file_get_contents($iplogfile); //check to see if the ipaddress is already in the file if ( ! preg_match("/$ipaddress/", $file )) { //nope, log it! $webpage = $_SERVER['SCRIPT_NAME']; $timestamp = date('d/m/Y h:i:s'); $browser = $_SERVER['HTTP_USER_AGENT']; $fp = fopen($iplogfile, 'a+'); chmod($iplogfile, 0777); fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n"); fclose($fp); } ?> <title>Welcome To - NZ Quakes</title> </head> </body> <frameset rows="17%,*,8%"> <frame src="nzquakes_frame_a.html" noresize="noresize"> <frame src="nzquakes_frame_b.html" noresize="noresize"> <frame src="nzquakes_frame_c.html" noresize="noresize"> </frameset> </html> Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507727 Share on other sites More sharing options...
tvercoe Posted March 6, 2015 Author Share Posted March 6, 2015 ok i chaged it to point to http://www.nzquakes.maori.nz/ip-address-mainsite.txt and same for other one now i just get these 2 messages, they are to do with the chmod as i can see both log files and directory are chmod 777 PHP Error MessageWarning: chmod() [function.chmod]: No such file or directory in /home/a1582367/public_html/index.html on line 42Free Web Hosting PHP Error MessageWarning: chmod() [function.chmod]: No such file or directory in /home/a1582367/public_html/index.html on line 59Free Web Hosting Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507728 Share on other sites More sharing options...
tvercoe Posted March 6, 2015 Author Share Posted March 6, 2015 chmod($iplogfile, 0777); seems to be the problem keeps saying error line 49 thats whats on line 49 Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507730 Share on other sites More sharing options...
tvercoe Posted March 6, 2015 Author Share Posted March 6, 2015 here's what i got now seems to work when take out chmod <!DOCTYPE html> <html> <head> <meta name="author" content="evilbudz"> <html lang="en-NZ"> <meta charset="UTF-8"> <meta name="keywords" content="NZ Quakes,nz quakes,Earthquake,earthquake,Christchurch,christchurch,Shake,shake,Quakes,quakes"> <meta name="msapplication-TileColor" content="#ffffff"> <meta name="msapplication-TileImage" content="/ms-icon-144x144.png"> <meta name="theme-color" content="#ffffff"> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta name="NZ Quakes, nz quakes, Earthquake, earthquake, Christchurch, christchurch, Shake, shake" content="NZ Quakes - Earthquake Info For New Zealand"> <link rel="manifest" href="/manifest.json"> <link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png"> <link rel="apple-touch-icon" sizes="60x60" href="apple-icon-60x60.png"> <link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png"> <link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png"> <link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png"> <link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png"> <link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png"> <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png"> <link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png"> <link rel="icon" type="image/png" sizes="192x192" href="/android-icon-192x192.png"> <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"> <link rel="icon" type="image/png" sizes="96x96" href="/favicon-96x96.png"> <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"> <!-- Below Code Logs ONLY User's IP Address & Time Stamp & Browser Info To logs/ip-address-mainsite.txt --> <?php $iplogfile = 'http://www.nzquakes.maori.nz/logs/ip-address-mainsite.txt'; $ipaddress = $_SERVER['REMOTE_ADDR']; //load the file $file = file_get_contents($iplogfile); //check to see if the ipaddress is already in the file if ( ! preg_match("/$ipaddress/", $file )) { //nope, log it! $webpage = $_SERVER['SCRIPT_NAME']; $timestamp = date('d/m/Y h:i:s'); $browser = $_SERVER['HTTP_USER_AGENT']; $fp = fopen($iplogfile, 'a+'); fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n"); fclose($fp); } ?> <title>Welcome To - NZ Quakes</title> </head> </body> <frameset rows="17%,*,8%"> <frame src="nzquakes_frame_a.html" noresize="noresize"> <frame src="nzquakes_frame_b.html" noresize="noresize"> <frame src="nzquakes_frame_c.html" noresize="noresize"> </frameset> </html> Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507731 Share on other sites More sharing options...
CroNiX Posted March 6, 2015 Share Posted March 6, 2015 Yeah, you can't do a chmod() on a URL. It has to be a filepath. Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507732 Share on other sites More sharing options...
tvercoe Posted March 6, 2015 Author Share Posted March 6, 2015 this is what i ended up using as dont know what code is to chmod so have to manually make file ip-address-mainsite.txt and its chmod is 0777 it works mint thank you heaps <!-- Below Code Logs ONLY User's IP Address & Time Stamp & Browser Info To logs/ip-address-mainsite.txt --> <?php $iplogfile = 'logs/ip-address-mainsite.txt'; $ipaddress = $_SERVER['REMOTE_ADDR']; //load the file $file = file_get_contents($iplogfile); //check to see if the ipaddress is already in the file if ( ! preg_match("/$ipaddress/", $file )) { //nope, log it! $webpage = $_SERVER['SCRIPT_NAME']; $timestamp = date('d/m/Y h:i:s'); $browser = $_SERVER['HTTP_USER_AGENT']; $fp = fopen($iplogfile, 'a+'); fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n"); fclose($fp); } ?> Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507735 Share on other sites More sharing options...
tvercoe Posted March 6, 2015 Author Share Posted March 6, 2015 this was for my site www.nzquakes.maori.nz thank you for the help Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507737 Share on other sites More sharing options...
joel24 Posted March 8, 2015 Share Posted March 8, 2015 you should just use a captcha field to stop spammers, https://developers.google.com/recaptcha/old/docs/php Link to comment https://forums.phpfreaks.com/topic/295113-need-help-with-codeing-to-check-against-already-logged-ip-address-nin-a-log-file/#findComment-1507907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.