# Solved: click count code...



## paulb100 (Mar 17, 2005)

Im creating a downloads site ... is there a simple code I can add to hyperlinks so that the number of clicks (downloads) appears right next to it?

thanks


----------



## Techmonkeys (Feb 10, 2005)

What are you creating the site in ?

I am guessing it will be php and mysql, you can then simply just store a value in the database for the link being clicked.


----------



## paulb100 (Mar 17, 2005)

sorry ive not got as far as using PHP MySQL ...YET.. ah well I thought it may have required a database... 

Im using Adobe CS3 and 110MB free hosts


----------



## Techmonkeys (Feb 10, 2005)

So how does the download site work? Are you having to put everything in to the page manually as links?

That is a much harder way of doing things really.

Get yourself a free (or cheap) hosting company (I can recommend www.tsohost.co.uk) that supports php, mysql then set about learning them as it will make life much simpler in the long run.

You can then set yourself up a small admin interface so you can browse for the file, fill in the details like, file name, version etc and then click upload and it will auto fill in everything else for you.


----------



## paulb100 (Mar 17, 2005)

yeh i was thinking about that last night... i dunno how to use databases etc yet ive yet to study my video tutorials - im just trying to get the home-page on the site done so ive got the basic layout (nothing pro hehe)

but yeah i was just gunna do it all manually - im with 110mb free hosts


----------



## Techmonkeys (Feb 10, 2005)

If you do decide to go down the php/mysql route then I recommend "building your own database driven website" by Kevin Yank.

Everything is clearly laid out and made easy.

http://www.sitepoint.com/books/phpmysql1/

Can read a few of the sample chapters here: http://www.sitepoint.com/books/phpmysql1/samplechapters.php


----------



## MMJ (Oct 15, 2006)

http://forums.techguy.org/showthread.php?t=621265#post5096856


----------



## paulb100 (Mar 17, 2005)

thanks can i use the same code in that link if my host supports PHP?


----------



## Techmonkeys (Feb 10, 2005)

do you mean if you host doesnt support PHP? is so then I am afraid not, they have to support it for it to run.

However 110 do support PHP

http://www.110mb.com/plans.php

@MMJ, nice code


----------



## MMJ (Oct 15, 2006)

paulb100 said:


> thanks can i use the same code in that link if my host supports PHP?


That code *is* PHP.



DMcCabe said:


> @MMJ, nice code


simple is beautiful.


----------



## paulb100 (Mar 17, 2005)

no i meant exactly what i said - will I be abe to use the code if my host support PHP - yeah 110mb do support it but its not in with the free package


----------



## Techmonkeys (Feb 10, 2005)

According to that list PHP 4 & 5 are both supported on the free hosting


----------



## paulb100 (Mar 17, 2005)

no , you have to pay a one time payment to be able to use it, thats whatv they do for all there extras , one payment for each thing you want


----------



## Techmonkeys (Feb 10, 2005)

It deffinitely says it is free on their site (see attachement)


----------



## paulb100 (Mar 17, 2005)

sorry you are right- its free - everything else like MySQL etc isnt

ah well am pleased about that - so what do I do to get the counter code in? do I create a .php file with the code in from the link above then use the code he provided in the counter example to retrieve it?

great stuff , thanks for showing me PHP is free


----------



## Techmonkeys (Feb 10, 2005)

phew I thought I was going mental.

Yes you basically open notepad and put the contents of code in the file and save it as *download.php* and then on your website where you would normally put


```
<a href="pathtoyourfile">filename</a>
```
you instead put:


```
<a href="download.php?file=pathtoyourfile">filename</a>
```
Then wherever you want to view the amount of downloads you put the code:


```
<?php
include('count.txt');
?>
```


----------



## paulb100 (Mar 17, 2005)

*Warning*: Cannot modify header information - headers already sent by (output started at /www/110mb.com/d/o/w/n/l/o/a/d/download-central/htdocs/download.php:1) in */www/110mb.com/d/o/w/n/l/o/a/d/download-central/htdocs/download.php* on line *7

*thats the error from the site when i click on the download


----------



## paulb100 (Mar 17, 2005)

the code in the PHP is wrong for what i need it for... I have done what you said above but the PHP file needs to be specific for my needs

this is code:
* <?php
$filename = 'count.txt';
if (!file_exists($filename))
file_put_contents($filename, 1);
else
file_put_contents($filename, file_get_contents($filename)+1);
header("Location:".$_GET['file']);
?> *


----------



## Techmonkeys (Feb 10, 2005)

so that is in a file of its own yes? not with any other code?

Also can you show us the code of the page with the download link

Tip: when posting code put [code ] and [ /code] (without the spaces) round it.


----------



## paulb100 (Mar 17, 2005)

code in downloads.php


```
<?php
$filename = 'count.txt';
if (!file_exists($filename))
    file_put_contents($filename, 1);
else
    file_put_contents($filename, file_get_contents($filename)+1);
header("Location:".$_GET['file']);
?>
```
code in dreamweaver


```
click <a href="download.php?file=http://download.mozilla.org/?product=firefox-3.0.3&amp;os=win&amp;lang=en-US">HERE</a> - <?php
include('count.txt');
?> downloads</p></td>
```
thanks for your help


----------



## paulb100 (Mar 17, 2005)

ive moved the " from 
href="download.php?

to

file="http://d

cos it just doesnt seem right but it still doesnt work - line 7 of the PHP code is wrong aparantley

It says summit about GETFILE..? im not wanting it to get any file hehe im just thinking cos the code was made for someone who wanted it to record each time someone went to the page and download his PDF...? I just my counter to tick over when someone clicks on the link


----------



## Techmonkeys (Feb 10, 2005)

Right, it doesnt seem to work when you downloading from another site.

Try it like this, all in 1 file:


```
click [URL=http://download.mozilla.org/?product=firefox-3.0.3&os=win&lang=en-US]HERE[/URL] - 
<?php
$filename = 'count.txt';
if (!file_exists($filename))
    file_put_contents($filename, 1);
else
    file_put_contents($filename, file_get_contents($filename)+1);
?>

<?php
include('count.txt');
?> downloads

[/TD]
```


----------



## paulb100 (Mar 17, 2005)

is that for the HTML code in web page??? when I put it in it deletes most my stuff off the page

if its for thr PHP file is it supposed to have 
downloads

[/TD]
on the end?


----------



## Techmonkeys (Feb 10, 2005)

Sorry the /td thing is just how I copied it from your code above.

Basically with PHP you can use HTML and PHP code in the same file, all the PHP stuff needs to be surrounded by <?php and ?> so it knows what is what.

So if you take the code I gave you there and put it in your page it should work. The page however must be saved as .php and not .html or .htm


----------



## paulb100 (Mar 17, 2005)

excellent my friend - that works


----------



## Techmonkeys (Feb 10, 2005)

hoorah!!! bit of an epic but we got there  

TX to MMJ for the code.

Dont forget to mark the thread as solved.


----------



## paulb100 (Mar 17, 2005)

is it still using the downloads.php file? or the count.txt? I will have to change the counts.txt to summit like firefoxcount.txt so i can have one for each download - i will have to save all my pages as .php


----------



## Techmonkeys (Feb 10, 2005)

it's not using the downloads.php any more and yes you can rename the file.txt to whatever you like, but make sure you change it in the code too.


```
click [URL=http://download.mozilla.org/?product=firefox-3.0.3&os=win&lang=en-US]HERE[/URL] - 
<?php
$filename = 'filename.txt';
if (!file_exists($filename))
    file_put_contents($filename, 1);
else
    file_put_contents($filename, file_get_contents($filename)+1);
?>

<?php
include('filename.txt');
?> downloads

[/TD]
```


----------



## MMJ (Oct 15, 2006)

DMcCabe said:


> Right, it doesnt seem to work when you downloading from another site.
> 
> Try it like this, all in 1 file:
> 
> ...


That isn't a good idea as every page refresh will increase the download count, whether the user downloaded the file or not.

The reason why you received that error as mentioned in post #17 is becuase of the preceding spaces on line.


```
<?php //THE SPACES AT THE BEGINNING OF THIS LINE IS WHAT CAUSED THE ERROR!
$filename = 'count.txt';
if (!file_exists($filename))
    file_put_contents($filename, 1);
else
    file_put_contents($filename, file_get_contents($filename)+1);
header("Location:".$_GET['file']);
?>
```
So remove those spaces and it should work fine.


----------



## paulb100 (Mar 17, 2005)

ARRGHH!! your totally right- 
http://download-centra.110mb.com/

it was going up and up on each visit/refresh

so now with new code what do i do?


----------



## MMJ (Oct 15, 2006)

paulb100 said:


> ARRGHH!! your totally right-
> http://download-centra.110mb.com/
> 
> it was going up and up on each visit/refresh
> ...


Go back to the old code and remove those spaces.


----------



## paulb100 (Mar 17, 2005)

i didnt have a space at the beginning of the code... it said the error was in line 7 summit to do with headers....


----------



## MMJ (Oct 15, 2006)

paulb100 said:


> i didnt have a space at the beginning of the code... it said the error was in line 7 summit to do with headers....


*sigh*



paulb100 said:


> [(output started at /www/110mb.com/d/o/w/n/l/o/a/d/download-central/htdocs/download.php:1)


line 1 of the file, there is spaces or its not a plain ascii file. but most probably there are spaces as you posted.


----------



## paulb100 (Mar 17, 2005)

i finally got it to work after playing around with it - I also wasnt aware that ORIGINALLY I was supposed to save the page as .PHP ... I thought the PHP code worked from within an HTML file.....

thanks


----------



## MMJ (Oct 15, 2006)

paulb100 said:


> i finally got it to work after playing around with it - I also wasnt aware that ORIGINALLY I was supposed to save the page as .PHP ... I thought the PHP code worked from within an HTML file.....
> 
> thanks


Sure


----------



## paulb100 (Mar 17, 2005)

thanks again...
http://download-central.110mb.com/
there you can see it in action....


----------

