where to put pregmatch??

Discussion in 'PHP' started by webber09, Feb 5, 2011.

  1. #1
    me again :D

    i am looking to name my hits on my website logger when i view the logs. i know it will be a pregmatch to find my ip then replace it with my name but where and what do i put?? the logging script is below

    thanks all

    $file1 = "./ip.txt";
    $lines = file($file1);
    $count = count($lines);
    
    foreach ($lines as $line_num => $line)
    {
    //echo $line;
    $firstPos = strpos($line,':');
    $secPos = strpos($line,';');
    $MaxFileLength = substr($line,$firstPos+1,($secPos-$firstPos)-1);
    $DeleteLength = substr($line,strpos($line,':',$firstPos+1)+1,(strpos($line,';',$secPos+1)-strpos($line,':',$firstPos+1))-1);
    //echo $MaxFileLength;
    //echo $DeleteLength;
    break;
    }
    
    if( $count >= $MaxFileLength)
    {
    	$filearray = array_slice($lines, 0, 10);
    	$filearray2 = array_slice($lines, $DeleteLength+10);
    	$fp = fopen($file1, "w+");
    	foreach($filearray as $line)
    	{
    		 fputs($fp, $line);
    	}
    	foreach($filearray2 as $line)
    	{
    		 fputs($fp, $line);
    	}
    	fclose ($fp);
    }
    
    
    $open1 = fopen($file1, "r");
    $size1 = filesize($file1);
    $count1 = fread($open1, $size1);
    
    $counter= $_POST['countValue'];
    $when = "all";
    if($counter == "" || $counter == 0)
    {
    $counter =10;
    }
    ?>
    <td><div align=center>
    <form method="post" action="ipReader.php">The last
     <input type="Text" name="countValue" size=5 value=<?php echo($counter); ?>>  visitors are -
    <input type="Submit" name="submit" value="Show">
    </div>
    
    <?php
    echo("<table align=center width=100% border=1>");
    echo("<tr align=center>");
    echo("<td align=center><b>Total Count</b>");
    echo("</td><td align=center><b>Host Name</b>");
    echo("</td><td align=center><b>Visitor Ip</b>");
    echo("</td><td align=center><b>File Visited</b>");
    echo("</td><td align=center><b>Date</b>");
    echo("</td><td align=center><b>Time</b>");
    echo("</td></tr>");
    
    $tag2="true";
    
    $i =1;
    
    while (strlen($count1) > 1 && $i <= $counter)
    {
    $i =$i+1;
    //Day Calculation
    $pos1 = strrpos($count1, '+');
    $day = substr($count1,$pos1+1,($size1-$pos1)-1);
    
    $count1 = substr($count1,0,$pos1);
    
    //time Calculation
    $pos1 = strrpos($count1, '+');
    $c  = strlen($count1);
    $time = substr($count1,$pos1+1,($c-$pos1)-1);
    
    $count1 = substr($count1,0,$pos1);
    
    //Fname Calculation
    $pos1 = strrpos($count1, '+');
    $c  = strlen($count1);
    $fname = substr($count1,$pos1+1,($c-$pos1)-1);
    
    $count1 = substr($count1,0,$pos1);
    
    //hostname Calculation
    $pos1 = strrpos($count1, '+');
    $c  = strlen($count1);
    $hostname = substr($count1,$pos1+1,($c-$pos1)-1);
    
    $count1 = substr($count1,0,$pos1);
    
    //ip Calculation
    $pos1 = strrpos($count1, '*');
    $c  = strlen($count1);
    $ip = substr($count1,$pos1+1,($c-$pos1)-1);
    
    $count1 = substr($count1,0,$pos1);
    
    $pos1 = strrpos($count1, '*');
    $c  = strlen($count1);
    $tag = substr($count1,$pos1+1,($c-$pos1)-1);
    
    $count1 = substr($count1,0,$pos1);
    
    
    //$pos2 = strpos($count1, '*',$pos1-8);
    //$count = substr($count1,$pos2+1,($pos1-$pos2)-1);
    //$count = $count+1;
    if($tag2=="true")
    {
    echo("<tr>");
    $tag2="false";
    }
    else
    {
    echo("<tr>");
    $tag2="true";
    }
    echo("<td>".$tag);
    echo("</td><td align=center>".$hostname);
    echo("</td><td align=center>".$ip);
    echo("</td><td align=center>".$fname);
    echo("</td><td align=center>".$day);
    echo("</td><td align=center>".$time);
    echo("</td></tr>");
    }
    echo("</table>");
    fclose($open1);
    PHP:
     
    webber09, Feb 5, 2011 IP
  2. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    pregmatch what? Why don't you just use "$_SERVER['REMOTE_ADDR']" to find your IP?
     
    moads, Feb 5, 2011 IP
  3. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #3
    all i want to do is when the ip logs get viewed (using the script above) it replaces my ip with my name
     
    webber09, Feb 6, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Rather then alter the script, as it might cause complications when it tries to read IP's, you should use preg_replace instead of preg_match

    Something like this:

    
    $myip = preg_replace('#123.123.123.123#', "YourName", $myip); // replace 123.123.123.123 with your actual IP address
    
    PHP:
    Could be use after you echo out the IP's, maybe after this line

    echo("</td><td align=center>".$ip);

    NOTE: this of course will only work with a static IP address
     
    Last edited: Feb 6, 2011
    MyVodaFone, Feb 6, 2011 IP
  5. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #5
    i have entered your code into my script and changed the ip, but nothing. it still comes through as the ip??
     
    webber09, Feb 6, 2011 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    Try this instead:

    
    echo"</td><td align=center>".str_replace('123.123.123.123', "YourName", $ip);
    
    PHP:
     
    MyVodaFone, Feb 6, 2011 IP
  7. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #7
    putting the above gives the error

    HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

    :(
     
    webber09, Feb 6, 2011 IP
  8. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #8
    anyone got an idea?
     
    webber09, Feb 18, 2011 IP