VALUABLE HELP FOR WEBMASTERS
To implement I'll use a click counter: The problem: can be defined as “How implement a counter of visitors of links with Onclick” or “how count clicks using PHP and JS Onclick”. At that point, I was customizing a Directory script and I wanted to count every visit of each link using PHP and a MySQL database. Basically the database has the link’s URL and a counter. Most of the javacript and PHP tutorials said you can not do this, so… how count clicks using PHP?…
The solution: First I want that “on clicks” the link open in a blank window:
<A HREF="PAGE_TO_VISIT.html]" target="_blank">PAG TITLE</A>
Then the Java jscript’s : OnClick is used to execute a PHP script embedded in regular PHP page:
<A HREF="PAGE_TO_VISIT.html" OnClick="parent.location='clickcounter.php?url=PAGE_TO_VISIT.html'" target="_blank">PAGE TITLE</A>
-I pass the parameter "URL" to locate the MySQL specific record and increment the counter in 1: clickcounter.php?url=PAGE_TO_VISIT.html
The clickcounter.php code:
// get the URL from the variable
$urlupdate = $_GET['url'];
// updates the counter
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
$update = "UPDATE pages SET accesscounter = accesscounter + 1 WHERE url = '$urlupdate'";
$result = mysql_query($update);
//return to previous screen
See it in action with my Live Demo, notice this my sandbox so sometimes it does not work because I´m playing around. The first link has the counter, complementary results are provided by my script viXML, so you will not see the counter on it.
Credits: Scripts created and maintained by Sergio Vargas-Sanabria, please use contact information on the header.
Use this space to comment PHP and Java OnClick Do you have any idea how improve this script? let us know...
21 May 2005
MORE WEBMASTERS VALUABLE HELPS AND TOOLS:
viXML Parse Web Search results in your web pages using Gigablast Metasearch XML Services.
READ_COMMENTS for CutePHP NEWS: script to parse comments for specific topic - news
Más scripts y programas gratis para WebMasters:
MOD_REWRITE ayuda. Como convertir páginas dinámicas en estáticas
Ejecutar código PHP in páginas .HTML Como crear páginas HTML que corran código PHP
Sergio Vargas-Sanabria © 2005