Download Counter Error

Don't know how to solve it!

Page 1 of 1

2 Replies - 893 Views - Last Post: 14 June 2009 - 06:17 AM Rate Topic: -----

#1 UnknownCoder   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Download Counter Error

Posted 14 June 2009 - 05:14 AM

I am having trouble with my download counter php file. It runs fine and well but gives an error at the top of the page; I don't have a clue how to sort it! Here is the error message:

Notice: Undefined index: click in C:\Documents and Settings\Jonny\Desktop\Portable USB Web Server\UsbWebserver\Root\index.php on line 5


Here is my php file:

<?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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
</head>
<body>
	  <p>Total Downloads: <?php echo file_get_contents('counter.txt'); ?></p>
	<a href="?click=yes">Download</a>
</body>
</html>


Any help will be appreciated, thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Download Counter Error

#2 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: Download Counter Error

Posted 14 June 2009 - 05:37 AM

If you don't pass ?click=whatever to your script, the index does not exist in the $_GET array.

You can fix it by checking if it infact exists before you try to read it's value.
if(isset($_GET['click']) && $_GET['click'] == "yes") {

Was This Post Helpful? 1
  • +
  • -

#3 UnknownCoder   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 137
  • Joined: 22-February 09

Re: Download Counter Error

Posted 14 June 2009 - 06:17 AM

Thanks mate works a treat!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1