I am looking for a way to count how many times a link is clicked on. I know that this can be done in php somehow with redirecting, but I do not know php. Can anybody help?
Offline
Bump
Offline
<?php // Insert some code to import the current number of "$counter" $counter = $counter + 1 // There is probably another way to do this // Insert some code here to save the counting number ?>
Could you explain what way you want to do it? Like if you want to use MySQL or a file to save the number of times a page has been clicked?
Offline
Magnie wrote:
Code:
<?php // Insert some code to import the current number of "$counter" $counter = $counter + 1 // There is probably another way to do this // Insert some code here to save the counting number ?>Could you explain what way you want to do it? Like if you want to use MySQL or a file to save the number of times a page has been clicked?
I would be able to specify a link, like scratch.mit.edu, and when the PHP script is run it would read the data from a file, add one to it and save it. Then the PHP script would redirect to the link i specified. I have no idea what MySQL is but I see it everywhere, I think it's some sort of database. I just wanted to know how many times people click links in my sig and forum topics. I just don't know the code for importing and saving the counting #
BTW, thanks very much
Last edited by hello12345678910 (2012-01-28 15:55:27)
Offline
For this you will need a site host with a MySQL database. I actually have a script you can copy that I wrote the other day for a charity traffic link on my site.
<?php
//count then redirect script by Sparks
mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error()); //connect to the database using your username and password
mysql_select_db("DATABASE_NAME") or die(mysql_error());//connect to your counting database table.
$ip= $_SERVER['REMOTE_ADDR']; //this sets the variable $ip to the IP address of the viewer. Used to count number of unique visits.
$exists = false; //state that the user who is visiting has never visited before until proven otherwise.
$result = mysql_query("SELECT * FROM table_name");//select all entries to the database.
while($row = mysql_fetch_array($result)) //run through each one and check to see if the IP matches the IP of that line.
{
if($row['ip'] == $ip){ //if IP matches an IP in the table...
mysql_query("UPDATE table_name SET visits=visits+1 WHERE ip='$ip'") or die(mysql_error()); ... change the view count of that IP by 1...
$exists = true; ... and state that they HAVE visited before.
}
}
if(!$exists){ // if the user has NOT been found already by IP in the table....
mysql_query("INSERT INTO table_name VALUES('', '$ip', '1')") or die(mysql_error()); //add the IP to the table with the view count of 1.
}
?>
<p>Your view has been counted. If you are not automatically redirected from this page, <a href = "LINK_TO_REDIRECT_PAGE">click here</a></p> <!-- A message with a link in case the redirect fails. -->
<meta http-equiv="REFRESH" content="0;url=LINK_TO_REDIRECT_PAGE"> <!--Redirect the user without caching the count page -->it requires a database with the following columns:
id (int) auto increment
ip (varchar)
views (int)
Last edited by sparks (2012-01-28 16:05:14)
Offline
sparks wrote:
For this you will need a site host with a MySQL database. I actually have a script you can copy that I wrote the other day for a charity traffic link on my site.
Code:
<?php //count then redirect script by Sparks mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error()); //connect to the database using your username and password mysql_select_db("DATABASE_NAME") or die(mysql_error());//connect to your counting database table. $ip= $_SERVER['REMOTE_ADDR']; //this sets the variable $ip to the IP address of the viewer. Used to count number of unique visits. $exists = false; //state that the user who is visiting has never visited before until proven otherwise. $result = mysql_query("SELECT * FROM table_name");//select all entries to the database. while($row = mysql_fetch_array($result)) //run through each one and check to see if the IP matches the IP of that line. { if($row['ip'] == $ip){ //if IP matches an IP in the table... mysql_query("UPDATE table_name SET visits=visits+1 WHERE ip='$ip'") or die(mysql_error()); ... change the view count of that IP by 1... $exists = true; ... and state that they HAVE visited before. } } if(!$exists){ // if the user has NOT been found already by IP in the table.... mysql_query("INSERT INTO table_name VALUES('', '$ip', '1')") or die(mysql_error()); //add the IP to the table with the view count of 1. } ?> <p>Your view has been counted. If you are not automatically redirected from this page, <a href = "LINK_TO_REDIRECT_PAGE">click here</a></p> <!-- A message with a link in case the redirect fails. --> <meta http-equiv="REFRESH" content="0;url=LINK_TO_REDIRECT_PAGE"> <!--Redirect the user without caching the count page -->it requires a database with the following columns:
id (int) auto increment
ip (varchar)
views (int)
Thanks sparks I've been led to believe that you have everything
Offline
Sparks: I don't like your indents, they are so random and hard to read.
No offense of course.
If you want to track each page individually and not count the IP addresses, this is probably a script also similar to something like tinyurl.com or any other tiny url site.
<?php
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$url_id = $_GET['id'];
$result = mysql_query("SELECT * FROM table_name WHERE id='$url_id'");
$row = mysql_fetch_array($result);
if ($row) {
mysql_query("UPDATE table_name SET visits=visits+1 WHERE id='$url_id'")
echo '<meta http-equiv="REFRESH" content="0;url='.$row['page'].'">';
} else {
echo "Page doesn't exist";
}
?>Offline
Magnie wrote:
Sparks: I don't like your indents, they are so random and hard to read. tongue No offense of course.
![]()
Those indents look odd to me too... they weren't like that when I posted...
$result = mysql_query("SELECT * FROM roxyTraffic");
while($row = mysql_fetch_array($result)){
if($row['ip'] == $ip){
mysql_query("UPDATE roxyTraffic SET visits=visits+1 WHERE ip='$ip'") or die(mysql_error());
$exists = true;
}
}
if(!$exists){
mysql_query("INSERT INTO roxyTraffic VALUES('', '$ip', '1')") or die(mysql_error());
}
?>still weird?
EDIT: Yes... still weird. How odd..
EDIT: fixed, I think...
Last edited by sparks (2012-01-28 17:56:01)
Offline
sparks wrote:
Magnie wrote:
Sparks: I don't like your indents, they are so random and hard to read. tongue No offense of course.
![]()
Those indents look odd to me too... they weren't like that when I posted...
Code:
$result = mysql_query("SELECT * FROM roxyTraffic"); while($row = mysql_fetch_array($result)){ if($row['ip'] == $ip){ mysql_query("UPDATE roxyTraffic SET visits=visits+1 WHERE ip='$ip'") or die(mysql_error()); $exists = true; } } if(!$exists){ mysql_query("INSERT INTO roxyTraffic VALUES('', '$ip', '1')") or die(mysql_error()); } ?>still weird?
EDIT: Yes... still weird. How odd..
EDIT: fixed, I think...
Yeah, tons better.
Maybe it's your Text editor?
Offline