Can anyone tell me the PHP CODE to make a simple counter, that counts the number of people who enter my homepage? I want the numbers to be desplayed in text, not a picture..

    Simpliest way to acheive this is by incrementing a variable each time a user access your site, and then store this variable in a txt file. You could do it that way ;

    // Getting current count
    $fp = fopen ("path/to/your/logfile/log.txt", "r");
    $count = fgets($fp, 1000);
    fclose($fp);

    // Increment by one
    $count++;

    echo "Your are visitor #" . $count . "on this website";

    // Open log file in write mode
    $fp = fopen ("path/to/your/logfile/log.txt", "w");
    fwrite ($fp, $count);
    fclose($fp);

    Very basic counter, really. Upon refresh, it will log again, and again, regardless of the client's IP.

    Hope this helps

      8 months later

      Yes Mr Dude (ftrudeau) you definately do rock.
      I'm a PHP newbie coming from Flash actionscripting and that was most helpful.

      Cheers from AOK

        If you want to prevent counter increments when people refresh, you can put an if statement around the increment code:

        //set date value for the cookie
        $Vcookie = date("l, F jS, Y");
        //check for cookie 
        if ($_COOKIE["Vdate"] == "")
        	{
        //if not present then set cookie that will expire in 12 hrs
        		setcookie ("Vdate", $Vcookie,time()+43200);
        
        // Increment by one 
        $count++; 
        
        }
        

        this gives you a more accurate idea of visits (although if someone has their cookies disabled it screws up the count)

          what do you do when you get...
          Warning: fopen(http://154.5.91.147/catacomb1/counter.txt): failed to open stream: HTTP wrapper does not support writeable connections. in C:\apache\Apache2\htdocs\catacomb1\logon.php on line 20
          this happens with safemode on or off, and whats that mean on if you wnat it relaxed off and its strict?

            The reason for this error is that you cannot write to a file via HTTP. This is simply the way HTTP is - it is not a limitation of PHP or a bug. If you want to access the file, the best way is to do so through filesystem access (if not the only way!)

            To do this, change "fopen('http... etc.." to

            fopen('/path/to/file/on/server','w');
            

            (I think that I wrote this correctly...)

            Hope it helps!

              i figured it would be easy, lol.

              here is the code:
              <?php
              $myip = '154.5.91.147';

              $file = fopen ("http://$myip/catacomb1/", "r");
              if (!$file) { echo "<p>Unable to open remote file.\n"; exit; }
              while (!feof ($file)) { $line = fgets ($file, 1024); if (eregi ("<title>(.*)</title>", $line, $out)) { $title = $out[1]; break; }}
              fclose($file);

              echo "<p><h3>$title</h3></p>";
              $file = fopen ("http://$myip/catacomb1/counter.txt", "r");
              if (!$file) { echo "<p>Unable to open remote file.\n"; exit; }
              while (!feof ($file)) {
              $line = fgets ($file, 1024);
              $cou = $line+1;
              break;
              }
              fclose($file);

              $file = fopen ("http://$myip/catacomb1/counter.txt", "w");
              if (!$file) { echo "<p>Unable to open remote file.\n"; exit; }
              fwrite($file, $cou);
              break;
              fclose($file);

              ...

              the rest of the script
              ?>

              the title line finder works and outputs the page title correctly,
              but it gets me error above opon trying to write to the file

              ps ty for help.

                yaya! yo got it, not directly , but you got it ::::

                $file = fopen ("counter.txt", "w");<<<worked

                $file = fopen ("http://$myip/catacomb1/counter.txt", "w"); <<< gave error

                this works for me!!
                [glow=blue]ty silent[/glow]

                  Write a Reply...