damerit Posted May 21, 2008 Share Posted May 21, 2008 I need to post to my table every time a link has been clicked on. I'm not sure how to handle posting every time a user clicks on a link. We are tracking who and how many times certain links are clicked on. My question is, how do I handle posting every time a link has been clicked on? I need to post an id and a timestamp to mysql. thnks, dvd Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 21, 2008 Share Posted May 21, 2008 Are these links to internal or external pages? Internal -> Track the pages that load, rather then when they are clicked. It might be easier to install something like Google Analytics to track those pages. External -> Most sites have a PHP page, like link.php, and the links would actually point to this internal page, with the external URL as an argument. link.php would record the info, then use header() to forward them along to the external site. Quote Link to comment Share on other sites More sharing options...
Rohan Shenoy Posted May 21, 2008 Share Posted May 21, 2008 Note: This a an entry from my blog. I had written it few days back when somebody was in a situation similar to yours. It is easy to track where your vistors come from, but not so straight-forward to track where your visitors left for after following an outbound link from your website. Here is a small JavaScript-PHP combination that will let you know which site they headed for. We need two scripts for this purpose: 1. convert_external_links.js - The file file that contains the javascript code. This function will convert ALL links to an external site from http://www.othersite.com to http://www.yoursite.com/redirect.php?url=http://www.newsite.com. Links to pages within your site will remain unaffected. 2. redirect.php - The PHP script that is supposed to record the site and then redirect the visitor to the new site. 1. convert_external_links.js This function will convert ALL links to an external site from http://www.othersite.com to http://www.yoursite.com/redirect.php?url=http://www.newsite.com. Links to pages within your site will remain unaffected. function record_leaving() { //declare your domain name below var domain="w3hobbyist.com"; a=document.getElementsByTagName("a");//a=an array of hyperlinks total=a.length;//total no of hyperlinks on the page for(i=0; i<total; i++) { link=a[i].href; if(link.match(domain)==null) /* We use the javascript match() function to check if the href attribute of the hyperlink contains your domain name. If it does not contain, its an outbound link to an external site and will be converted. */ { prev_href=link; new_href="http://www."+domain+"/redirect.php?url="+prev_href+""; a[i].href=new_href; } } } Now simply call this function wherever you want to convert the links to outbound sites. 2. redirect.php The PHP script that is supposed to record the site and then redirect the visitor to the new site. <?php $url=$_GET["url"]; //insert the URL into your database header("Location: $url"); ?> Quote Link to comment Share on other sites More sharing options...
damerit Posted May 21, 2008 Author Share Posted May 21, 2008 Sorry, here is a clearer picture. I have an internal web application in php and mysql. I'm looking to log every time the user clicks on a specific link. This would gather their information like, session user ID, timstamp, and a running count for this record into a seperate table space. What is the best method of posting this information to the database? thks, dvd Quote Link to comment Share on other sites More sharing options...
damerit Posted June 18, 2008 Author Share Posted June 18, 2008 I add the code to the top of the page... session_start(); require_once('Connections.php'); $q = "insert into log (logid, userid, timestamp) VALUES ('$dtid', '$userid', '$submitDate')"; mysql_query($q, $DTDB) or die (mysql_error()); Problem: Once I moved it to the production it posts twice to the database, but it doesn't post twice all the time. It is random when I use IE 6.0. When I use Mozilla I don't have this problem. Are there any suggestions as to why this would post twice in IE 6.0? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.