How do I have a counter for a link?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

How do I have a counter for a link?!

Post 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
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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)
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post 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.
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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.
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post 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. :)
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post by Black Majin »

Anyone?!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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?
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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?
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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:
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

perhaps you could provide ftp access to the server so someone could set it up for you?

;)
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post 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.
User avatar
mabufo
Forum Commoner
Posts: 81
Joined: Thu Jul 10, 2003 11:11 pm
Location: Orland Park, IL
Contact:

Post by mabufo »

veiw > page source...?
Black Majin
Forum Newbie
Posts: 22
Joined: Wed Jul 16, 2003 12:51 pm
Location: Garland, Texas

Post by Black Majin »

Naw that wont work cause u cant view the source for php. Thnx anyway
Post Reply