Jump to content

Need help with codeing to check against already logged ip address nin a log file


tvercoe
Go to solution Solved by CroNiX,

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

  • Solution
<?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
Share on other sites

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 Message
Warning: 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 52


PHP Error Message
Warning: 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 60


PHP Error Message
Warning: chmod() [function.chmod]: No such file or directory in /home/a1582367/public_html/index.html on line 61


PHP Error Message
Warning: fwrite(): supplied argument is not a valid stream resource in /home/a1582367/public_html/index.html on line 62


PHP Error Message
Warning: fclose(): supplied argument is not a valid stream resource in /home/a1582367/public_html/index.html on line 63

Edited by tvercoe
Link to comment
Share on other sites

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>

Edited by tvercoe
Link to comment
Share on other sites

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 Message
Warning: chmod() [function.chmod]: No such file or directory in /home/a1582367/public_html/index.html on line 42


PHP Error Message
Warning: chmod() [function.chmod]: No such file or directory in /home/a1582367/public_html/index.html on line 59

Edited by tvercoe
Link to comment
Share on other sites

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
$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
Share on other sites

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);
}
?>
Edited by tvercoe
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.