tags:

views:

58

answers:

1

I have this php code:

<?php
if(!file_exists('counter.txt')){
file_put_contents('counter.txt', '0');
}
if($_GET['click'] == 'yes'){
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>counter</title>
</head>
<body>
<h1><?php echo file_get_contents('counter.txt'); ?></h1>
<a href="?click=yes">clickMe</a>
</body>
</html>

It's supposed to count the amount of times someone clicks on a certain link.

I saved this code in a file called index.php, and then in the same directory I made a file called counter.txt(set the permissions of counter.txt to 666). However when I run the script it comes up with:

Fatal error: Call to undefined function: file_put_contents() in /home/index.php on line 6

How can I fix this error, and somehow display the count click on the same page as the link?

+3  A: 

If file_put_contents() is undefined, my guess is you're using a version of php < 5.

If that is the case, you will need to replace that function with fopen(), fwrite() and fclose(), see also the php manual page.

jeroen
Thanks @jeroen, luckily my webhoster lets me switch my version of php, fixed that right up, thanks a lot!
Tony C
You're welcome!
jeroen