This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-01-28 14:38:45

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Link view counter

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?


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#2 2012-01-28 15:07:35

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Link view counter

Bump


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#3 2012-01-28 15:29:17

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Link view counter

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?

Offline

 

#4 2012-01-28 15:54:55

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Link view counter

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)


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#5 2012-01-28 15:55:27

zippynk
Scratcher
Registered: 2011-07-23
Posts: 500+

Re: Link view counter

This is called web analytics. There are several sites out there that do web analytics, but here is a list of some of them. [note-link to google]


https://dl.dropbox.com/u/60598636/trifocal_interlude_soundcloud_button.png

Offline

 

#6 2012-01-28 16:00:57

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Link view counter

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)

Last edited by sparks (2012-01-28 16:05:14)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#7 2012-01-28 16:19:15

hello12345678910
Scratcher
Registered: 2009-07-11
Posts: 100+

Re: Link view counter

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  tongue


http://tinyurl.com/8yt32o9 http://tinyurl.com/6tgwp5r || Fish = F+I+S+H = 6+9+19+8 = 42<<The answer to Life, the Universe and Everything

Offline

 

#8 2012-01-28 16:20:32

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Link view counter

Oh no, I'm no expert, I just pick the odd thing or two up  tongue  Do you have a website host with SQL services? And do you know how to create databases on them?


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#9 2012-01-28 16:43:47

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Link view counter

Sparks: I don't like your indents, they are so random and hard to read.  tongue  No offense of course.  wink

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.

Code:

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

 

#10 2012-01-28 17:54:20

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Link view counter

Magnie wrote:

Sparks: I don't like your indents, they are so random and hard to read.  tongue  No offense of course.  wink

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...

Last edited by sparks (2012-01-28 17:56:01)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#11 2012-01-28 18:31:25

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Link view counter

sparks wrote:

Magnie wrote:

Sparks: I don't like your indents, they are so random and hard to read.  tongue  No offense of course.  wink

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.  smile  Maybe it's your Text editor?

Offline

 

#12 2012-01-28 18:55:27

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: Link view counter

You could do that or just use bit.ly.


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

Board footer