prattmic Posted July 22, 2006 Share Posted July 22, 2006 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 beginningpart 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 Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/ Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 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 THISinclude '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 arrayif(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. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62093 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 Is it possible to use [code]$_SERVER['REMOTE_ADDR'][/code] 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 Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62096 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 [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) . ': ' . $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? Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62103 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62105 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62108 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 I have PHP 5.1.1 Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62109 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62111 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 Safe mode is off, and I get the same result with the new code Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62112 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 Could you post your phpinfo? By running[code]<?phpphpinfo();?>[/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? Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62113 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 I get an array of the IPs in the logphpinfo() = http://prattmic.homedns.org/xampp/phpinfo.php Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62114 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62117 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 with that as the entire script I get nothing Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62118 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 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) . ': ' . rtrim($ip[$i]); }}// matches where found! Check that $log is an arrayif(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>! Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62126 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62127 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62129 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 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.1192.168.1.180.144.16.12480.144.16.124[/code]phpsimplechoose_log.php is the IPs and the content, it contains:[code]<?php// DO NOT DELETE THISinclude '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] Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62130 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 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.phpIn order for the code to work. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62132 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62133 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 No, you dont. Yeah you can just use phpsimplechoose_log.php instead of iplog.phpI 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. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62137 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 No they don't need to login. Did you see my edit? Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62138 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 Must of missed that, does the code now work when use file('phpsimplechoose_log.php'); instead of file('iplog.php'); Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62141 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 Yes it does, but how do I take away the line number and the IP in the line, since they will all have the same IP anyway. Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62142 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 Change:[code]$log[] .= 'Line ' . ($i+1) . ': ' . rtrim($ip[$i]);[/code]to[code]$log[] .= str_replace($ipaddy . ': ', '', rtrim($ip[$i]));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62144 Share on other sites More sharing options...
prattmic Posted July 22, 2006 Author Share Posted July 22, 2006 Thank you for all your help. If you want to see the full script in action (but not done) it is at http://prattmic.homedns.org/phpsimplechoose/0_7_beta/phpsimplechoose.php Quote Link to comment https://forums.phpfreaks.com/topic/15337-file-reading-question/#findComment-62145 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.