Jump to content

File reading question


prattmic

Recommended Posts

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

    [code]<?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 />[/code]



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

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:
[code]<?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>";
}

?>[/code]

Change this line:
[code]$ipaddy = "207.144.154.140";[/code]

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.
Link to comment
Share on other sites

[quote]Is it possible to use
[code]$_SERVER['REMOTE_ADDR'][/code]

for the IP to look for?[/quote]
Yes, Change $ipaddy = "207.144.154.140"; to $ipaddy = $_SERVER['REMOTE_ADDR'];

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

[quote]EDIT: With and without the header I am getting: No matches found for IP 192.168.1.1[/quote]
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?
Link to comment
Share on other sites

Just out of curiousity, change this:
[code]$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);[/code]

To this:
[code]$ip = file('iplog.php');[/code]
Also do you have safe_mode on?
Link to comment
Share on other sites

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

Okay, so its getting the contents of the file. But its failing somewhere.
Change your code to this:
[code]// 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>';
    }
}[/code]
Do you get anything? You should get an array of ipaddress found within iplog.php
Link to comment
Share on other sites

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:
[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>";
}

?>[/code]
You should now get some results, <i>touch wood</i>!
Link to comment
Share on other sites

No it doesn't work with 192.168.1.1.

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

It currently contains:
[code]192.168.1.1
192.168.1.1
80.144.16.124
80.144.16.124
[/code]

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

[code]<?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 />[/code]
Link to comment
Share on other sites

Okay, you'll want to put:
[code]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 />[/code] within iplog.php
In order for the code to work.
Link to comment
Share on other sites

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.
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.