|
|
|
|
|
|
| |
An absolute beginners tutorial for total niftyness in PHP!
Hi. What if you could count the amount of times visitors that clicked on certain links on your site? You could track how often they click on that link to greenpeace, or how often they click on the links at the top of the page compared with those on the bottom of the page. You could even count how often they click on that animated gif you had set up as a link.
Well, I'm here to tell you: it's easy! This is an Absolute Beginners tutorial.
What you'll need for this tutorial:
-PHP
-mySQL
-a good kick in the butt (we all need that now and then)
What we'll make:
We'll make a click.php3 page, through which all clicks will go. It will log
where the click came from and put that in a database.
So your links will look like
|
|
<a href="/click.php3?url=evolt.org&id=3">
|
|
|
instead of
|
|
<a href="http://evolt.org">
|
|
|
|
where id will tell the click.php3 page where the link came from. This way, you
can have a bunch of links all have the same id, and we keep it simple. Simple,
as you know, makes us Happy.
Tricky bit: no more relative links like this: ../../page.html (We're
keeping it simple)
Ok, let's get started.
There's a nifty function in PHP (look it up) that's called header. It sends a
header. It can be used to send the browser to another page, like this:
|
|
<code>
<?header("Location: http://evolt.org");
exit;?>
</code>
|
|
|
Try it out, upload it and go to the page. You'll be send straight to evolt.
Tricky bit: be careful not to put any spaces before the header function.
This will force output and it won't work.
Cool hey?
Now try this:
|
|
<code>
<?header("Location: http://$url");
exit;?>
</code>
|
|
|
Can you guess what this does?
Upload it, and then go to it like this:
click.php3?url=evolt.org
You'll be sent straight to evolt.org.
Niftyness!!!
Now you can see the possibilities here, right? We're gonna need a database to
track all this counting going on. Good thing you've got mySQL installed!
Now, the following bit will be easier if you've done some databasing before. But if not, don't worry, you can do it! To make all this really easy, you can use a database interface like facemysql, available for free. Or you can do it all from the command line.
We'll set up a table in our database that looks like this:
name: clickcount
fields(=columns): id / clicks
Yes, only 2 fields! (remember the Simple thing?)
To set up the database: we'll call the table clickcount:
|
|
<code>
id INT AUTO_INCREMENT NOT NULL PRIMARY_KEY,
clicks INT
</code>
|
|
|
That's all. Now you need to make a bunch of rows and set the clicks all to
0(id=1,clicks=0; id=2,clicks=0; id=3 and so on). You can do that easily with
facemySQL.
Have the database set up?
Good! That was the hard part. Here's the complete program, have a look at it:
|
|
<code>
<?
//Redirect browser to web site
header("Location: http://$url");
//connect to dbase like this:
$db=mysql_connect("mister.frogspace.net","user","pass");
// pedro is the name of the database in this case
mysql_select_db("pedro",$db);
// update the database:
// clickcount is the table, with 2 columns: id and clicks
$sql="UPDATE clickcount SET clicks=clicks+1 WHERE (id = '$id')";
$result=mysql_query($sql,$db);
// just to be nice
exit();
?>
</code>
|
|
|
Short explanation:
First, we send the user to the page where he was going.
Then, while the user is happily surfing on, we update the database.
And that's that for today. Cool hey? |
|
| |
| How To add paging (Pagination) with PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP | | | Multicolumn Output from a Database with PHP Categories : PHP, Databases, HTML and PHP, MySQL | | | Webstatistics with Redirectors Categories : PHP, HTML, HTML and PHP | | | Alternating row colors with PHP and mySQL Categories : PHP, Databases, MySQL, HTML and PHP | | | Beginners guide to PHP and MySQL - Creating a simple guest book Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL | | | How TO Install PHP, Apache and MySQL on Linux or Unix Categories : PHP, MySQL, Apache, Installation, Beginner Guides | | | Simple Connection to MySQL with PHP Categories : PHP, MySQL, Databases | | | Web database Information Flow Categories : PHP, MySQL | | | Static HTML Generation With PHP Categories : PHP, HTML and PHP | | | PHP, MySQL and Authentication 101 Categories : PHP, Databases, MySQL, Authentication | | | Extracting Elements from a Database into a Select Form Field Categories : PHP, HTML | | | Miles To Go Before I Sleep... Categories : PHP, Calendar, Databases, MySQL | | | Building A Dynamic MySQL Paging Class With PHP Categories : PHP, Databases, MySQL | | | Time Is Money Part 1 of 2 - Designing and implementing a Web-based application Categories : PHP, Databases, MySQL, Complete Programs | | | Case Study: Handling MySQL Growth With a PHP Class Categories : Databases, MySQL, PHP | |
| | | dhaval desai wrote : 72 Hiee!
I am unable to uderstand the above example..CAn
u please explain in detail.. I mean by using mysql
database..
Keep the commands of mysql as if they were typed
in from Shell
and then tell me What ios the header location and
why is the questionmark used after the links..and
stuf..
I thnik the questionmark thing will help make a
more of Dynamic website..I guess.....
Thank You
Dhaval Desai | | | dhaval desai wrote : 145 Im a fool | | | Anonymous wrote : 165 great - for me it works without problems (i just had
to remove the "code" tags).....will there be more
tutors from you ??? | | | Niels Oesten wrote : 190 Works great. With a little modification, even mailto: links
are handled. BUT: I sometimes use JavaScript to create
new windows in a predefined style and size. Therefore
the links may look like <A HREF=`javascript:wmVindue
()`> will shjow up. I can`t figure those out.
My website: www.oesten.dk
| | | Red hair wrote : 229 I made a usefull tool simular to this example.
Download it here:
http://redhair.free.fr/u-red-l/u-red-l.rar
It can: create/ delete/ modify/ track/ and fetch urls.
Admin protected, make new users, etc :)
At the time I post this message, I did not include a
readme....it will be there next week ;) | | | Anonymous wrote : 265 i want to change passwd. how i can do it
Thanks
Suntaree | | | Daniel Stefaniu wrote : 306 Yep, great, but if you need security, I suppose it sucks.
HTTP_GET_VARS and HTTP_POST_VARS must be mixed
together.
The other way around this is to include a
page "click.php" in every page you want to log.
This page can contain the link to database and the
name of the page is easy to collect with $PHP_SELF.
| |
|
|
|