/    Sign up×
Community /Pin to ProfileBookmark

Tracking time of visitor

I have code for tracking time of visitor. I have the following function:

[code=php]
<?

$db = “data/testdb”;
$dbres = sqlite_open ($db, 0666,$error);

if (!is_int ($field))
if (!is_resource ($dbres))
{
die (“sqlite error:$error”);
}

$GLOBALS[‘slength’]= 300;
$GLOBALS[‘dbres’] = connect (“data/testdb”);
$GLOBALS[‘visitID’]= $_COOKIE[‘cookname’];
$GLOBALS[‘userStats’];

if (empty($visitID) )

{
$userStats=newuser();
print “Welcome, first time user!”;
}

else
{
print “Welcome back, $visitID!”;
$userStats=olduser($visitID);
}

function newuser()
{
$visitData = array
(
‘FirstVisit’=>time(),
‘LastVisit’=>time(),
‘numVisits’=>1,
‘totalDuration’=>0,
‘totalClicks’=>1,
);
insertVisit ($visitData); /* row 132 function InsertVisit*/
setcookie (“visitID”, $visitData [‘ID’], time()+(60*60*24*365*10), “/”);
return $visitData;

}

function olduser($visitID)
{
$now = time();
$visitData = getVisit($visitID);
if ( ! $visitData )
{ return newuser();}
$visitData [‘totalClicks’]++;
if ( ($visitData [‘lastVisit’] + $GLOBALS [‘slength’]) > $now)
{
$visitData [‘totalDuration’] += ($now – $visitData [‘lastVisit’]);
}
else
{
$visitData [‘numVisits’]++;
}

$visitData [‘lastVisit’]=$now;
updateVisit ($visitData);
return $visitData;
}

function getVisit ($visitID)
{
$query = ”
SELECT * from trackVisit WHERE ID = $visitID”;
$result = sqlite_query ($GLOBALS[‘dbres’], $query);

if (! sqlite_num_rows ($result) )
{
return FALSE;
}
return sqlite_fetch_array ($result, SQLITE_ASSOC);

}
/* Old user*/
function updateVisit (&$visitData)
{

$update_pairs = array ();
foreach ($visitData as $field => $val)
{ if (!is_int ($field))
{
array_push ($update_pairs,”$field=$val” );
}

}

$query = “UPDATE trackVisit SET”;
$query .= implode (“,” ,$update_pairs);
$query .= “WHERE ID = “.$visitData [‘ID’];
sqlite_query ($GLOBALS[‘dbres’],$query);

}

/* New user*/
function insertVisit (&$visitData)
{

$query = “INSERT INTO trackVisit (
“;
$query .= implode (“,”,array_keys ($visitData));
$query .= “)VALUES ( “;
$query .= implode (“,” ,array_values ($visitData));
$query.= “);
“;
$result = sqlite_query ($GLOBALS[‘dbres’], $query);
$visitData [‘ID’]= sqlite_last_insert_rowid ($GLOBALS[‘dbres’]);
/* field ID is automatic incremented*/
}
?>
[/code]

What is actually correct formula for variable $visitData [‘totalDuration’] ?
Now is $visitData [‘totalDuration’] += ($now – $visitData [‘lastVisit’]);

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Archon_MkIVDec 05.2005 — I didn't read all that, partly because I'm too lazy. I think you'll find, however, that a much better way to track the duration a user spends on your site is by calling a session, storing the time() of the initial hit, and pulling that data from the session when it closes.
Copy linkTweet thisAlerts:
@toplisekauthorDec 05.2005 — Ok, I will do with session. Please inform me how to track with session these items:

1. Last visit time

2. Number of visits

3. Total duration on web site

4. Total clicks
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYDec 05.2005 — I didn't read all that, partly because I'm too lazy.[/QUOTE]
lazy, huh?

[url=http://www.webdeveloper.com/forum/showthread.php?t=85864&page=4]vote yes then![/url]
Copy linkTweet thisAlerts:
@Archon_MkIVDec 05.2005 — First off, you want to enable PHP session registering for your webpage. Create an include with session_start(); in it. Then, create a session and fill it with the current UNIX Timestamp. This can be done exactly like defining a variable ($_SESSION["tim"] = time()?. Then, when the server receives notice the session was closed, you pull the data from it and insert it into the database (or whatever you're working with). Tracking total clicks is rather tricky, however.

I'd recommend downloading some kind of stats package.
Copy linkTweet thisAlerts:
@toplisekauthorDec 06.2005 — I think the first two can't be done with sessions, requires cookies.

  • 1. How is code when the server receives notice the session was closed.


  • 2. what is actually difference between click and visit?
  • Copy linkTweet thisAlerts:
    @bokehDec 06.2005 — [code=php]$GLOBALS['slength']
    $GLOBALS['dbres']
    $GLOBALS['visitID']
    $GLOBALS['userStats'][/code]
    [/QUOTE]
    [DELETED] Why bother writing [B]$GLOBALS['slength'][/B] when you could simply write [B]$slength[/B]. [DELETED]
    Copy linkTweet thisAlerts:
    @NewZealandDec 07.2005 — [DELETED]



    lazy, huh?

    [url=http://www.webdeveloper.com/forum/showthread.php?t=85864&page=4]vote yes then![/url][/quote]
    Done and Done
    ×

    Success!

    Help @toplisek 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 5.3,
    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: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

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