Re: user identification



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'
?>


.



Relevant Pages

  • Re: [PHP] Sessions Lose Form Field Data When Back Button Used?
    ... That has to be your code re-setting the session variables or something. ... PHP won't do this automatically for you. ... Go to the test.php page in your browser by typing in the URL. ... If I add many line break br tags immediately before the submit input tag, so that you are required to scroll down to see the Submit button, after step A4 you will notice that the page reloads when using sessions, but seems to be cached by the browser when not using sessions. ...
    (php.general)
  • Re: [PHP] Re: HTTP headers, IE and downloading
    ... > I use sessions, and I've tried to send the same headers as the webserver ... > sends if I download a file directly (rather than through PHP). ... > the browser to it in case the browser is IE... ...
    (php.general)
  • Re: Session garbage collection query
    ... Mickey wrote: ... I just have a couple of questions regarding sessions. ... I read the php ... Your system gets no notification when the client closes their browser. ...
    (comp.lang.php)
  • Re: How to transfer the variable to next page?
    ... David Haynes wrote: ... The first step is to get php to send the predefined value to the web ... browser. ... This may be done using SESSIONs. ...
    (comp.lang.php)
  • Re: how to run scripts after a page has already loaded and been sent to a users browser?
    ... > It's because PHP is a server side scripting language, ... > do stuff before you send stuff to user's web browser. ... As we've discussed on this newsgroup before, if a script starts ... that is after the last HTML is sent to a web browser. ...
    (alt.php)