/    Sign up×
Community /Pin to ProfileBookmark

Download Tracker?

I ned a script where it will generate a number next to a link…like if i have

joe.html(54), meaning it has been clicked on 54 times. Anyone know how to make this script?

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@pyroMay 03.2004 — I'd use MySQL to store the data, but you could use a flat file (either a text file, or an XML file). You'd just send the link as an argument to a PHP script, increment the count, and redirect to the URL. It'd be quite easy.
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 03.2004 — pyro my man! there you are! Where can i find instructions
Copy linkTweet thisAlerts:
@pyroMay 03.2004 — Do you know how to interact with a (mysql) database?
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 03.2004 — yes....phpmyasdmin im good with. BAsic but not outstanding
Copy linkTweet thisAlerts:
@pyroMay 03.2004 — Ok, then I'll try to explain what you'll want to do:

First, create a table with 3 fields, an ID field (which will be your primary field, set to auto-increment), a field to hold the URLs and a field to hold the count. Now, when you click a URL, it'll take you to a PHP script that will add new URLs/increment existing URLs. Pass the URL through the query string (eg. counter.php?url=http://www.php.net/). On the counter.php page, you'll read the URL, and either add or increment the URL, depending on if it is already in the database. Then, redirect the users to the URL. Finally, you can read the values out of the database, to display the current count.
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 03.2004 — you know the funniest part of this whole thing is you make it sound like its the easiest thing in the world. There isnt a block by block instructions on the net anywhere. Im going to try and do this but you didnt tell me how/what the code is for displaying the count and so forth. My php is scrathcy if at any. LEt me know...pyro
Copy linkTweet thisAlerts:
@pyroMay 03.2004 — Well, showing the count depends on what you need, really. I'm not sure what you are thinking here, so that makes it difficult to give you further help. How should the count be displayed?
Copy linkTweet thisAlerts:
@Paul_JrMay 03.2004 — My PHP is pretty bad, so don't quote me on this, but it should do something like you want:

[b]redirect.php[/b]
[code=php][size=1]
<?php
mysql_connect("localhost", "user", "pass");
mysql_select_db("links_db");
if(isset($_GET["link"]) && !empty($_GET["link"]) && is_numeric($_GET["link"])) {
$link = $_GET["link"];
$query = mysql_query("SELECT * FROM links_table WHERE id = " . $link);
while($rows = mysql_fetch_array($query)) {
$url = $rows["url"];
mysql_query("UPDATE links_table SET visits = " . ($rows["visits"] + 1) . " WHERE id = " . $link);
}
header("location: " . $url);
}
?>
[/size][/code]


Then the page where the links are displayed:
[code=php][size=1]
<?php
mysql_connect("localhost", "user", "pass");
mysql_select_db("links_db");
$query = mysql_query("SELECT * FROM links_table ORDER BY id");
echo "<ul>n";
while($rows = mysql_fetch_array($query)) {
echo "<li><a href="redirect.php?link=" . $rows["id"] . "">" . $rows["name"] . " (" . $rows["clicks"] . ")</a></li>n";
}
echo "</ul>n";
?>
[/size][/code]


That [i]should[/i] update the DB every time someone clicks on a link, and then, on the display page, echo out all the links in a list with the number of visits in the parentheses. Since I deleted it yesterday, I don't have a table full of links and such to try this, so I can't guarantee it'll work.

Also, you need 4 fields here: the link url, the auto_incrementing id, the name (which will be what everyone sees, between <a ... ></a> tags), and the number of clicks.
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 04.2004 — ok so for eac hlink I have to make a redirect.php page? What about wqhta i need to put in the Mysql?
Copy linkTweet thisAlerts:
@Paul_JrMay 04.2004 — [i]Originally posted by Baby Jai [/i]

[B]ok so for eac hlink I have to make a redirect.php page?[/B][/QUOTE]

No. You only need one reqirect page. The links would look like this:
[size=1]
&lt;a href="redirect.php?link=2"&gt;Foo&lt;/a&gt;
[/size]

The number (2) would be the ID of the link stored in the DB.

[i]Originally posted by Baby Jai [/i]

[B]What about wqhta i need to put in the Mysql? [/B][/QUOTE]

You'll need 4 fields: url (the url of the page), name (the name you want displayed in the <a ... ></a> tags), id(a auto_incrementing field), and visits (to store the number of times the link has been clicked on).

This all assumes the links will already be in the DB -- you'll have to manually insert them in. This also assumes all the links are in the same place -- I'm not real far along with PHP, so I'm not exactly sure how you'd do this with the links all spread out over the page.
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 04.2004 — Ok I see what you mean for the diffrent fields...what should i name the table I put them all in. You have to remember im working inside phpmyadmin

ok what do i put for the

Type

Length/Values*

Attributes

Null

Default**

Extra
Copy linkTweet thisAlerts:
@Paul_JrMay 04.2004 — [i]Originally posted by Baby Jai [/i]

[B]Ok I see what you mean for the diffrent fields...what should i name the table I put them all in. [/B][/QUOTE]

Whatever you want. In the script above, the name of the table is "links."
[i]Originally posted by Baby Jai [/i]

[B]You have to remember im working inside phpmyadmin[/B][/QUOTE]

I downloaded that when I got MySQL working, so I work in phpMyAdmin, too. ?

[i]Originally posted by Baby Jai [/i]

[B]ok what do i put for the

Type

Length/Values*

Attributes

Null

Default**

Extra [/B]
[/QUOTE]

This is what I have for mine:

[b]Field:[/b] name

[b]Type:[/b] VARCHAR

[b]Length/Values:[/b] 35

[b]Null:[/b] not null

[b]Field:[/b] url

[b]Type:[/b] VARCHAR

[b]Length/Values:[/b] 35

[b]Null:[/b] not null

[b]Field:[/b] visits

[b]Type:[/b] INT

[b]Length/Values:[/b] 4

[b]Null:[/b] not null

[b]Field:[/b] id

[b]Type:[/b] INT

[b]Length/Values:[/b] 4

[b]Null:[/b] not null

[b]Extra:[/b] auto_increment

And make sure you check the "Primary" radio button for the id field.
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 05.2004 — so where do i put the link in that code? I dont I just assign a number to each link then?
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 05.2004 — ok check out this

http://www.invision-it-all.com/skins.html

now if you see the stylistic dreamz skin...check out the error im getting in there. If you click on download you will see more errors and im not understanding why. Check out the download.php code...

[code=php]<?php

$db = mysql_connect('babyjai.mysql.protgp.com', 'babyjai', 'password');
mysql_select_db('babyjai_links');

$url = $_GET['url'];


$q = mysql_query("SELECT hits FROM skin_downloads WHERE url='$url'");
if(mysql_num_rows($q) == 0)
{
mysql_query("INSERT INTO skin_downloads values('$url', 0)");
$q = mysql_query("SELECT hits FROM skin_downloads WHERE url='$url'");
}
$r = mysql_fetch_row($q);

$r[0]++;

$q = mysql_query("UPDATE skin_downloads SET hits='".$r[0]."' WHERE url='$url'");

header("Location: $url");
?>[/code]


and here is the getcount.php
[code=php]<?php

$db = mysql_connect('babyjai.mysql.protgp.com', 'babyjai', 'password' );
mysql_select_db('babyjai_links');

$url = $_GET['url'];


$q = mysql_query("SELECT hits FROM skin_downloads WHERE url='$url'");
if(mysql_num_rows($q) == 0)
{
echo "0";
exit();
}
else
{
$r = mysql_fetch_row($q);
echo $r[0];
}

?>[/code]


and from s-light.php
[code=php]<a href="http://www.invision-it-all.com/dlm/download.php?url=http://www.invision-it-all.com/images/set-set_Stylistic_Dreamz.tar">Download</a> (<?php include("<http://www.invision-it-all.com/dlm/getCount.php?url=http://www.invision-it-all.com/images/set-set_Stylistic_Dreamz.tar>"); ?>)[/code]
Copy linkTweet thisAlerts:
@Paul_JrMay 07.2004 — Well, I don&#8217;t really see anything wrong with it, except this part isn&#8217;t needed:
[code=php]
if(mysql_num_rows($q) == 0)
{
echo "0";
exit();
}
else
{
$r = mysql_fetch_row($q);
echo $r[0];
}
[/code]

Since you inserted a default value of 0 when you insert a URL, you can just echo out the value of the hits field.

You might want to use use an auto_incrementing ID field to reference the links, instead of using the full URL.
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 07.2004 — well you know, I have played with this thing to no end. I dont know what to do else with it. I was givin this code and it doesnt work for whatever reason. What would you say the reason behind the error is
Copy linkTweet thisAlerts:
@Paul_JrMay 07.2004 — Well, the error leads me to believe that you are trying to access a file that doesn&#8217;t exist: [i]failed to open stream: No such file or directory[/i]. Is that code that you posted, from s-light.php, from line 129?

Try this for [b]download.php[/b]

[size=1][code=php]
<?php
mysql_connect("babyjai.mysql.protgp.com", "babyjai", "password");
mysql_select_db("babyjai_links");
if(!empty($_GET["url"])) {
$url = $_GET["url"];
$query = mysql_query("SELECT hits FROM skin_downloads WHERE url = '" . $url);
if(mysql_num_rows($query) == 0) {
mysql_query("INSERT INTO skin_downloads (url, hits) VALUES ('" . $url . "', 0)");
}
$row = mysql_fetch_row($query);
$query = mysql_query("UPDATE skin_downloads SET hits = " . $row[1]++ . " WHERE url = '" . $url);
header("location: " . $url);
}
?>
[/code]
[/size]

And this for [b]getCount.php[/b]

[size=1][code=php]
<?php
mysql_connect("babyjai.mysql.protgp.com", "babyjai", "password");
mysql_select_db("babyjai_links");
if(!empty($_GET["url"])) {
$url = $_GET["url"];
$query = mysql_query("SELECT hits1 FROM skin_downloads WHERE url = '" . $url);
$row = mysql_fetch_row($query);
echo $r[0];
}
?>
[/code]
[/size]
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 08.2004 — ok issue now...I solved the error issues within the s-light.php, but check this out when i click on the link

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/babyjai/www.invision-it-all.com/docs/dlm/download.php on line 7

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/babyjai/www.invision-it-all.com/docs/dlm/download.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /home/babyjai/www.invision-it-all.com/docs/dlm/download.php:7) in /home/babyjai/www.invision-it-all.com/docs/dlm/download.php on line 12[/QUOTE]


one again the files for you

download.php
[code=php]<?php
mysql_connect("babyjai.mysql.protgp.com", "babyjai", "password");
mysql_select_db("babyjai_links");
if(!empty($_GET["url"])) {
$url = $_GET["url"];
$query = mysql_query("SELECT hits FROM skin_downloads WHERE url = '" . $url);
if(mysql_num_rows($query) == 0) {
mysql_query("INSERT INTO skin_downloads (url, hits) VALUES ('" . $url . "', 0)");
}
$row = mysql_fetch_row($query);
$query = mysql_query("UPDATE skin_downloads SET hits = " . $row[1]++ . " WHERE url = '" . $url);
header("location: " . $url);
}
?> [/code]


getcount.php
[code=php]<?php

$db = mysql_connect('babyjai.mysql.protgp.com', 'babyjai', 'password' );
mysql_select_db('babyjai_links');

$url = $_GET['url'];


$q = mysql_query("SELECT hits FROM skin_downloads WHERE url='$url'");


?>

[/code]
Copy linkTweet thisAlerts:
@Paul_JrMay 08.2004 — On the download.php page, try changing the first query to this:

[size=1][code=php]
$query = mysql_query("SELECT * FROM skin_downloads WHERE url = '" . $url);
[/code]
[/size]

And that last error message is because you&#8217;re trying to send a header after some content has been displayed (e.g., printed something, some whitespace, HTML tags). To solve this problem, make sure that PHP code is at the very top of the page, with not even a space infront of the opening <?php tag.
Copy linkTweet thisAlerts:
@Baby_JaiauthorMay 09.2004 — ok...well if i did the php syntax on the top I got na error on line 2. You tell me what I need to change. I dont see if I changed it how it would still show the link hence im getting rid of the code if i was to do it your way.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Light Skins</title>

<style type="text/css">

<!--

body {

font-family: Tahoma;

font-size: 11px;

background-color: #C6BAB5;

}

table {

font-family: Tahoma;

font-size: 11px;

font-style: normal;

color: #000000;

}

a:link {

font-family: Tahoma;

font-size: 11px;

font-style: normal;

color: #000066;

text-decoration: underline;

}

a:hover {

font-family: Tahoma;

font-size: 11px;

font-style: normal;

color: #008080;

text-decoration: underline;

}

.tableborder {

border: 1px solid #900500;

}

.tableborder2 {

border-top: 0px solid #900500;

border-right: 1px solid #900500;

border-bottom: 0px solid #900500;

border-left: 1px solid #900500;

}

.tablepad {

border-top: 0px solid #900500;

border-right: 1px solid #900500;

border-bottom: 0px solid #900500;

border-left: 1px solid #900500;

background-color: #BAB1B1;

}

h1 {

font-family: Tahoma;

font-size: 16px;

font-style: italic;

color: #000000;

}

.style2 {font-weight: bold; font-size: 14px;}

-->

</style>

</head>

<body>

<div align="center" class="style2"></div>

<table width="600" border="0" cellpadding="0" cellspacing="0">

<tr>

<td width="23%"><table width="200" border="0" cellpadding="0" cellspacing="0">

<tr>

<td width="17"><img src="/images/image-left.gif" /></td>

<td width="212" background="/images/image-back.gif" valign="bottom"><div align="center">Gradilization</div></td>

<td><img src="/images/image-right.gif" width="17" height="18" alt="/" /></td>

</tr>

<tr>

<td height="16" background="/images/image-leftbg.gif">&nbsp;</td>

<td width="212"><div align="center"><img src="/images/Ss-GGMain.png" width="200" height="200" /></div></td>

<td width="17" background="/images/image-rightbg.gif">&nbsp;</td>

</tr>

<tr>

<td><img src="/images/image-left2.gif" /></td>

<td width="212" background="/images/image-back2.gif">&nbsp;</td>

<td><img src="/images/image-right2.gif" width="17" height="16" /></td>

</tr>

</table></td>

<td width="77%">Skin Title: Gradilization <br />

Author: Baby Jai <br />

Site: <a href="http://www.babyjai.com" target="_new">Baby Jai Online</a> / <a href="http://www.invision-it-all.com" target="_new">Invision-It-All </a><br />

Release Date: Unknown<br />

Tested Browsers: Internet Explorer<br />

Screenshot: <a href="/images/S-GGMain.png" target="_new">Main</a>, <a href="/images/S-GGReplies.png" target="_new">Replies</a>, <a href="/images/S-GGAddReply.png" target="_new">Add Reply</a><br />

Access To File: <strong>Minimum Donation of $1.49

</strong><br />

Premier Download: <a href="/members/set-Gradilization.tar">Download Skin </a><br />

Cost For Ownership (Removed From Database): $99.99 <br /><form action="https://www.paypal.com/cgi-bin/webscr" method="post">

<input type="hidden" name="cmd" value="_
xclick">

<input type="hidden" name="business" value="[email protected]">

<input type="hidden" name="item_name" value="Gradilization Skin">

<input type="hidden" name="amount" value="1.49">

<input type="hidden" name="page_style" value="PayPal">

<input type="hidden" name="no_shipping" value="1">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<table><tr><td><input type="hidden" name="on0" value="Enter Your Email:">Enter Your Email:</td><td><input type="text" name="os0" maxlength="200"></td></tr><tr><td><input type="hidden" name="on1" value="Type Of Delivery">Type Of Delivery:</td><td><select name="os1"><option value="Download Link">Download Link<option value="Full File">Full File</select>

</td></tr></table><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">

</form></td>

</tr>

</table><br /><table width="600" border="0" cellpadding="0" cellspacing="0">

<tr>

<td width="23%"><table width="200" border="0" cellpadding="0" cellspacing="0">

<tr>

<td width="17"><img src="/images/image-left.gif" /></td>

<td width="212" background="/images/image-back.gif" valign="bottom"><div align="center">Stylistic Dreamz </div></td>

<td><img src="/images/image-right.gif" width="17" height="18" alt="/" /></td>

</tr>

<tr>

<td height="16" background="/images/image-leftbg.gif">&nbsp;</td>

<td width="212"><div align="center"><img src="/images/sS-SD.png" width="200" height="200" /></div></td>

<td width="17" background="/images/image-rightbg.gif">&nbsp;</td>

</tr>

<tr>

<td><img src="/images/image-left2.gif" /></td>

<td width="212" background="/images/image-back2.gif">&nbsp;</td>

<td><img src="/images/image-right2.gif" width="17" height="16" /></td>

</tr>

</table></td>

<td width="77%">Skin Title: Stylistic Dreamz <br />

Author: Baby Jai <br />

Site: <a href="http://www.babyjai.com" target="_new">Baby Jai Online</a> / <a href="http://www.invision-it-all.com" target="_new">Invision-It-All </a><br />

Release Date: Unknown<br />

Last Update: May 4th, 2004 <br/>

Tested Browsers: Internet Explorer<br />

Screenshot: <a href="/images/S-SD.png" target="_new">Main</a>, <a href="/images/S-SDReply.png" target="_new">Replies</a>, <a href="/images/S-SDControls.png" target="_new">Controls</a><br />

Access To File: <a href="http://www.invision-it-all.com/dlm/download.php?url=http://www.invision-it-all.com/images/set-set_Stylistic_Dreamz.tar">Download</a> <?php include("http://www.invision-it-all.com/dlm/getCount.php?url=http://www.invision-it-all.com/images/set-set_Stylistic_Dreamz.tar"); ?><br />

Premier Download: N/A<br />

Cost For Ownership (Removed From Database): $14.99 <br /></td>

</tr>

</table>

</body>

</html>[/QUOTE]
Copy linkTweet thisAlerts:
@Paul_JrMay 10.2004 — All right, this should work, I tested it out just a few minutes ago.

This is the page to display the count, it should only contain this PHP code:
[code=php]
<?php
mysql_connect("babyjai.mysql.protgp.com", "babyjai", "password");
mysql_select_db("babyjai_links");
$query = mysql_query("SELECT * FROM skin_downloads WHERE url = '" . $_GET["url"] . "'");
if(mysql_num_rows($query) == 0) {
mysql_query("INSERT INTO skin_downloads (url, visits) VALUES ('" . $_GET["link"] . "', 0)");
}
$row = mysql_fetch_row($query);
echo $row[2];
?>
[/code]

To retrieve the count, your include should look like this: [i]include("http://www.domain.com/dir/subdir/file.php?url=http://www.domain.com/dir/subdir/subdir/file.php");[/i]

In other words, you need to use absolute paths.
×

Success!

Help @Baby_Jai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...