Simple way to count outbound clicks through my redirect.php?

Discussion in 'PHP' started by Kerosene, Aug 4, 2008.

  1. #1
    I need to setup a simple counter for my outbound link page, redirect.php.

    My basic php knowledge can increment a number in a txt file everytime somebody clicks through redirect.php - but is there a more elegant way?
     
    Kerosene, Aug 4, 2008 IP
  2. MartinGr

    MartinGr Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A simple txt based solution would look like this. Just create a file called count.txt to the same directory where your code is located.

    if(file_exists("count.txt"))
    {
      $file = fopen("count.txt", "r");
      $count = fgets($exist_file, 255);
      $count++;
      fclose($file);
      $file= fopen("count.txt", "w");
      fwrite($file, $new_count);
      fclose($file);
    }
    PHP:
    However I'd advise you to go for a database driven solution. Do you have MySql on your webserver?
     
    MartinGr, Aug 4, 2008 IP
  3. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Create count.txt, Chmod 777.

    Better code.

    if(file_exists("count.txt"))
    {
      $count = file_get_contents('count.txt');
      $count++;
      $file= fopen("count.txt", "w");
      fwrite($file, $count);
      fclose($file);
    }
    PHP:
    Jay
     
    jayshah, Aug 4, 2008 IP