You are here: Re: user identification « PHP Programming Language « IT news, forums, messages
Re: user identification

Posted by BearItAll on 11/07/33 11:17

On Mon, 30 May 2005 13:40:19 -0700, d.schulz81 wrote:

> Hi all,
>
> We have about 10 different domains that are linked very closely and we
> want to identify and track every single user that surfs our websites.
> Later we want to analyse user paths and find out the search robots with
> the referring search words.
>
> What are the possibilities?
> Cookies are not accepted by 40 % of our users and in addition to that for
> each domain a different cookie is created what makes it really
> complicated.
> I guess a combination of Browser type, Operating System, Hostname etc is
> really insecure as there are many users using the same stuff. I think the
> only secure way to logg this is by the way of using sessions.
>
> One disadvantage of sessions is that they take very much performance of
> the server when there are many users at the same time. Is there a way to
> reduce performance of the sessions? Are there any other possibilities
> except sessions? Are there freeware php statistic functions that allow the
> reuse of their statistic data in our own implementations?
>
> Thank you very much for your help!
>
> Dennis

This one is based on a mysql table, it records standard information from
$_SERVER[]. You might want to resolve the IPs on entry to the table,
personally I prefer to only look up those that haven't been looked up
before, which I do locally so haven't included here. Also, I moved the
database connect to inside the function, for the sake of those new to this
stuff who want to see how it fits together.

Finally, don't criticise my crappy HTML bits. I have a real battle with
html. Don't bother asking me why because I don't know the answer, if only
we could do php without having to bother at all with the html side I'd be
alright.


The first function is the one to include in each of your pages (it records
the page hits as well as site hits).

<?php
// Visitor hits
//
function VisitorHit()
{
//constants

define('DB_USER','your mysql user name');
define('DB_PASSWORD','your password');
define('DB_HOST','localhost');
define('DB_NAME','database name');

//connect to database

$dbh=mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
if( ! $dbh) {
die ('I'm terribly sorry but I cannot connect to the database because:
'.mysql_error());
}

//open database

mysql_select_db (DB_NAME);

//select records
$thispage = $_SERVER['PHP_SELF'];
if( strlen($thispage) < 1 )
{
$thispage = "none";
}
$browser = $_SERVER['HTTP_USER_AGENT'];
if( strlen($browser) < 1 )
{
$browser = "none";
}
$ip = $_SERVER['REMOTE_ADDR'];
if( strlen($ip) < 1 )
{
$ip = "none";
}
$requestmethod = $_SERVER['REQUEST_METHOD'];
if( strlen($requestmethod) < 1 )
{
$requestmethod = "none";
}
$querystring = $_SERVER['QUERY_STRING'];
if( strlen($querystring) < 1 )
{
$querystring = "none";
}
$requesturi = $_SERVER['REQUEST_URI'];
if( strlen($requesturi) < 1 )
{
$requesturi = "none";
}
$referer = $_SERVER['HTTP_REFERER'];
if( strlen($referer) < 1 )
{
$referer = "none";
}

// build SQL string

$sql = "INSERT INTO `visitors` ( `visitorid` , `self` , `browser` , `ip` , `requestmethod` , `querystring` , `requesturl` , `referer` , `touched` ) VALUES ('', '$thispage', '$browser', '$ip', '$requestmethod', '$querystring', '$requesturi', '$referer', NOW( ));";

//perform query

$result = mysql_query($sql);

// no checking because we aren't that bothered about missing the odd hit

}

?>

This next part is really an example of how a 'stats.php' file might be
used to examine the data.


<?php
$page_title='Stats';
include 'header.inc';

define('DB_USER','your mysql username');
define('DB_PASSWORD','your password');
define('DB_HOST','localhost');
define('DB_NAME','your database name');

$dbh=mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
if( ! $dbh) {
die ('I cannot connect to the database because: '.mysql_error());
}

//open database

mysql_select_db (DB_NAME);

//select minimum date record

$sql = "SELECT DATE_FORMAT(MIN(touched),'%D %b %Y') FROM visitors;";
$result = mysql_query($sql);

if($result)
{
$row = mysql_fetch_array($result);
$mindate = $row[0];
}

//select maximum date record
$sql = "SELECT DATE_FORMAT(MAX(touched),'%D %b %Y') FROM visitors;";
$result = mysql_query($sql);

if($result)
{
$row = mysql_fetch_array($result);
$maxdate = $row[0];
}

//DISTINCT visitors

$sql = "SELECT COUNT(DISTINCT(ip)) FROM visitors;";
$result = mysql_query($sql);

if($result)
{
$row = mysql_fetch_array($result);
$distinct = $row[0];
}

//DISTINCT browsers

$sql = "SELECT COUNT(DISTINCT(browser)) FROM visitors;";
$result = mysql_query($sql);

if($result)
{
$row = mysql_fetch_array($result);
$browser = $row[0];
}


?>

<div >
<h2><a name="Stats">Statistics</a></h2>
<?php
echo "<p>Stats between $mindate and $maxdate</p>";
echo "<p>Number of visitors $distinct</p>";
echo "<p>Number of browsers $browser</p>";

//DISTINCT browsers

$sql = "SELECT browser, COUNT(*) AS icount FROM visitors GROUP BY browser
ORDER BY icount DESC;";

$result = mysql_query($sql);

echo "<p>";
if($result)
{
while( $row = mysql_fetch_array($result))
{
printf("%s --- %s<br>", $row[1],$row[0]);
}
}
echo "</p>";

?>

</div>

<?php
include 'footer.inc'
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация