sasori Posted October 17, 2011 Share Posted October 17, 2011 Hi, here's my problem let's say I gave an end user a url such as e.g http://www.test.com/index.php?user=asdf1234 and I did the same thing to different end users with unique $_GET['user'] value each . my question is, how to track the URLs ?, i mean, how will i know if those urls were clicked and how to count the clicks made ? some possible scenarios could be - the url is pasted and send via email - the url is embeded as a link in some portals Quote Link to comment https://forums.phpfreaks.com/topic/249237-url-tracking/ Share on other sites More sharing options...
MasterACE14 Posted October 17, 2011 Share Posted October 17, 2011 if(isset($_GET['user'])) { // do something? } is that what you mean? Quote Link to comment https://forums.phpfreaks.com/topic/249237-url-tracking/#findComment-1279842 Share on other sites More sharing options...
Psycho Posted October 17, 2011 Share Posted October 17, 2011 MasterACE14 has got the right idea. Although, I'm not sure what you mean by - the url is pasted and send via email - the url is embeded as a link in some portals You will have no idea if and by what means a link is shared - only that the user came to your page with a certain $_GET value passed via the URL. So, to expand upon Offline MasterACE14's suggestion, it all depends on how much information you track. The more information you want to track the more data there will be. For the sake of argument, let's say you want to count the number of times each unique 'user' value is submitted from each unique IP address. (NOTE: the 'user' value should be the User ID and not the User Name). Then you would want a database table with the following fields: - user_id - user_ip - count And, very important, you want to set the user_id and user_ip combination as unique. Do not set each field as unique - you need the combination to be unique. Example: CREATE TABLE IF NOT EXISTS `link_count` ( `user_id` int(2) NOT NULL, `user_ip` varchar(12) NOT NULL, `count` int(5) NOT NULL DEFAULT '1', UNIQUE KEY `link_count` (`user_id`,`user_ip`) ) Now, your code might look something like this $user_id = (isset($_GET['user'])) ? mysql_real_escape_string(trim($_GET['user'])) : ''; if(empty($user_id)) { //No user ID passed - create error condition echo "No user id passed!."; } else { //Validate that user ID is valie $query = "SELECT user_id FROM users WHERE user_id = '$user_id'"; $result = mysql_query($query); if(!mysql_num_rows($result)) { //User ID sdoes not exists - error condition echo "User ID is invalid!."; } else { //User id is valid show content and insert link count //This query will add a new row or increment count of existing row $user_ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']); $query = "INSERT INTO `link_count` (`user_id`,`user_ip`) VALUES ('{$user_id}', '{$user_ip}') ON DUPLICATE KEY UPDATE `count` = `count` + 1"; $result = mysql_query($query); } } Quote Link to comment https://forums.phpfreaks.com/topic/249237-url-tracking/#findComment-1279849 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.