Page 1 of 2

How do I have a counter for a link?!

Posted: Wed Jul 16, 2003 12:51 pm
by Black Majin
Ok look below, i'm lookin for a script that can count the total clicks on a link when u do a mouse over like shown below. Plz help
Image

Posted: Wed Jul 16, 2003 1:05 pm
by daven
Just store a counter. You don't need a database, just a text file with the value. When someone clicks on the link, increment the counter (you would do that on the next page)

Posted: Wed Jul 16, 2003 1:08 pm
by Black Majin
Yeah I know everyone keeps saying that its so easy to write but I dont know anything about php. Do u know where I can find a script like that, or maybe u can tell me how to write one.

Posted: Wed Jul 16, 2003 1:26 pm
by daven
Lets say the link goes to "page2.php", and that you have file named "counter.txt" that contains an integer (the number of times the link has been clicked).

on page2.php, put the following code

Code: Select all

<?
// only process if you came from index.php
if($_SERVER['HTTP_REFERER']=="www.yoursite.com/index.php"){ // or whatever the index page is named
  // open counter.txt and read the current value
  $fp=fopen("counter.txt","r");
  $counter=fread($fp,filesize("counter.txt"));
  // increment the value
  $counter++;
  fclose($fp);
  // write the value
  $fp=fopen("counter.txt","w");
  fwrite($fp,$counter);
  fclose($fp);
}
?>
I am certain that there is a way to do this without closing and re-opening the file, but my brain is not really working at the moment.

Posted: Wed Jul 16, 2003 1:28 pm
by Black Majin
nooooo lol, I want it to count the number of times someone clicks a link that goes to a different site lol. :)

Posted: Wed Jul 16, 2003 6:05 pm
by Black Majin
Anyone?!

Posted: Wed Jul 16, 2003 6:19 pm
by Gen-ik
Black Majin wrote:nooooo lol, I want it to count the number of times someone clicks a link that goes to a different site lol. :)
The script posted above by daven will do the trick.

Why do you want to do this anyway?

Posted: Wed Jul 16, 2003 6:44 pm
by Black Majin
I just said that I want the link to go to a diff site. I dont understand how i'm suppose to put some code on someone elses site...
All man Ive asked this same question on like 5 diff forums and not one person can give me the code for that script

Posted: Wed Jul 16, 2003 6:55 pm
by Gen-ik
This will work..

Instead of linking directly to the new site the user will be taken through this page without knowing it.

Just put this code into a file called link.php or something and then in your link(s) href put ... href="links.php?sitename=name_of_website"

Then when that link is used the php page loads, updates the count, and then goes directly to the site.

You will need to add your sites into the code below..

Code: Select all

<?php

//list of links
if($sitename=="site_name_one") { $countfile = "site_name_one.txt"; $url="http://the_sites_web_address"; }
if($sitename=="site_name_two") { $countfile = "site_name_two.txt"; $url="http://the_sites_web_address"; }

// open count file and read the current value 
// make sure the .txt files are in the same folder as the php file

$fp=fopen($countfile,"r"); 
$counter=fread($fp,filesize($countfile)); 
// increment the value 
$counter++;
fclose($fp); 
// write the value 
$fp=fopen($countfile,"w"); 
fwrite($fp,$counter); 
fclose($fp); 

header('location:'.$url);

?>

..why didn't you just ask the person who put your site together?

Posted: Wed Jul 16, 2003 9:04 pm
by Black Majin
ahhhh nvm still not how its like on http://www.shiver7.com just forget it. And i'm the one who put my site together LOL
http://saiyanarmy.paradoxquest.com

Posted: Wed Jul 16, 2003 9:17 pm
by Gen-ik
WHAT IS YOUR PROBLEM?!?!

The script above does exactly what you asked for..... it counts the number of times a user goes to a different site via a link!

If this isn't what you wanted then try learn some f**king english and ask your question properly.

Don't come onto this site, ask a question, and then get moody because no-one has a clue what they are supposed to be helping you with.


:evil:

Posted: Wed Jul 16, 2003 9:32 pm
by macewan
perhaps you could provide ftp access to the server so someone could set it up for you?

;)

Posted: Wed Jul 16, 2003 10:59 pm
by Black Majin
SRY man but i'm not the one moody its u. I mean I just dont see anywhere in that code where it shows the amount of clicks when u do a mouse over. Maybe i'm wrong but calm down.

Posted: Wed Jul 16, 2003 11:23 pm
by mabufo
veiw > page source...?

Posted: Wed Jul 16, 2003 11:26 pm
by Black Majin
Naw that wont work cause u cant view the source for php. Thnx anyway