IPB

Welcome Guest ( Log In | Register )


Welcome Guest, Register to Remove this Message!
3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> [Intermediate] Outward Link Counter, Count how many times links are clicked
Timo
post Feb 9 2004, 06:08 PM
Post #1


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



Well, Joe asked for one, so here goes.
This will allow you to create a script so when you click a link on a page it will direct you to the link and count up how many times it has been clicked.


This is the SQL query to use on our link counter.
If you don't know how to add this using phpmyadmin, refer to Leo's tutorial.
CODE

#
# Table structure for table `LinkCounter`
#

DROP TABLE IF EXISTS `LinkCounter`;
CREATE TABLE `LinkCounter` (
 `ID` int(11) NOT NULL auto_increment,
 `name` text NOT NULL,
 `address` text NOT NULL,
 `description` text NOT NULL,
 `count` int(11) NOT NULL default '0',
 KEY `ID` (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=1;


The next file I have named to (linkout.php) If you wish to switch this name you must also switch it in the display code accordingly. I don't suggest you switch it unless you know what you're doing.


Remember to include your database information or an external file with these variables.
CODE

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


CODE

<?php

#include your database variables

function dbinsans($text) {
$text = strip_tags ($text, "");
$text = str_replace(chr(10),"",$text);
$text = str_replace(chr(13), "<br>", $text);
$text = str_replace("\"","*",$text);
$text = str_replace("'","*",$text);
$text = addslashes($text);
return($text);
}
#This function is used to check the input, a hacker could break the query
#and use his own query if we're not careful and don't check this stuff

$link=dbinsans($_GET['link']);
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM LinkCounter WHERE ID='$link'";

$result = mysql_query ($requete,$db);
#Should return only one value, our link if it exists

$article = mysql_fetch_object($result);
#Gives an object with our row back

$addy = $article->address;
#Gets the address of the link

$count = $article->count + 1;
#Gets and adds one to counter variable

$sql = "UPDATE LinkCounter  SET count='$count' WHERE ID='$link'";
#Updates count

mysql_query($sql, $db);
#Queries the database and updates the count at ID.

mysql_free_result($result);

header("Location: ".$addy);
#like Leo's tutorial this redirects the browser
exit();
?>



This next piece of code can go where you want the links to go; name the page you put it on whatever you want
alternatively if you only want certain links to appear you could manipulate the query, maybe by adding another field with a section name and using the WHERE command in the query to match it.
Remember to include a file with your database variables or type them out.
CODE

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



CODE

<?php
#include database variables
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);


$requete = "SELECT * FROM LinkCounter";
$result = mysql_query ($requete,$db);

while ($article = mysql_fetch_object($result))
{
echo '<a href="linkout.php?link='.$article->ID.'">'.$article->name.'</a> Clicked ('.$article->count.') Times - '.$article->description.'<br />';
}

mysql_free_result($result);
?>


This, too, can go where you want.
Admin Section Script, remember to include the database variables. This section will allow you to easily add links... I highly suggest you put this in an admin section or a password protected directory.

Remember to include a file with your database variables or type them out.
CODE

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



CODE

<?php
if (!isset($_POST['submit'])) {
echo '<form method="post" action="'.$_SERVER['SCRIPT_NAME'].'" />';
echo 'Name Of Link : <input type="text" name="name" /><br />';
echo '<br />';
echo 'URL of Link (Include http://) : <input type="text" name="address" /><br />';
echo '<br />';
echo 'Description of Link : <input type="text" name="description"><br /><br />';
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />";
echo '</form>';
}
else {
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);

# I think we can trust our own input since this should be in a protected spot
$name = $_POST['name'];
$address = $_POST['address'];
$description = $_POST['description'];


$sql="INSERT INTO LinkCounter (name, address, description) VALUES ('$name','$address', '$description')";

mysql_query($sql, $db);
echo 'Thank you your input it is now added.!';


}

?>



Edit 2/11/04: Oops messed up on a line of code in the submitting thing, fixed it.
Edit 10/03/04: Gave the pages some names. tongue.gif

This post has been edited by Timo: Oct 3 2004, 03:01 PM


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post
Josh
post May 28 2004, 11:04 PM
Post #2


Facetious
*******

Group: Members
Posts: 2402
Joined: 3-February 04
From: NC
Member No.: 12



OK timo, i was reading this because I plan on doing something like this soon.

I don't understand how the $_GET['link'] thing is working.

And that second snippet of code with the while loop is just there to echo all of the links onto the page instead of doing it manually?


--------------------
Go to the top of the page
 
+Quote Post
Timo
post May 28 2004, 11:43 PM
Post #3


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



QUOTE (Josh @ May 28 2004, 11:04 PM)
OK timo, i was reading this because I plan on doing something like this soon.

I don't understand how the $_GET['link'] thing is working.

And that second snippet of code with the while loop is just there to echo all of the links onto the page instead of doing it manually?

1st. $_GET['Name'] grabs a variable sent with the URL

2nd. Yep that's what it does. smile.gif


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post
Josh
post May 28 2004, 11:45 PM
Post #4


Facetious
*******

Group: Members
Posts: 2402
Joined: 3-February 04
From: NC
Member No.: 12



Ah, ok. And in this example the variable is the ID number.


--------------------
Go to the top of the page
 
+Quote Post
Throwbacks
post Jun 11 2004, 05:15 PM
Post #5


Newbie
*

Group: Members
Posts: 23
Joined: 30-May 04
Member No.: 127



Thanx this code works... I was wondering if u could show me how i would add a button for the link cuz i wanna see how many times the button link has been clicked?


--------------------
user posted image
Go to the top of the page
 
+Quote Post
Josh
post Jun 12 2004, 02:15 AM
Post #6


Facetious
*******

Group: Members
Posts: 2402
Joined: 3-February 04
From: NC
Member No.: 12



Instead of text as the links just use an image..
<a href=""><img></a>


--------------------
Go to the top of the page
 
+Quote Post
Timo
post Jun 12 2004, 03:51 AM
Post #7


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



QUOTE (Josh @ Jun 12 2004, 02:15 AM)
Instead of text as the links just use an image..
<a href=""><img></a>

Yep, when you add a link use <img src="addy.jpg"> as the name.


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post
Throwbacks
post Jun 12 2004, 03:54 AM
Post #8


Newbie
*

Group: Members
Posts: 23
Joined: 30-May 04
Member No.: 127



Ight thanx! O yeah say i want to display the amount something has been clicked on a different page like by the download... like on Here I have my scripts on there and I want to show the amount of how many times it has been clicked instead of going Here How can I do that?


--------------------
user posted image
Go to the top of the page
 
+Quote Post
Timo
post Jun 12 2004, 04:43 AM
Post #9


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



<?php
#include database variables
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);


$requete = "SELECT * FROM LinkCounter";
$result = mysql_query ($requete,$db);

while ($article = mysql_fetch_object($result))
{
echo '<a href="linkout.php?link='.$article->ID.'">'.$article->name.'</a><br />';
}

mysql_free_result($result);
?>


for the page and


<?php
#include database variables
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);


$requete = "SELECT * FROM LinkCounter";
$result = mysql_query ($requete,$db);

while ($article = mysql_fetch_object($result))
{
echo '<a href="linkout.php?link='.$article->ID.'">'.$article->name.'</a> Clicked ('.$article->count.') Times - '.$article->description.'<br />';
}

mysql_free_result($result);
?>


to display


That would work...
It's easy to fix that up; all the code you need is already there.


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post
Throwbacks
post Jun 12 2004, 01:32 PM
Post #10


Newbie
*

Group: Members
Posts: 23
Joined: 30-May 04
Member No.: 127



OK thanx I will try it now! biggrin.gif


--------------------
user posted image
Go to the top of the page
 
+Quote Post
Timo
post Jun 12 2004, 02:01 PM
Post #11


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



QUOTE (Throwbacks @ Jun 12 2004, 01:32 PM)
OK thanx I will try it now! biggrin.gif

Remember to include your database variables.


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post
Throwbacks
post Jun 12 2004, 02:47 PM
Post #12


Newbie
*

Group: Members
Posts: 23
Joined: 30-May 04
Member No.: 127



Ok got ya! biggrin.gif


--------------------
user posted image
Go to the top of the page
 
+Quote Post
SERBIA
post Oct 3 2004, 08:12 AM
Post #13


Newbie
*

Group: Members
Posts: 10
Joined: 3-October 04
Member No.: 303



got one question what are the pAGEs called biggrin.gif
Go to the top of the page
 
+Quote Post
Joe
post Oct 3 2004, 11:21 AM
Post #14


* legend *
Group Icon

Group: Moderators
Posts: 5166
Joined: 31-January 04
From: The Valleys
Member No.: 8



linkout.php

CODE
<?php

#include your database variables

function dbinsans($text) {
$text = strip_tags ($text, "");
$text = str_replace(chr(10),"",$text);
$text = str_replace(chr(13), "<br>", $text);
$text = str_replace("\"","*",$text);
$text = str_replace("'","*",$text);
$text = addslashes($text);
return($text);
}
#This function is used to check the input, a hacker could break the query
#and use his own query if we're not careful and don't check this stuff

$link=dbinsans($_GET['link']);
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM LinkCounter WHERE ID='$link'";

$result = mysql_query ($requete,$db);
#Should return only one value, our link if it exists

$article = mysql_fetch_object($result);
#Gives an object with our row back

$addy = $article->address;
#Gets the address of the link

$count = $article->count + 1;
#Gets and adds one to counter variable

$sql = "UPDATE LinkCounter  SET count='$count' WHERE ID='$link'";
#Updates count

mysql_query($sql, $db);
#Queries the database and updates the count at ID.

mysql_free_result($result);

header("Location: ".$addy);
#like Leo's tutorial this redirects the browser
exit();
?>


links.php

CODE
<?php
#include database variables
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);


$requete = "SELECT * FROM LinkCounter";
$result = mysql_query ($requete,$db);

while ($article = mysql_fetch_object($result))
{
echo '<a href="linkout.php?link='.$article->ID.'">'.$article->name.'</a> Clicked ('.$article->count.') Times - '.$article->description.'<br />';
}

mysql_free_result($result);
?>


smile.gif hope that helps


--------------------
[http://www.joe2torials.com/] Your guide to (X)HTML, CSS, PHP, ASP, Photoshop, Web Management, Accessibility, Standards, Semantics and much more...
[http://www.rough-draft.co.uk/] My NEW webdesign portfolio.

Read my Weblog.
Go to the top of the page
 
+Quote Post
Timo
post Oct 3 2004, 02:58 PM
Post #15


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



haha well you could name any of the pages what you want besides the linkout.php page. ;D I didn't make that very clear though so I'll edit my post.


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post

3 Pages V   1 2 3 >
Reply to this topicStart new topic

 



Lo-Fi Version
Euribor
Reviews
Time is now: 2nd September 2010 - 11:38 PM