Help - Search - Members - Calendar
Full Version: Link Counter
Weborum Webmaster Forum > Web Page Design > PHP
Joe
take a look at this site -http://www.good-tutorials.com/ - and on each review they have 786 total clicks | 786 clicks this month

i have looked in many places but i cannot find any help on how to do this. i just want a script that will tell me how many people have clicked each link

does anyone know of / or have this script?

tia, Joe
Timo
It wouldn't be too hard to make,
You'd need to just have a table with the following
A unique ID
The link out
The name of the link
Counter field

Then you just call a page which adds one to the counter by the ID and sends the person to the link out

On the page you just grab whatever data you want from the database

You could even add a description field...

I may have some time later today to pull something together real quick if you want, but need some sleep right now. Darn 830 classes cause day drowsyness.


Been working on it, almost done, just need to add a tiny script to add in new records and the one to display them.
Joe
thanks man, i will be looking over your tutorial (and leo's) to get it working

if i run into any problems, im coming back wink.gif lol
Timo
I didn't include a monthly count because I was a bit lazy but I reckon that wouldn't be too hard to put in.
You'd need 2 new fields month and mcount

When the links are displayed check the month against the month in the database, if it's the same, do nothing at this point, if they are different update the month in the field and set the mcount to 0.

When the person clicks on the link instead up just adding one to the count also add one to the mcount, because of the way we set it up to set it back to 0, it should be fine.

Maybe a future version I'll add that in if you want.
Joe
i wasnt really bothered about the monthly part, i just wanted the link and then how many times it has been clicked next to it.

gonna try your example now
Timo
k if you need any help formatting the link on its way out feel free to show how you want it
Like

LinkName - 5 click(s) - Description
Or however you want it, I'll rewrite the echo code for it, for you.
Joe
thanks man, i have just PM'd you.

need help already, not really understanding all this PHP stuff
Josh
I'm going to need a script like that for my future game site. But some of the links won't be linking off my site, some will be http://joshmccrain.com/gamename.htm.

Will it still work?
Timo
Yep, should work, if you want to do it relative you can just exclude the http:// when you're entering them or you can just write out the whole absolute url, relative will work though.
Joe
here is my file biggrin.gif

cheers for all this m8
Timo
You have to add the links in one by one into the database, use that code from the tutorial, this should pull them out though.

Remember to change your variables for the database and stuff, I left them blank.

Psychology time, adios

-Timo
Joe
CODE
<?php
$dbHost =; #Your db host
$dbUser =; #username
$dbPass =; #Pass
$dbname =; #Name of the database.
?>

localhost
root
my_pass
links (create database using myphpadmin)

do i create any tables in this database?
Timo
Yep you'll need to need to use the query I have in the tutorial, Leo's tutorial shows how to do it in phpmyadmin. You'll also need to upload the link handler script.

Once you have the Query in, upload the y file and just add the links one by one, then get rid of the y file, or put it in a password protected directory.

Also you need to put the variables in again, inside the files. I suggest just making an external file with them and including that file in the future, but it's up to you.
Joe
ok, i have both files now and i have added my database / username information ect

i added the query to the database using phpmyadmin

the bit im confused about is how do i add links?
Timo
In the zip there is a file called y.php upload that and just add the links one by one. Then delete that from your server or put it in a password protected directory.
Joe
add the links in normal HTML ?
Timo
Oops put the wrong y.php in the zip haha laugh.gif

Here is the real one.
Timo
For anyone else, I already showed Joe, I messed up again on the y.php file laugh.gif I fixed it though.
Timo
This time it's fixed, and tested ;-)
How many times can I mess up my own darn code?

Joe either way you'll want to grab this new version, I messed up on the last.

Oh they look fine though Joe, besides the newest Y...
leo
hehe, i'd lost track of this thread.

did someone get confused? i often confuse myself laugh.gif
Joe
keep up leo tongue.gif

i got confused about everything and Time messed up his own code laugh.gif

i think everything is sorted now though, just about to test the new 'y.php'
Joe
it works biggrin.gif

Timo your a genius!!!!! i finally have it working, and now for the big step, getting to work online, for this i will need your help leo wink.gif

when i create a database using phpmyadmin it creates a new folder in the \EasyPHP1-7\mysql\data folder, the question is how can i bring the database WITH the php files so i can use it online?
leo
yeah sure, use the tutorial i did to transfer the database, timo posted the link in this thread i think, i would go and get it but my connection is playing up, not sure if this post will get through sad.gif
leo
got through smile.gif transfer the database with that tutorial and then uplaod all your php files in the same structure they are now and put them in the root folder
Joe
ok, i will give that a go.

also, another quick question.
i have found a nice little tutorial on how to make a simple news program located http://www.phpdeveloper.org/section/tutorial/56

when it mentions the database i need to create, would this be to name the database 'news' (for example) and then post this code into the query part of phpmyadmin?

CODE
CREATE
TABLE news(
author VARCHAR(20),
date INT,
content TEXT,
ID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID)
)
Timo
Yeah, you'd put that in the query thing and submit it.
Joe
with the actual code it gives would i have to define the variables mysefl?

CODE
$db=mysql_connect("localhost","user","pass") or die ("cant connect");
mysql_select_db("database",$db) or die ("cant change");
$news=mysql_query("select * from news ORDER BY date DESC LIMIT 8") or die ("cant get em");
while($rows=mysql_fetch_array($news)){
$date=date("F d, Y H:i ",$rows["date"]);
$author=$rows["author"];
$number="100%";
printf("<table cellpadding=2 cellspacing=0 border=0 width=%s>n",$number);
echo "<td bgcolor=#496CB1 class='first'><b>$title</b></td>n";
echo "<td align=right bgcolor=#496CB1 class='first'>$date</td><tr>n";
echo "<td colspan=2><i>by <A href=\"mailto:$email\">$author</a></i><br>
$content</td></table><p>n";
}


using

CODE
<?php
$dbHost = "localhost";  #Your db host
$dbUser = "root"; #username
$dbPass = "password"; #Pass
$dbname = "news"; #Name of the database.


and then closing the php file with ?>
leo
yeah, replace any values in the script with the database values, if it doesn't ahve anything which will setup the database for you (i thought most scripts had a setup function these days).

don't replace any code though joe, just the approptˇriate passwords etc, it should already have the closing ?>
Timo
He makes you manually put it in the code, see like where it says localhost in the code? I usually put mine in variables so I can just have the code link to one file with all of them. Saves me from having to define them each time; if I wanted to move to a different server with mysql I wouldn't have to go through the code each time.
leo
oh i see, yeah variables would be trhe way to go, everything in one config file.
Joe
CODE
<?php
$dbHost = "localhost";  #Your db host
$dbUser = "root"; #username
$dbPass = "password"; #Pass
$dbname = "news"; #Name of the database.

$db=mysql_connect("localhost","user","pass") or die ("cant connect");
mysql_select_db("database",$db) or die ("cant change");
$news=mysql_query("select * from news ORDER BY date DESC LIMIT 8") or die ("cant get em");
while($rows=mysql_fetch_array($news)){
$date=date("F d, Y H:i ",$rows["date"]);
$author=$rows["author"];
$number="100%";
printf("<table cellpadding=2 cellspacing=0 border=0 width=%s>n",$number);
echo "<td bgcolor=#496CB1 class='first'><b>$title</b></td>n";
echo "<td align=right bgcolor=#496CB1 class='first'>$date</td><tr>n";
echo "<td colspan=2><i>by <A href=\"mailto:$email\">$author</a></i><br>
$content</td></table><p>n";
}

?>
so this would be in the news.php file and then i make the database called news and add the query

the only thing is i tried this y.day and i got an empty page, now i suppose this is a good thing seeing as i dont get any error messages but how do i add news to see if the news program actually works?
Timo
Joe that wouldn't work. I added the variables when I did my scripts. This guy likes placing them in the code. You could replace the line...
CODE

$db=mysql_connect("localhost","user","pass") or die ("cant connect");

with
CODE

$db = mysql_connect("$dbHost","$dbUser","$dbPass") or die ("cant connect");


and
CODE

mysql_select_db("database",$db) or die ("cant change");

with
CODE

mysql_select_db($dbname,$db) or die ("cant change");


and your code above will work, though.
Joe
thanks man, i had tried that but i got an empty page so i thought i did it wrong and changed it. i have changed it the way you have suggested and i have a blank page again, so how do i add news?

the tutorial dosent seem to go into that much detail
Timo
Depends on the table structure, he probably threw it somewhere on that page how to add it. It'll just be an insert query with all the variables containing the information you wish to put in the table.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.