question about adding .txt

Discussion in 'Programming' started by izlik, Oct 4, 2010.

  1. #1
    Hello

    if i visit my page lets say http://mydomain.com/25 when someone visits this page there should be a number added to 25.txt so if there is a 0 in that txt file it should become 1. +1 each time basiclly. And if 25.txt do not exist it should be created, can this be done somehow in either php/html ?
     
    izlik, Oct 4, 2010 IP
  2. sourabhj

    sourabhj Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello
    well , its possible, and can be achieved easily, but i think why to write in a File, i would recommend using a Database...but if you want to get it done in file , its ok, it can be done..you need to follow simple login ,
    1) Check the last part of url hxxp://mydomain.com/25 , which is 25 in this case...Store it in a variable say $filename.
    2) use php file handling function to check if the file exists or not,
    3) if exists , read the file(one line only), and check if its number or not and increment it .and save it to file... you are done
    4)and if the file doesnt exists create the file , and store 1 in the file, and close the file....

    Hope this helps, it wont be difficult to accomplish this, but if you need any help ,let me know, ill try to post the code for you... Cheers :)
     
    sourabhj, Oct 4, 2010 IP
  3. mmua

    mmua Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Why not files? Files are great and in many cases outperform database. So I'd implement it with .htaccess and simple script.
    Something like this:

    .htacess:
    
    RewriteEngine On
    RewriteRule ^([0-9]+)$ counter.php?id=$1 [L]
    
    Code (markup):
    counter.php:

    
    <?
    $id = $_GET["id"];
    $fname = sprintf("%d.txt", $id);
    $f = fopen($fname, 'c+');
    if(flock($f, LOCK_EX )) {
      $data = fread($f, filesize($fname)?filesize($fname):1);
      $data = intval($data) + 1;
      ftruncate($f, 0);
      rewind($f);
      fwrite($f, $data);
      flock($f, LOCK_UN);
    }
    ?>
    
    Code (markup):
    Just basic idea ;)
     
    mmua, Oct 5, 2010 IP
  4. sourabhj

    sourabhj Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello

    Well this Idea is also nice...and easy to implement,But i just gave information about it and option to use Database too, and i agree in some cases files outperforms database, but for the rest we have to use Database.. Btw your Coding style is nice.. I like it... :)
     
    sourabhj, Oct 6, 2010 IP
  5. mmua

    mmua Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you, sourabhj.
    You are completely right on databases. In many cases they are more simple to use and implement more complex things.
     
    mmua, Oct 7, 2010 IP