From Python to PHP

Home Forums Languages PHP & MySQL From Python to PHP

Tagged: ,

This topic contains 8 replies, has 2 voices, and was last updated by  cinnamonsui 1 year, 8 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #13983

    cinnamonsui
    Participant

    Hey guys! I need help with something that’s been driving me crazy for days! I have a Python program that is opening a log file (text file) that contains ip-numbers, dates and page visited. Then it prints out a reverse list where the latest dates comes first amd sorts it so that if several “calls” have been made at the same time and from the same ip-number, it only shows the first call in the printed list. The result of the sorting is printed out in a table with ip, date and url as columns.

    Now I’m trying to do the same with PHP. I’ve tried different approaches, while, for and foreach but I can’t get it to work! I wish it was as simple as just translate the PHP code! :P I hope there is a PHP-guru around there that can give me some help :)

    This is my Python file:

    text_file = open("access.log", "r")
    entire_file = text_file.readlines()
    text_file.close()

    print """Content-type: text/html

    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml&quot; lang="sv">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Log</title>
    </head>
    <body>
    <table cellspacing="10">
    <tr><td>IP</td><td>Date</td><td>URL</td></tr>"""

    old_ip = ""
    old_date = ""
    list1=[]

    for line in reversed(entire_file):
    list1 += line,
    for line in list1:
    try:
    list1 = line.split('-')
    list1_ip = list1 [0]
    list2 = list1[2].split(" ")
    list1_date = list2[1].split(":")
    list1_url = list2 [4]

    if (old_ip == list1_ip) and (old_date == list1_date [0]):
    continue
    else:
    print "<tr><td>", list1_ip, "</td><td>", list1_date [0][1:], "</td><td>", list1_url,"</td></tr>"
    old_ip = list1_ip
    old_date = list1_date [0]
    except IndexError, e:
    error=e

    print """
    </table>
    </body>
    </html>"""

    This is one of the test files in PHP that I’ve made:

    <table>
    <tr>
    <td class="header"><strong>IP</strong></td>
    <td class="header"><strong>Date</strong></td>
    <td class="header"><strong>URL</strong></td>
    </tr>

    <?php

    try {
    $lines = file('access.log');
    $reversedArray = array_reverse($lines);

    for($i = 0; $i < sizeof($reversedArray); $i++)
    {
    $exploded = explode(" ", $reversedArray[$i]);
    }

    for($i = 0; $i < sizeof($reversedArray); $i++)
    {
    $ip = $exploded[0];
    $dateTime = $exploded[3];
    $date = substr($exploded[3], 1, 11);
    $time = substr($exploded[3], 13, 17);
    $url = $exploded[6];
    echo "<tr><td>" . $ip . "</td><td>" . $date . "</td><td>" . $url . "</td></tr>";
    }

    catch(Exception $e){ //If error
    echo "Error!";
    }
    ?>

    </table>

    Anyone? *puppy eyes*

    #14968

    Vera
    Participant

    So I created an access.log file which looks as follows:

    ==================

    127.0.0.1 01.01.1111 12:31 http://first.com

    127.0.0.2 02.02.1222 12:32 http://second.com

    127.0.0.3 03.03.1333 12:33 http://third.com

    127.0.0.4 04.04.1444 12:34 http://fourth.com

    127.0.0.5 05.05.1555 12:35 http://fifth.com

    127.0.0.6 06.06.1666 12:36 http://sixth.com

    And then changed your PHP code to:

    =====================================

    <table border="1">
    <tr>
    <td class="header"><strong>IP</strong></td>
    <td class="header"><strong>Date</strong></td>
    <td class="header"><strong>URL</strong></td>
    </tr>

    <?php
    try {
    $lines = file('access.log');
    $reversedArray = array_reverse($lines);

    for($i = 0; $i < sizeof($reversedArray); $i++){
    $exploded = explode(" ", $reversedArray[$i]);

    $ip = $exploded[0];
    $date = $exploded[1];
    $time = $exploded[2];
    $url = $exploded[3];
    echo "<tr><td>" . $ip . "</td><td>" . $date ." ". $time . "</td><td>" . $url . "</td></tr>";
    }

    }catch(Exception $e){ //If error
    echo "Error!";
    }
    ?>

    </table>

    And got the following output:

    ================================

    ____IP____|_______DATE________|________URL________

    127.0.0.6__|__06.06.1666 12:36__|___http://sixth.com

    127.0.0.5__|__05.05.1555 12:35__|___http://fifth.com

    127.0.0.4__|__04.04.1444 12:34__|___http://fourth.com

    127.0.0.3__|__03.03.1333 12:33__|___http://third.com

    127.0.0.2__|__02.02.1222 12:32__|___http://second.com

    127.0.0.1__|__01.01.1111 12:31__|___http://first.com

    Is this what you’re looking for? Or does your access.log file look differently?

    #14969

    cinnamonsui
    Participant

    If it just were that simple! ;) Sorry, I should have mentioned how the log file looked in my first post. The first rows in the log file looks like this:

    193.10.249.70 - - [26/Jul/2000:06:16:30 +0200] "GET /~dhtml HTTP/1.0" 301 302
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml HTTP/1.1" 301 314
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/ HTTP/1.1" 200 7502
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/back.gif HTTP/1.1" 200 1307
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/blnk.gif HTTP/1.1" 200 56
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/img0.gif HTTP/1.1" 200 6835
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/img1.gif HTTP/1.1" 200 7216
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/img2.gif HTTP/1.1" 200 2820
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/img3.gif HTTP/1.1" 200 1805
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/img4.gif HTTP/1.1" 200 2253
    195.67.108.14 - - [26/Jul/2000:10:18:55 +0200] "GET /~dhtml/img5.gif HTTP/1.1" 200 5348
    195.67.108.14 - - [26/Jul/2000:10:19:05 +0200] "GET /~dhtml/page2.html HTTP/1.1" 200 4525
    195.67.108.14 - - [26/Jul/2000:10:19:05 +0200] "GET /~dhtml/img6.gif HTTP/1.1" 200 5675
    195.67.108.14 - - [26/Jul/2000:10:19:05 +0200] "GET /~dhtml/img7.gif HTTP/1.1" 200 2815
    195.67.108.14 - - [26/Jul/2000:10:19:05 +0200] "GET /~dhtml/img8.gif HTTP/1.1" 200 2402
    195.67.108.14 - - [26/Jul/2000:10:19:05 +0200] "GET /~dhtml/img9.gif HTTP/1.1" 200 3027
    195.67.108.14 - - [26/Jul/2000:10:19:06 +0200] "GET /~dhtml/img10.gif HTTP/1.1" 200 10774
    195.67.108.14 - - [26/Jul/2000:10:19:06 +0200] "GET /~dhtml/img11.gif HTTP/1.1" 200 3074
    193.45.247.50 - - [26/Jul/2000:15:22:15 +0200] "GET /~dhtml/page4.html HTTP/1.0" 200 8898
    193.45.247.50 - - [26/Jul/2000:15:22:16 +0200] "GET /~dhtml/back.gif HTTP/1.0" 200 1307
    193.45.247.50 - - [26/Jul/2000:15:22:16 +0200] "GET /~dhtml/blnk.gif HTTP/1.0" 200 56

    There are plenty of lines and the date at the end is September. Of those above, only one line with ip 193.10.249.70, one line with ip 195.67.108.14 and one line with ip 193.45.247.50 should be displayed at the output.

    And that’s what makes everything so darn hard.

    My Python program does the following output (it’s reversed):

    IP	        Date	         URL
    212.75.67.89 16/Sep/2000 /~dhtml/img22.gif
    62.172.199.24 16/Sep/2000 /~dhtml/
    216.35.103.59 15/Sep/2000 /~dhtml/page2.html
    195.84.61.205 15/Sep/2000 /~dhtml/page5.html
    216.35.103.78 15/Sep/2000 /~dhtml/page4.html

    I don’t know what I’m doing wrong ;)

    #14970

    cinnamonsui
    Participant

    And for the record I also tried this code, and it went even worse :P It’s all very confusing right now.

    <?php

    try
    {
    $old_ip = "";
    $old_date = "";
    $list1 = array();
    $filehandler = fopen("access.log", 'r') OR exit("Cannot open file");

    while (!feof($filehandler) )
    {

    $row = fgets($filehandler);
    $list1 = explode(' ' , $row );

    $ip = $list1[0];
    $date = $list1[3];
    $url = $list1[6];
    }

    if ($old_ip == $list1_ip && $old_date = $list1_date)
    {
    continue;
    }
    else
    {
    echo "<tr><td>" . $ip . "</td><td>" . $date . "</td><td>" . $url . "</td></tr>";
    }

    ?>

    #14971

    Vera
    Participant

    Oh I see. Same basic structure, but the explode-ing needs to be a bit more complex.

    I’ll rewrite this when I get home (am at work now :P)… which will be in about 12 hours or so. Hope it’s not too late.

    #14972

    Vera
    Participant

    So from what I understand, you have to display the most recent info for each IP.

    From what the access.log file looks like, the entries are listed in a chronological order (earliest to latest).

    So this is what I came up with:

    =====================================================

    <?php
    /*
    * Check if IP was already added to the results array
    */
    function getArrayIndex($haystack=array(),$ip=''){
    if(!$ip || empty($ip) || count($haystack)==0) return -1; //nothing to search for

    $found = false; $index = -1;
    while($index<count($haystack)-1 && !$found){
    $index++;
    if($haystack[$index]['ip'] == $ip ){
    $found = true;
    }
    }

    if($found) return $index;
    else return -1; //not found
    }

    ?>

    <html>
    <head>
    <title>read from a file</title>
    </head>
    <body>

    <?php
    $lines = file('access.log');
    $reversedArray = array_reverse($lines);

    $result = array();

    for($i = 0; $i < sizeof($reversedArray); $i++){
    $exploded = explode(" - - ", $reversedArray[$i]);

    $ip = $exploded[0];

    $extra = explode(" ", $exploded[1]); // date, request, url, numbers

    $dateTime = substr($extra[0] ,1,(strlen($extra[0])-1));
    $date_data = explode(":",$dateTime);
    $date = $date_data[0];

    $url = $extra[3];

    $ind = getArrayIndex($result,$ip);
    if($ind == -1){
    $result[] = array('ip' => $ip, 'date' => $date, 'url' => $url);
    }

    }
    ?>
    <table border="1">
    <tr>
    <td>IP</td>
    <td>DATE</td>
    <td>URL</td>
    </tr>
    <?
    foreach($result as $resItm){
    echo "tt<tr>n".
    "ttt<td>".$resItm['ip']."</td>n".
    "ttt<td>".$resItm['date']."</td>n".
    "ttt<td>".$resItm['url']."</td>n".
    "tt<tr>n";
    }
    ?>
    </table>

    </body>
    </html>

    #14973

    cinnamonsui
    Participant

    Hm, I tried out your code and it didn’t work at all. It didn’t print out anything from the log.

    I got a little bit help from a few other friendly souls, but I thought I could share my solution (it’s what I had in mind from the beginning but didn’t quite know how to get there):

    <?php
    echo ("<table cellpadding='5'>
    <tr><td>IP</td><td>Date</td><td>URL</td></tr>");

    $array = file('access.log');

    reversedArray = array_reverse($array);

    $old_ip="";
    $old_date="";

    foreach ($reversedArray as $row) {
    $exploded = explode (' ', $row);
    $ip = $exploded[0];
    $dateTimeSplit = $exploded[3];
    $date = substr($exploded[3], 1, 11);
    $time = substr($exploded[3], 13, 8);
    $url = $exploded[6];

    if (($ip == $old_ip) && ($date == $old_date)){
    } else {
    echo ("<tr><td>" . $ip . "</td><td>" . $date . " " . $time . "</td><td>" . $url . "</td></tr>");
    }

    $old_ip = $ip;
    $old_date = $date;
    }

    echo ("</table>");
    ?>

    #14974

    Vera
    Participant

    Oh ok, sorry, I couldn’t help you. I’ve tested my code with the bit of the access.log file you gave me and it worked… :

    But at least you managed to make it work though :)

    #14975

    cinnamonsui
    Participant

    No worries. I really appreciate your effort :)

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.