By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

how to get the int value from access.log and divided into session for each time?

P: 5
i'm new in PHP and I have a problem that is count the number of session
the problem is show below

example:
there are many lines string-record in access.log and i want to divided into some session by the requested time...for example by dividing 30minute/session
how can I divide it whereas the record time is string so I can find the number of session?

thanx b4
Jul 18 '07 #1
Share this Question
Share on Google+
2 Replies


pbmods
Expert 5K+
P: 5,821
Heya, thefredzx. Welcome to TSDN!

Are you trying to count the number of distinct sessions that get created every 30 minutes?
Jul 18 '07 #2

P: 5
yes, i want it
i have some scripts which has parsing the links of a web page and access log...but i have a problem for dividing datetime into 30minutes that have recorded on my table 'ststslog'

here's the code for parse the hyperlinks:
<?
include ("app.php");
$url = "http://localhost/MyZone/index.php";
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
$input = @file_get_contents($url) or die('Could not access file: $url');
$fhandle = fopen("web.txt", "a+");

if (preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER))
{
$i=1;
foreach($matches as $match)
{
$exstensi = explode (".", $match[2]);
$ext = $exstensi[1];
if($ext=="php" || $ext=="html"|| $ext=="xhtml"|| $ext=="asp"|| $ext=="aspx"|| $ext=="doc"|| $ext=="txt")
$ok = true;
else
$ok = false;

if ($ok==true)
{
$new_format[$each_rec]="$i\t$match[3]\t$match[2]";
$sql= "INSERT INTO hyperlink (id_link_perhal, nama, url)
VALUES('$i', '$match[3]','$match[2]')";
$result = mysql_query($sql);
$i++;
foreach($new_format as $data)
{
fputs($fhandle, "$data\n");
}
}
}
if ($result==true)
echo "data sudah tersimpan pada database<br>";
else
echo "data gagal tersimpan<br>";

fclose($fhandle);
}
?>

<a href="web.txt">file links</a>

and here's the code for parse access.log:
<?php
include "app.php";
$ac_arr = file('access.log');
$astring = join("", $ac_arr);
$astring = preg_replace("/(\n|\r|\t)/", "", $astring);
$records = preg_split("/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $astring, -1, PREG_SPLIT_DELIM_CAPTURE);

$sizerecs = sizeof($records);

// pembagian kedalam record
$i = 1;
$each_rec = 0;
$j=0;
while($i<$sizerecs)
{
$all = $records[$i+1];
//WAKTU
preg_match("/\[(.+)\]/", $all, $match);
$access_time = $match[1];
$waktu = explode (" ", $access_time);
$tanggal = explode (":", $waktu[0]);
$hari = explode ("/", $tanggal[0]);
$tgl= $hari[0];
$bln= $hari[1];
$bln= $b[$bln];
$b['Jan'] = '01';
$b['Feb'] = '02';
$b['Mar'] = '03';
$b['Apr'] = '04';
$b['May'] = '05';
$b['Jun'] = '06';
$b['Jul'] = '07';
$b['Aug'] = '08';
$b['Sep'] = '09';
$b['Oct'] = '10';
$b['Nov'] = '11';
$b['Dec'] = '12';
$thn= $hari[2];
$jam = $tanggal[1]; $menit= $tanggal[2]; $detik= $tanggal[3];
$access_time= "$thn-$bln-$tgl $jam:$menit:$detik";
$all = str_replace($match[1], "", $all);

//METHOD&URL harus halaman dokumen saja (php/html/asp)
preg_match("/\"GET (.[^\"]+)/", $all, $match);
$http = $match[1];
$link = explode(" ", $http);
$exstensi = explode (".", $link[0]);
$web = explode ("/", $exstensi[0]);
$all = str_replace("\"GET $match[1]\"", "", $all);
//STATUS harus <400
preg_match("/([0-9]{3})/", $all, $match);
$success_code = $match[1];
$all = str_replace($match[1], "", $all);

$j++;

if ($success_code>=400)
{
$stat_ok=false;
}
else
{$stat_ok=true;}

$ext = $exstensi[1];
if($ext=="php" || $ext=="html"|| $ext=="asp"|| $ext=="doc"|| $ext=="txt"|| $ext=="aspx")
{
$ok = true;
}
else
{
$ok = false;
}

$webku = $web[1];
if ($webku =="isi_com")
$web_ok = true;
else
$web_ok = false;

if (($ok==true && $stat_ok==true) && ($web_ok == true))
{
$new_format[$each_rec]="$access_time\t$link[0]\t$success_code";
$sql= "INSERT INTO statslog (tanggal, url, status)
VALUES('$access_time','$link[0]','$success_code')";
$result = mysql_query($sql);
}
$i = $i +2;
$each_rec++;
}
if ($result)
{
echo "data cleaning didapatkan....<br>";
}
else
echo "data cleaning gagal didapatkan<br>";
?>

<?
// untuk mendapatkan file dalam bentuk baru pada *.txt
$fhandle = fopen("access2.txt", "a");
{
foreach($new_format as $data)
{
fputs($fhandle, "$data\n");
}
fclose($fhandle);
}
?>
<a href="access2.txt">file data cleaning</a>

here's the db_config
<?
class object {};

$CFG = new object;

$CFG->dbhost = "localhost";
$CFG->dbname = "skripsi";
$CFG->dbuser = "root";
$CFG->dbpass = "";

$CFG->wwwroot = "/coba_skripsi";
$CFG->dirroot = dirname(__FILE__);
$CFG->datadir = "$CFG->wwwroot/data";

$DB_DEBUG = true;
$DB_DIE_ON_FAIL = true;

//mysql_select_db($dbname);
$db_connection = @mysql_connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass) or die (mysql_error());
@mysql_select_db($CFG->dbname,$db_connection);
?>

THANX B4
Jul 25 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.