Jump to content

URL tracking


sasori

Recommended Posts

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 ?  :confused:

some possible scenarios could be

- the url is pasted and send via email

- the url is embeded as a link in some portals

 

Link to comment
Share on other sites

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);
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.