note 14959 deleted from ref.session by sniper
| From: | sniper@php.net | Date: | Sun, 28 Jul 2002 01:09:28 +0000 |
| Subject: | note 14959 deleted from ref.session by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-33782@lists.php.net to get a copy of this message | ||
I was trying to keep track of some pretty detailed statistics of a website
(mostly client side) and also run server side sessions to hold database information.
I kept getting "header already sent...." errors and my session variables weren't
staying registered - a total mess. I was using cookies to track statistics about a given user of
the website. I finally figured out a nice way to use both client and server side
variables and keep them constant across multiple pages. Here is how it's done.
First: Client side tracking.
Many sites keep track of who hits them, and they usually do this by sending the client a cookie
with some registered id that might correspond to a id system at the database level. In the
following example I used a cookie id that is used to locate information in a mysql database.
So, prepend something like this to the main index page of your site - I've left statistic
information
out to make things brief.
<?php
if(isset($statsid)) //Check to see if the stats cookie has been sent.
// Update the database information. This can include host and page information.
else {
$newid = random_string(); // Calculate a new statistics id number or whatever you want.
// Create a new stats database entry with the new id.
setcookie("statsid",$newid);
session_id($newid); // Server side php session id.
session_start();
session_register("statsid"); // Register the id variable - now both client and server.
}
?>
The perpuse of registering that statsid in that last step is so your index page can
use that session for other variables without screwing up the stats logging. Email me
if you want detailed information about logging activity.
The next step is creating a script that with log link activity so you can know what pages are
visited.
Again, this is only a short (stripped) example but it should be enough to get the idea across. Use
something
like the following script as the target of all <a href="track.php"> tags.
// track.php
<?php
// First, at each link check to see if the browser has cookie support.
// Here is an exmple of how this is done.
$support = 0;
if (isset($check) && ($browser == "check")) $support = 1;
if (!isset($CCHK)) {
setcookie("browser", "check");
header("Location: $PHP_SELF?CCHK=1"); }
// Parse the query sent to the track script so that we can redirect correctly.
// Example: $query = "some var passed to track.php";
if(isset($statsid)) {
// Make the necessary database updates based on $statsid.
// Pull whatever environment variables and info you want.
setcookie("statsid",$statsid); }
// We set the statsid cookie here because browsers associate client side
// variable with where they cam from.
else { // Same as above.
$newid = random_string(); // Calculate a new statistics id number or whatever you want.
// Create a new stats database entry with the new id.
setcookie("statsid",$newid);
session_id($newid); // Server side php session id.
session_start();
session_register("statsid"); // Register the id variable - now both client and server.
// Remember that we only init this php session if there is no record of the user in the db.
// This way they are logged regardless of how they arrive at the site.
}
?>
So now we have a stats logger that will keep track of what a browser of your site does.
Again, the problem I ran into is that prepending the first script and useing track.php would
interfer
with sessions I was trying to run at the the start of other pages on my site. The solution is to use
the cookie
information (if availiable) to hook into the same php session for all pages on your site.
Here is the code you want to prepend all other pages with.
<?php
// Check for the cookie given by the stats setup.
if (isset($statsid)) {
$sid = $statsid; // Set the session id variable with the id of the stats system.
else if ($passid) { $sid = $passid; } // If cookies aren't enabled by the user, enable the
possibility of
// passing id information. This is the hardass way to track movement on
// your site regardless of what the user wants - :)
else { $sid = random_string(); } // Or, just start a new session id for this page.
session_id($sid);
session_start(); // Since the stats scripts keep track of what id the user is associated with
// this is also a really nice way to maintain consistency on your site.
?>
The nice thing about this approach is that if the user has cookies turned off the stats store only
the initial
connection to the website (i.e you still know how hits the site) and php sessions are created
randomly to fit
your needs. If cookies are enabled or you wish to track variables with URL querys then the same php
session
(using the stats cookie id) can be maintained throughout the course of a given user's visit.
You may have run
into the problem of trying to run multiple php sessions - a mess. This approach eliminates the
problem.
Once the session is started on a given page you have access to all registered variables on the site.
And the stats
system works right along side with no complaints.
Email me if you want detailed information on how to implement this. I used this method on my site
and I
use sessions heavily on many pages to store db info. Enjoy.
Nolan Goodnight