Jump to content


Photo

File reading question


  • Please log in to reply
26 replies to this topic

#1 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 03:53 PM

I am running Apache 2.2 on Windows with PHP 5.1.1.

I am trying to show lines of a file based on the beginning
part of the line, and the user's IP. For example if a user's IP is 192.168.1.1 and the log contains

   
<?php
    // DO NOT DELETE THIS
    include 'phpsimplechoose_config.php';
    //Below is where the verification takes place. Try to play around with it.
    if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="Please enter User/Password"');
    header('HTTP/1.0 401 Unauthorized');
    die;

    } else {

    if (($_SERVER['PHP_AUTH_USER'] !== $phpsc_username) || ($_SERVER['PHP_AUTH_PW'] !== $phpsc_password)) {
    header('WWW-Authenticate: Basic realm="Incorrect! Please try again."');
    header('HTTP/1.0 401 Unauthorized');
    die;
    }
    }
    // To delete log erase everything after the next line, but not the next line itself (Line 20 and down can be deleted)
    ?>
    192.168.1.1: Fri July 21, 2006 18:47 : Choice 1.   test  Choice 2.   tghsdfg  We say... test <br />
    207.144.154.140: Fri July 21, 2006 18:48 : Choice 1.   me  Choice 2.   you  We say... you <br />
    207.144.154.140: Fri July 21, 2006 19:00 : Choice 1.   you  Choice 2.   me  We say... me <br />
    207.144.154.140: Fri July 21, 2006 19:01 : Choice 1.   you  Choice 2.   me  Choice 3.   thee  We say... me <br />
    207.144.154.140: Fri July 21, 2006 19:03 : Choice 1.   you  Choice 2.   me  Choice 3.   thee  We say... thee <br />
    192.168.1.1: Fri July 21, 2006 19:26 : Choice 1.   fsgdf  Choice 2.   gdfgsdf  We say... gdfgsdf <br />
    192.168.1.1: Fri July 21, 2006 19:26 : Choice 1.   fghdfgh  Choice 2.   fghfdgh  We say... fghfdgh <br />



I want the script to display all the lines that start with the user's IP. The beginning is just security for the file, I hope that won't mess it up.

Thanks

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 04:33 PM

I would store your IP addresses in a seperate log file, called iplog.php, The use this code to show the log for a particular IP Address:
<?php

// DO NOT DELETE THIS
include 'phpsimplechoose_config.php';

// Below is where the verification takes place. Try to play around with it.
if (!isset($_SERVER['PHP_AUTH_USER']))
{
    header('WWW-Authenticate: Basic realm="Please enter User/Password"');
    header('HTTP/1.0 401 Unauthorized');
    die;
}
else
{
    if (($_SERVER['PHP_AUTH_USER'] !== $phpsc_username) || ($_SERVER['PHP_AUTH_PW'] !== $phpsc_password))
    {
        header('WWW-Authenticate: Basic realm="Incorrect! Please try again."');
        header('HTTP/1.0 401 Unauthorized');
        die;
    }
}

// open the ip log file:
$file = 'iplog.php';
$iplog = fopen($file, 'r');

// get contents of file:
$ips = fread($iplog, filesize($file));

// close file:
fclose($iplog);

// now get each line from the log file:
$ip = explode("\n", $ips);

// intiate our log var, will be used later
$log = '';

// our IP addy to search for
$ipaddy = "207.144.154.140";

// now loop through our ips, and display log for any matches:
for($i = 0; $i < count($ip); $i++)
{
    #if(preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:+/", $ip[$i], $log))

    // find our matches
    if(preg_match("/^{$ipaddy}:+/", $ip[$i]))
    {
        // add each match to the log array
        $log[] .= $ip[$i];
    }
}

// matches where found! Check that $log is an array
if(is_array($log))
{
    // count how many matches there are
    $matches = count($log);

    // display how many matches there were
    echo "<h1>{$matches} matchs found for IP {$ipaddy}</h1>\n";

    // display the log
    echo implode("<br />\n", $log);
}
else
{
   // no matches where found!
   echo "<h1>No matches found for IP {$ipaddy}</h1>";
}

?>

Change this line:
$ipaddy = "207.144.154.140";

To the ip address you want to search for, or you can implement a form instead.

NOTE: This code is untested with the header authentication, however it works fine without it.

#3 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 04:40 PM

Is it possible to use
$_SERVER['REMOTE_ADDR']
for the IP to look for?

Also, that code shows the line of the lines of the log right?

EDIT: With and without the header I am getting: No matches found for IP 192.168.1.1

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 05:11 PM

Is it possible to use

$_SERVER['REMOTE_ADDR']

for the IP to look for?

Yes, Change $ipaddy = "207.144.154.140"; to $ipaddy = $_SERVER['REMOTE_ADDR'];

Also if you want to display line numbers, change this:
$log[] .= $ip[$i];
to this:
$log[] .= 'Line ' . ($i+1) . ':&nbsp; &nbsp; ' . $ip[$i];

EDIT: With and without the header I am getting: No matches found for IP 192.168.1.1

In that case do you have the correct permissions to iplog.php or whatever file you are using to log the ip addresses, also is iplog.php in the same directory as this script is being run in?

#5 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 05:15 PM

My files always have the right permissions, its Windows.  And the iplog.php is in the same directory.

P.S.  I don't actually need the security for this script, the security was for the other log.

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 05:19 PM

What version of PHP are you using? As I am using PHP5, but I dont think I am using PHP5 specific functions. I am on Windows too.

#7 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 05:20 PM

I have PHP 5.1.1

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 05:32 PM

Just out of curiousity, change this:
$file = 'iplog.php';
/*$iplog = fopen($file, 'r');

// get contents of file:
$ips = fread($iplog, filesize($file));

// close file:
fclose($iplog);

// now get each line from the log file:
$ip = explode("\n", $ips);

To this:
$ip = file('iplog.php');
Also do you have safe_mode on?

#9 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 05:38 PM

Safe mode is off, and I get the same result with the new code

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 05:46 PM

Could you post your phpinfo? By running
<?php
phpinfo();
?>
So I can see what you current setup is. As currently I dont know why its not working.
Also add this:
echo '<pre>' . print_r($ip, true) . '</pre>';
After:
$ip = file('iplog.php');
Do you get anythink?

#11 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 05:51 PM

I get an array of the IPs in the log

phpinfo() = http://prattmic.home...mpp/phpinfo.php


#12 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 06:00 PM

Okay, so its getting the contents of the file. But its failing somewhere.
Change your code to this:
// open the ip log file:
$ip = file('iplog.php') or die("Unable to open iplog.php");

// intiate our log var, will be used later
$log = '';

// our IP addy to search for
$ipaddy = $_SERVER['REMOTE_ADDR'];

// now loop through our ips, and display log for any matches:
for($i = 0; $i < count($ip); $i++)
{
    if(preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:+/", $ip[$i], $log))
    {
        echo '<pre>' . print_r($log, true) . '</pre>';
    }
}
Do you get anything? You should get an array of ipaddress found within iplog.php

#13 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 06:03 PM

with that as the entire script I get nothing

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 06:13 PM

Argh! Hand on. I think I know why. Looking at the log you have from your first post, you have spaces at the beginning of each line within your iplog.php file. The regular expression is failing becuase of this. Okay try this code:
<?php

// open the ip log file:
$ip = file('iplog.php') or die("Unable to open iplog.php");

#echo '<pre>' . print_r($ip, true) . '</pre>';

// intiate our log var, will be used later
$log = '';

// our IP addy to search for
$ipaddy = $_SERVER['REMOTE_ADDR'];

// now loop through our ips, and display log for any matches:
for($i = 0; $i < count($ip); $i++)
{
    #if(preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:+/", $ip[$i], $log))

    // find our matches
    if(preg_match("/^{$ipaddy}:+/", trim($ip[$i])))
    {
        // add each match to the log array
        $log[] .= 'Line ' . ($i+1) . ':&nbsp; &nbsp; ' . rtrim($ip[$i]);
    }
}

// matches where found! Check that $log is an array
if(is_array($log))
{
    // count how many matches there are
    $matches = count($log);

    // display how many matches there were
    echo "<h1>{$matches} matchs found for IP {$ipaddy}</h1>\n";

    // display the log
    echo implode("<br />\n", $log);
}
else
{
   // no matches where found!
   echo "<h1>No matches found for IP {$ipaddy}</h1>";
}

?>
You should now get some results, <i>touch wood</i>!

#15 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 06:16 PM

Nope, still get no results.  :-\

Also, I have not spaces in front of each IP.  Don't you need a reference somewhere to phpsimplechoose_log.php (the main log).  I tried putting it as $log everytimes, but still never got results.

#16 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 06:20 PM

Whats in iplog? As this is the file being used to get the log for the ip address ($ipaddy). Does the code work when $ipaddy is $ipaddy = '192.168.1.1';

Whats in phpsimplechoose_log.php?

#17 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 06:24 PM

No it doesn't work with 192.168.1.1.

iplog.php contains a list of the all the IPs.

It currently contains:
192.168.1.1
192.168.1.1
80.144.16.124
80.144.16.124

phpsimplechoose_log.php is the IPs and the content, it contains:

<?php
// DO NOT DELETE THIS
include 'phpsimplechoose_config.php';
//Below is where the verification takes place. Try to play around with it.
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Please enter User/Password"');
header('HTTP/1.0 401 Unauthorized');
die;

} else {

if (($_SERVER['PHP_AUTH_USER'] !== $phpsc_username) || ($_SERVER['PHP_AUTH_PW'] !== $phpsc_password)) {
header('WWW-Authenticate: Basic realm="Incorrect! Please try again."');
header('HTTP/1.0 401 Unauthorized');
die;
}
}
// To delete log erase everything after the next line, but not the next line itself (Line 20 and down can be deleted)
?>
192.168.1.1: Sat July 22, 2006 12:55 : Choice 1.   dfghf  Choice 2.   ghdfgh  We say... ghdfgh <br />
192.168.1.1: Sat July 22, 2006 13:39 : Choice 1.   gsdfg  Choice 2.   sdfgdfg  We say... gsdfg <br />
80.144.16.124: Sat July 22, 2006 13:56 : Choice 1.   me  Choice 2.   him  Choice 3.   she  Choice 4.   it  Choice 5.   who  We say... it <br />
80.144.16.124: Sat July 22, 2006 13:57 : Choice 1.   good  Choice 2.   bad  Choice 3.   evil  Choice 4.   ugly  Choice 5.   world  We say... bad <br />


#18 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 06:26 PM

Okay, you'll want to put:
192.168.1.1: Sat July 22, 2006 12:55 : Choice 1.   dfghf  Choice 2.   ghdfgh  We say... ghdfgh <br />
192.168.1.1: Sat July 22, 2006 13:39 : Choice 1.   gsdfg  Choice 2.   sdfgdfg  We say... gsdfg <br />
80.144.16.124: Sat July 22, 2006 13:56 : Choice 1.   me  Choice 2.   him  Choice 3.   she  Choice 4.   it  Choice 5.   who  We say... it <br />
80.144.16.124: Sat July 22, 2006 13:57 : Choice 1.   good  Choice 2.   bad  Choice 3.   evil  Choice 4.   ugly  Choice 5.   world  We say... bad <br />
within iplog.php
In order for the code to work.

#19 prattmic

prattmic
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 22 July 2006 - 06:28 PM

So then I don't even need iplog, I can just change the value to phpsimplechoose_log?

EDIT:  It works now! thank you!  Is it possible to take out the line number and IP from what it shows though?  It isn't extremely important, but it would be nice.

#20 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 06:33 PM

No, you dont. Yeah you can just use phpsimplechoose_log.php instead of iplog.php

I thought you wanted the person to login first in order to view the ip logs. That why I suggested you use a seperate file for the iplogs.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users