Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Quick Question: TXT File or MySQL Database?

    I'm planning on making a small hit counter that anyone could sign up to use on a site. However, first I need to choose a way to store data. I could either store pageload/unique hits information and configuration settings in text files, or store it all in a database. I would think txt files would be faster, but I don't know. Which method should I use?

  • #2
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    A MySQL database would be much faster than a text file although for a simple hit counter is may more hassle to create so unless you are likely to experience a large amount of traffic then a text file may be equally acceptable.
    Basically if you are already using a MySQL database for other things and you feel it will be easy to add a hit counter into that then that is the most appropriate although for a low traffic easy solution a text file may be appropriate.
    Mike

  • #3
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    i have one on my site for download, i dont use it, but it works fine. it uses a txt file.

    PHP Code:
    <?php
        
    if (file_exists('count.txt')) 
        {
            
    $fil fopen('count.txt'r);
            
    $dat fread($filfilesize('count.txt')); 
            echo 
    $dat+1;
            
    fclose($fil);
            
    $fil fopen('count.txt'w);
            
    fwrite($fil$dat+1);
        }

        else
        {
            
    $fil fopen('count.txt'w);
            
    fwrite($fil1);
            echo 
    '1';
            
    fclose($fil);
        }
    ?>
    its the most simple i could make, so if you want mod it to your needs, i know your looking for somthing more complex, but sometimes non-complexity is better.

    you can use it if you want. just save it and use require or include to show it.

  • #4
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    The problem is, I don't know how much traffic it will get. I'm hoping for a lot...

    Thanks, rappa, but I already have a script like that that counts both total and unique visitors (using a cookie, though I may add support for IP address checking). Opening the file twice is a good idea. In mine, I opened it once to read and write, read it, truncated it, and wrote new data to it. I had some problems doing that way, but eventually (with help lol) got it to work.

    If I used text files, I would have made it so that it creates two per user--one that holds settings (like background and text colors, and maybe even stuff like a list of IP addresses to block) and one that contains the number of total and unique visitors. A database would probably be easier (especially since I can create counter ID numbers based on the number of rows in the database, instead of having to make a text file that counts this). I could even just use one table for both hits and settings, instead of two text files.

    Long story short, I'll probably just use a database. Thanks!

    Oh, and what might I have to worry about as far as security (like SQL injection)? Can I prevent this by just escaping strings?
    Last edited by thesmart1; 08-10-2007 at 12:53 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •