PHP and text data

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I have a PHP program that I use to track visitors to my website.

It collects, among other things, the page visited, the refferer, the day of visit, the month of visit and the year of visit.

Once the data is colled, it is written to a log text file.

I can view the file by using a text editor but it is not that easy to read.

After some manipulation, the complete log text file is parsed so that I can display each item separately in a table.

Evenually this log text file will have many days, months and years,

What I would like to do is to enter a particular day, month and year and have it display only that portion from the log text file.

Basically then, I need a php script that when I open the log text file, I have a table form where I enter a day, month and year and then submit it to display results for that particular time frame only.

I would appreciate any suggestions
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

Why don't you rather store this log in a MySql database ... ?
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

MySQL database is not available
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Your going to have to load the data into an array and manipulate it. Instead of reading the flat file and put it into an array you can simply just store an array in a serialized format.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

All data in the log text file are basically rows.

Each row is parsed so that each row becomes a series of separate variables, each variable containing the particular data

I know the postion of the day, month and year variables in the row.

I want to be able to enter a particular day, month and year and then display all the rows with that particular date.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

I'm going to recommend what SpooF suggested. That is the best option for you without any database being available for you.

Here is one way you could attempt this. (Just to give you an idea).
PHP Code: [ Select ]
<?php
$array = array();
$today = date("j/n/Y"); // 3/11/2010
$array[$today]['referrer'] = $referrer;
$array[$today]['other_things'] = $otherThings;
// so on and so forth
 
// saving the contents into the log file
file_put_contents('log.txt', serialize($array));
 
// retrieving the files
$retrieved = unserialize(file_get_contents('log.txt'));
 
echo $retrieved['3/11/2010']['referrer'];
?>
  1. <?php
  2. $array = array();
  3. $today = date("j/n/Y"); // 3/11/2010
  4. $array[$today]['referrer'] = $referrer;
  5. $array[$today]['other_things'] = $otherThings;
  6. // so on and so forth
  7.  
  8. // saving the contents into the log file
  9. file_put_contents('log.txt', serialize($array));
  10.  
  11. // retrieving the files
  12. $retrieved = unserialize(file_get_contents('log.txt'));
  13.  
  14. echo $retrieved['3/11/2010']['referrer'];
  15. ?>
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I have serialzied the data.

Let me explain again.

The data for each visitor is written in the form of a row in the log text file.
For a new visitor, a new row is written.

With some manipulation, each row becomes a number of serialized data elements, element1 is page visited, element2 is refferer, element3 is day, element4 is month, element5 is year, etc.

As you can see I know exactly where the day, month and year are stored in each row

I want to use a POST function so that I can enter a day, month and year and then display the results in an echoed html table.

I must confess, I am a novice PHP programmer and learn much of my stuff by experimentation and advice from people. So if I sound like sometimes I am not talking correctly, please excuse me.

Thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I should also have added that I want to display all the rows that have the day, month and year in question, not just individual data elements
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Do you have the code to retrieve the data and your just asking how to display it?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

That's why I suggested having the date as a key... I think that would be easier to search the data by date. But I'm not sure if that's the best way.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Yes, right now I can display all the data by echoing an html table.

My goal is to be able to display only the data for a certain date so I need to know how to select this so when I echo the html table it will only display the rows for the date entered.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

Anyone else think that the solution could be done in JavaScript?
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I would really like to stay with PHP if possible
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Give me a bit and I'll put something together to show you.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

The serialized data is displayed with following:

foreach ($log as $logline) {
echo '<tr>';

echo '<td>' . $logline['0'] . '</td>';
echo '<td>' . $logline['1'] . '</td>';
echo '<td>' . $logline['2'] . '</td>';
echo '<td>' . $logline['3'] . '</td>';
echo '<td>' . $logline['4'] . '</td>';
echo '<td>' . $logline['5'] . '</td>';
echo '<td>' . $logline['6'] . '</td>';
echo '<td>' . $logline['7'] . '</td>';
echo '<td>' . $logline['8'] . '</td>';
echo '<td>' . $logline['9'] . '</td>';
echo '<td>' . $logline['10'] . '</td>';
echo '<td>' . $logline['11'] . '</td>';
echo '<td>' . $logline['12'] . '</td>';
echo '<td>' . $logline['13'] . '</td>';
echo '<td>' . $logline['14'] . '</td>';
echo '<td>' . $logline['15'] . '</td>';

echo '</tr>';


}

echo '</table>';

The day is in $logline['3']
The month is in $logline['4']
The year is in $logline['5']

What I would like to do is to somehow POST the day, month and year and then these values would be used to display only the rows that contain the posted day, month and year.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

The way you are doing it right now, the only solution that is possible (imo) is to do it in Javascript, unless you are willing to try a different form of putting the content into the log file.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Is it possible to do a php search and if it matches the day, month and year, it displays the entire row(s)
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Code: [ Select ]
foreach ($log as $logline) {

if($logline[3] == $day AND $logline[4] = $month AND $logline[5] = $year)
{
echo '<tr>';

echo '<td>' . $logline['0'] . '</td>';
echo '<td>' . $logline['1'] . '</td>';
echo '<td>' . $logline['2'] . '</td>';
echo '<td>' . $logline['3'] . '</td>';
echo '<td>' . $logline['4'] . '</td>';
echo '<td>' . $logline['5'] . '</td>';
echo '<td>' . $logline['6'] . '</td>';
echo '<td>' . $logline['7'] . '</td>';
echo '<td>' . $logline['8'] . '</td>';
echo '<td>' . $logline['9'] . '</td>';
echo '<td>' . $logline['10'] . '</td>';
echo '<td>' . $logline['11'] . '</td>';
echo '<td>' . $logline['12'] . '</td>';
echo '<td>' . $logline['13'] . '</td>';
echo '<td>' . $logline['14'] . '</td>';
echo '<td>' . $logline['15'] . '</td>';

echo '</tr>';

}
}

echo '</table>';
  1. foreach ($log as $logline) {
  2. if($logline[3] == $day AND $logline[4] = $month AND $logline[5] = $year)
  3. {
  4. echo '<tr>';
  5. echo '<td>' . $logline['0'] . '</td>';
  6. echo '<td>' . $logline['1'] . '</td>';
  7. echo '<td>' . $logline['2'] . '</td>';
  8. echo '<td>' . $logline['3'] . '</td>';
  9. echo '<td>' . $logline['4'] . '</td>';
  10. echo '<td>' . $logline['5'] . '</td>';
  11. echo '<td>' . $logline['6'] . '</td>';
  12. echo '<td>' . $logline['7'] . '</td>';
  13. echo '<td>' . $logline['8'] . '</td>';
  14. echo '<td>' . $logline['9'] . '</td>';
  15. echo '<td>' . $logline['10'] . '</td>';
  16. echo '<td>' . $logline['11'] . '</td>';
  17. echo '<td>' . $logline['12'] . '</td>';
  18. echo '<td>' . $logline['13'] . '</td>';
  19. echo '<td>' . $logline['14'] . '</td>';
  20. echo '<td>' . $logline['15'] . '</td>';
  21. echo '</tr>';
  22. }
  23. }
  24. echo '</table>';
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Looks very good

Is it possible to let someone enter the day, month and year

This way you can view various dates
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Yes, your going to want to look at $_GET, $_POST and forms.

http://www.w3schools.com/php/php_forms.asp
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I tried the following:

Code: [ Select ]
echo '<form action="select.php" method="post">';
echo 'Input Day: <input type="text" name="day" />';
echo 'Input Month: <input type="text" name="month" />';
echo 'Input Year: <input type="text" name="year" />';
echo '<input type="submit" />';
echo '</form> ';

$day = '$_REQUEST["day"]';
$month = '$_REQUEST["month"]';
$year = '$_REQUEST["year"]';


foreach ($log as $logline) {

if($logline['3'] == $day AND $logline['4'] == $month AND $logline['5'] == $year)

{
  echo '<td>' . $logline['0'] . '</td>';
  echo '<td>' . $logline['1'] . '</td>';
  echo '<td>' . $logline['2'] . '</td>';
  echo '<td>' . $logline['3'] . '</td>';
  echo '<td>' . $logline['4'] . '</td>';
  echo '<td>' . $logline['5'] . '</td>';
  echo '<td>' . $logline['6'] . '</td>';
  echo '<td>' . $logline['7'] . '</td>';
  echo '<td>' . $logline['8'] . '</td>';
  echo '<td>' . $logline['9'] . '</td>';
  echo '<td>' . $logline['10'] . '</td>';
  echo '<td>' . $logline['11'] . '</td>';
  echo '<td>' . $logline['12'] . '</td>';
  echo '<td>' . $logline['13'] . '</td>';
  echo '<td>' . $logline['14'] . '</td>';
  echo '<td>' . $logline['15'] . '</td>';   

  echo '</tr>';


}

}

echo '</table>';
  1. echo '<form action="select.php" method="post">';
  2. echo 'Input Day: <input type="text" name="day" />';
  3. echo 'Input Month: <input type="text" name="month" />';
  4. echo 'Input Year: <input type="text" name="year" />';
  5. echo '<input type="submit" />';
  6. echo '</form> ';
  7. $day = '$_REQUEST["day"]';
  8. $month = '$_REQUEST["month"]';
  9. $year = '$_REQUEST["year"]';
  10. foreach ($log as $logline) {
  11. if($logline['3'] == $day AND $logline['4'] == $month AND $logline['5'] == $year)
  12. {
  13.   echo '<td>' . $logline['0'] . '</td>';
  14.   echo '<td>' . $logline['1'] . '</td>';
  15.   echo '<td>' . $logline['2'] . '</td>';
  16.   echo '<td>' . $logline['3'] . '</td>';
  17.   echo '<td>' . $logline['4'] . '</td>';
  18.   echo '<td>' . $logline['5'] . '</td>';
  19.   echo '<td>' . $logline['6'] . '</td>';
  20.   echo '<td>' . $logline['7'] . '</td>';
  21.   echo '<td>' . $logline['8'] . '</td>';
  22.   echo '<td>' . $logline['9'] . '</td>';
  23.   echo '<td>' . $logline['10'] . '</td>';
  24.   echo '<td>' . $logline['11'] . '</td>';
  25.   echo '<td>' . $logline['12'] . '</td>';
  26.   echo '<td>' . $logline['13'] . '</td>';
  27.   echo '<td>' . $logline['14'] . '</td>';
  28.   echo '<td>' . $logline['15'] . '</td>';   
  29.   echo '</tr>';
  30. }
  31. }
  32. echo '</table>';


I see the input boxes and enter day, month year

When I submit nothing is displayed

Am I missing something

Thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I found some little php errors

Once I cleared them up, everything is now working OK

By using a certain date now, I can view only the data for that date which is what I wanted

Thanks for all the suggestions

Thanks Bogey. That if statement did the trick and was quite simple to implement. I will remember this next time I am in the same situation

Again, thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Sorry SpooF

I should have thanked you for the IF statement that solved my problems

Between you and Bogey, I believe we came up with a very elegant and simple solution to my problem.

Not only can I view all data but now I can view selected data

Thanks again to both of you
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I would appreciate if I could get some more help with this same project I am working on.

In my serialized row data, one of the elements is the IP address

The same IP address could appear in multiple rows if it is a return visitor.
If the IP address appears only in one row, then it is a unique visitor or first time visitor.

I have created an array for my serialized data rows and included an echo statement to check that I can indeed read the IPs in each row as follows:

foreach ($log as $logline) {
$visdata = array (
$logline['0'],
$logline['1'],
$logline['2'],
$logline['3'],
$logline['4'],
$logline['5'],
$logline['6'],
$logline['7'],
$logline['8'],
$logline['9'],
$logline['10'],
$logline['11'],
$logline['12'],
$logline['13'],
$logline['14'],
$logline['15']
);

echo $logline['9'], "<br/>";

}

When I run the script I do indeed see the same IP appearing more than once and unique IPs.

I can count the total number of rows so that I know the total for all the visits, return and unique.

What I would like to get is the following:

1) For the same multiple IPs, I want this to be grouped as one return vistior.
If there are say 7 same Mutiple IPs then this would count as 7 return visitors.
2) The number of unique IPs

I would then end up with something like the following:

Total Visits: 100
Return visits: 40
unique visits: 20

I would appreciate any suggestions

Thanks
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

Image


I assume $logline['9'] is the IP...
PHP Code: [ Select ]
<?php
$holder = array();
$total = 0;
foreach ($log as $logline)
{
    $visdata = array (
        $logline['0'],
        $logline['1'],
        $logline['2'],
        $logline['3'],
        $logline['4'],
        $logline['5'],
        $logline['6'],
        $logline['7'],
        $logline['8'],
        $logline['9'],
        $logline['10'],
        $logline['11'],
        $logline['12'],
        $logline['13'],
        $logline['14'],
        $logline['15']
    );
   
    if(in_array($logline['9'], $log))
    {
        $holder[$logline['9']] += 1;
    }
    $total++;
    echo $logline['9'], "<br/>";
}
?>
  1. <?php
  2. $holder = array();
  3. $total = 0;
  4. foreach ($log as $logline)
  5. {
  6.     $visdata = array (
  7.         $logline['0'],
  8.         $logline['1'],
  9.         $logline['2'],
  10.         $logline['3'],
  11.         $logline['4'],
  12.         $logline['5'],
  13.         $logline['6'],
  14.         $logline['7'],
  15.         $logline['8'],
  16.         $logline['9'],
  17.         $logline['10'],
  18.         $logline['11'],
  19.         $logline['12'],
  20.         $logline['13'],
  21.         $logline['14'],
  22.         $logline['15']
  23.     );
  24.    
  25.     if(in_array($logline['9'], $log))
  26.     {
  27.         $holder[$logline['9']] += 1;
  28.     }
  29.     $total++;
  30.     echo $logline['9'], "<br/>";
  31. }
  32. ?>


I don't even know if that works or even goes with the $log array... I think this might give an idea to someone... or maybe even you.

The main purpose of this post is the image up top :D ... and then I decided to put in something...
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Bogey

After reading your post, I did in fact get some ideas that worked out for me.

I first declared an IP Address only array called $ip = array()

From my serialized log data, as shown above, I took only the IP address elements and dynamically wrote them to the $ip array with the following:

$ip[]=$logline['9']

So now I had the $ip array with all the IP addresses, duplicates and singles to manipulate.

I then declared a new array that counted the frequency of the IP addresses in the $ip array as follows:

$dataout = array_count_values ($ip)

So now I new exactly how many times an IP(s) was duplicated, return visitors, and how many single IP(s), unique visitors there were.

I then set up a simple counter for the duplicated and single IP values that would increment depending on how many IPs were duplicated and which were not as follows;

PHP Code: [ Select ]
$dataout = array_count_values ($ip);
$r=0;
$u=0;
 
foreach ($dataout as $value)
{
If ($value > 1 )
{
$r++;
}
else
{
$u++;
}
}
 
echo "Return Visitors = ".$r." <br/><br/>";
echo "Unique vistors  = " .$u." <br/><br/>";
 
  1. $dataout = array_count_values ($ip);
  2. $r=0;
  3. $u=0;
  4.  
  5. foreach ($dataout as $value)
  6. {
  7. If ($value > 1 )
  8. {
  9. $r++;
  10. }
  11. else
  12. {
  13. $u++;
  14. }
  15. }
  16.  
  17. echo "Return Visitors = ".$r." <br/><br/>";
  18. echo "Unique vistors  = " .$u." <br/><br/>";
  19.  


Nothing fancy here, but it works like a charm.

Like you said, sometimes you just need someone else to come up with a suggestion and then your flying.

Thanks again

Post Information

  • Total Posts in this topic: 26 posts
  • Users browsing this forum: Liamw411 and 139 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.