Have you ever wondered where people go when they leave your site? With php3 it is easy to trace this.
Sure, several solutions of redirect-cgi's exist, but why bother with another language when
you can keep it straight to php3 and make it more comfortable?
My setting for writing this from scratch was a site where I had multiple pages with identical
links in a part of them, but I'd wanted to know the use of the external links in detail: Who
left where from which page and when. So here is how it works:
When you read the documentation about php3 in detail, you may have come across this kind of
one-liner:
<?php Header("Location: http://www.php.net"); exit; ?>
You find it in Section XIV. It uses a HTTP redirect command to make the users browser access a new
location. This is a good example how simple things in PHP3 can be: We will expand it, so that
we make a suitable tool for the curious ;).
First we make targeting more flexible, it is the 'where'. This one is easy, because you just
add a parameter to the URL of the redirector page. I'll call it
<?php '$u_target' ?>
.
(u_ stands for URI/URL, this is a personal way to determine where the variable belongs to that I am using.
Others: db_ is for database , p_ for page , f_ for function and so on.)
Then we want to record from 'which' page the link was activated. Since the referrer-information
is sometimes filtered out by proxies, browsers and firewalls, we will use the additional variable
<?php '$u_referrer' ?>
for this purpose. You can use simple keywords like
'homepage', the name of the page example.html or the full address "http://...". It doesn't really matter,
but keywords are better to understand on first look. When you skip this parameter,
<?php $HTTP_REFERER ?>
is used automatically.
Note that if it doesn't exist either,
<?php '$u_referrer' ?>
will be set to 'none'.
We will determine the "who" by using server-variables. We'll take
<?php $REMOTE_ADDR ?>
address for
this purpose, since Apache and IIS deliver it in nearly 90% of all installations ;).
<?php $REMOTE_HOST ?>
is better but since DNS-lookup is switched off sometimes, you'll have to try out on your server
yourself. (Use
<?php phpinfo() ?>
for this purpose. It's tip I got from Tim and others
which lead also to this script. Thanks ;)
'When' is easy:
<?php time() ?>
is all we need.
Logging the information gathered to a database is the method of choice. Since the data-structure
is simple and flat, you should be able to use any kind of solutions - even plain text files will
do it. Since MySQL is nice for this job, we'll take it for our purpose.
Since this article focuses on redirecting, I will keep the part of information exchange with the
database brief. You'll find more information on this topic here in phpbuilder.com and on the web.
Here is the SQL-Command for creating the table in MySQL:
# Definition
# Table structure for table \'redirect\'
#
CREATE TABLE redirect (
target varchar(80) DEFAULT \'no target\' NOT NULL,
referrer varchar(80) DEFAULT \'none\' NOT NULL,
client varchar(40) DEFAULT \'no client\' NOT NULL,
timeused datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL
);
You can easily expand the table for additional parameters like Keyword for the link,
User ID's and more. What follows is the code for the php3-Page: redir.php3
<?php
// Where did they go today? Version 1.02 - redir.php3
// (c) 1998 by smk_at_well.com
// Set variables for Database Access - you'll have to set up your own
$db_server = "127.0.0.1";
$db_user = "redirector";
$db_pass = "secret";
$db_data = "redir";
$db_table = "redirect";
// First, check target
if (!isset($u_target))
die("Sorry, no link was set in \$u_target");
// Second, is the referrer set? Note: HTTP_REFERER and some other variables
//have to be written uppercase or they won't work.
if (!isset($u_referrer)) {
if (!isset($HTTP_REFERER)) {
$u_referrer = "none";
} else {
$u_referrer = $HTTP_REFERER;
};
};
// Now the client address
if (!isset($REMOTE_ADDR)) {
$p_remotehost = "no client address";
} else {
$p_remotehost = $REMOTE_ADDR;
};
// And what time is it now?
$p_timeused = date("Y-m-d H:i:s",time());
// Get connected and transfer data
@mysql_connect("$db_server","$db_user","$db_pass") or
die("Couldn't connect to the database-server!");
@mysql_select_db("$db_data") or
die("Can connect to the server, but not to the table or database itself");
@mysql_db_query($db_data , "insert into $db_table values('$u_target','$u_referrer',
'$p_remotehost','$p_timeused')");
// And now she/he's gone with the wind ;)
Header("Location: $u_target");
exit;
?>
When you have set up the database and placed the page right you can use it in
different ways:
First, you can use it for standard
<a href="redir.php3?u_target=http://www.zet.net">
-commands. Second, you can use it in special forms for easy (and logged ;) navigation. Look at
this example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Where did they go today?</title>
</head>
<body>
<form action="redir.php3" method="POST" target="">
<select name="u_target" size="1">
<option value="http://www.slashdot.org">News for Nerds</option>
<option value="http://www.phpbuilder.com">News for cool Coders</option>
<option value="http://www.hotbot.com">If you can't find it there...</option>
<option value="http://community.zet.net">This is my baby...</option>
</select>
<input type="Submit">
</form>
</body>
</html>
Third: If you fear of getting 'copied' away with all of you links on your site, you
can expand the script, so that the parameter '$u_refferer' is looked up in a separate
database and replaced by an address. (Like the one in html: 1 is for www.slashdot.org,
2 is for www.phpbuilder.com, and so on)
Finally: Mail me an other ideas you have! Tell me if you want this code expanded
with user-id's, symbolic names for links and so on. I will put this script on an
own webpage soon and if I have the time, I will enhance it.
Sebastian