/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] using the array_unique function

Hi Im trying to work out unique page visits within my script from my server log files. So far I can calculate the page views, but not the UNIQUE page views.

I belive I need to use the array_unique function, but I can’t get it to work.

Can anyone help me??

Here is my code:

[code=php]
<?php

$filename = ‘logs/’. ‘ex’. date(‘ymd’). ‘.log’; // Filename Checker

$fp = fopen($filename, “r”); //Open the server log
$content = fread($fp, filesize($filename)); // Read the server log
$content = explode(“n”, $content); // explode into array
$content = array_reverse($content ); // reverse the array
$n =0;
$n2 =0;
foreach ($content as $key=>$value)
{//strip out all this stuff we dont want
$weberror = substr($value, -18, 3); //strip lines beginning with 400/401/403/404/503

$findme = ‘Googlebot’;
$googlebot = strpos($value, $findme);

$findme1 = ‘picsearch.com/bot’;
$picsearch = strpos($value, $findme1);

$findme5 = ‘.gif’;
$gif = strpos($value, $findme5);

$findme6 = ‘.jpg’;
$jpg = strpos($value, $findme6);

$findme7 = ‘.txt’;
$txt = strpos($value, $findme7);

$findme8 = ‘.swf’;
$swf = strpos($value, $findme8);

$findme9 = ‘.dll’;
$dll = strpos($value, $findme9);

$findme10 = ‘.asp’;
$asp = strpos($value, $findme10);

$findme11 = ‘.png’;
$png = strpos($value, $findme11);

$findme12 = ‘.css’;
$css = strpos($value, $findme12);

$findme13 = ‘.ico’;
$ico = strpos($value, $findme13);

$findme14 = ‘.js’;
$js = strpos($value, $findme14);

$findme16 = ‘msnbot’;
$msnbot = strpos($value, $findme16);

$findme17 = ‘gigablast.com/spider.html’;
$gigaspider = strpos($value, $findme17);

$findme18 = ‘InternetSeer.com’;
$internetSeer = strpos($value, $findme18);

$findme19 = ‘+Slurp’;
$yahoo = strpos($value, $findme19);

$findme20 = ‘……/1.0+’;
$msnsneakbot = strpos($value, $findme20);

if ($weberror != “400” && $weberror != “401” && $weberror != “403” && $weberror != “404” && $weberror != “503” && $googlebot === false && $picsearch === false && $msnbot === false && $gigaspider === false && $internetSeer === false && $yahoo === false && $msnsneakbot === false && $gif === false && $jpg === false && $txt === false && $swf === false && $dll === false && $asp === false && $png === false && $css === false && $ico === false && $js === false)
{

//now we have stripped explode

$bits = explode(” “, $value);
@$request = $bits[5];
@$log_client = $bits[9];

$log_clientvar = explode(“n”, $log_client);
$result = array_unique($log_clientvar);

//search for string to find page views for this page
$find = ‘booked/excursions/’;

if (strpos($request, $find) == true )
{
print_r(“$request<br/>”);
$n++;
}

//Surely I should work out unique page views here???

}

else
{
}

}
echo “<br/>Page views for ‘$find’ = $n<br/><br/>”;
echo “Unique page views for ‘$find’ = $n2”;
fclose($fp);

?>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 12.2007 — I'm sorry, but I can't find where you store IPs or whatever so as to tell you how to render unique views, sorry 'bout that.

basicly the idea however is to do:

[code=php]$IPs[] = "NEW IP";
$IPs = array_unique($IPs);
[/code]
Copy linkTweet thisAlerts:
@bokehNov 12.2007 — Your script is poorly thought out. Reading a file of unknown size into PHP a variable and then working on it with functions such as explode and array_reverse is a sure way to crash PHP. You should read and work on the file one line at a time.
[code=php]$googlebot === false && $picsearch === false && $msnbot === false && $gigaspider === false && $internetSeer === false && $yahoo === false && $msnsneakbot === false && $gif === false && $jpg === false && $txt === false && $swf === false && $dll === false && $asp === false && $png === false && $css === false && $ico === false && $js === false[/code][/QUOTE]
When do any of those ever get set to false? Or even set at all?[code=php]$IPs[] = "NEW IP";
$IPs = array_unique($IPs);
[/code]
[/QUOTE]
I think I would probably do something like this instead (create an array with the IPs as the keys)which would save an unnecessary call to array_unique and keep the array size to the minimum possible size, while counting both pageviews and unique visits in one go:[code=php]@$IPs[$ip]++[/code]
Copy linkTweet thisAlerts:
@dan_coop3rauthorNov 12.2007 — Ok. thanks for that. It seems to be working ok now.

?
Copy linkTweet thisAlerts:
@bokehNov 12.2007 — It will work until the day when the combined processing carried out on the logfile exceeds the PHP RAM allocation.
×

Success!

Help @dan_coop3r spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...