Results 1 to 7 of 7

Thread: php question

  1. #1
    Senior Member
    Join Date
    Mar 2002
    Posts
    166

    php question

    guys i need to make a hit counter...

    and the hit count to appear on a single html....

    plz help


    ty

  2. #2
    Senior Member
    Join Date
    Mar 2002
    Posts
    502
    What server do you have access to? Do you have access to a database, and what kind? How experienced are you with PHP, and how far did you get when trying to code it yourself?

    BTW, if you really need one there are already tons of those available on the net. You could try hotscripts.com for instance, or good 'ol google. In my oppinion, hit counters don't really add any significant value to a website, though I created a lot of those when I was bored lol.

    I could give you code but that wouldn't really teach you anything. IMO, the best way to learn from it is try to do it yourself, and when you can't figure something out ask around about the specific problem you're dealing with. If you just want code, try hotscripts, they usually have such things laying around.

    Franky.
    Bleh.

  3. #3
    I wrote a simple PHP script that can do what he wants, but I fully surrender to Sick Dwarf's elitist questions.

    Code:
    <?php
    $filename = "visitors.txt";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    $contents++;
    echo($contents);
    $fp = fopen('visitors.txt', 'w');
    fwrite($fp,$contents);
    ?>
    A text file will be created where-ever the file with the code is and it will keep the number of visitors. The echo() statement prints it out and you can use HTML to purrty it up.

    - dave

  4. #4
    Make sure the text file has the correct permissions to be written to if you want to use GW's script.

  5. #5
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    If you have access to a mysql db you might be able to use this.

    includes/auth.php
    PHP Code:
    <?php
    $db_host 
    "localhost";
    $db_user "root";
    $db_pass "password";
    $db_name "yourdb";
    ?&
    gt
    put this in your index page
    PHP Code:
    if(!isset($_COOKIE["hits"])){
        
    setcookie("hits""true", (time() + (60*30)));
        
    updateHits();

    what that does is it makes a cookie so that it doesn't increment the hit counter for every single hit, only one hit per 30 minutes per user. That way if someone sits there and refreshes it doesn't increment. But then again they could delete their cookies or not use them at all.
    Lastly put this function in the index page also, the last piece of code as well as this once must be withing the <?php ?> tags.
    PHP Code:
    function updateHits(){
        require(
    "./includes/auth.php");
        
    $connection mysql_connect($db_host$db_user$db_pass) or die("Unable to connect!");
        
    $query "SELECT hits FROM hits";
        
    mysql_select_db($db_name);
        
    $result mysql_query($query) or die ("Error in query.");
        
    $oldhits mysql_fetch_assoc($result);
        
    $hits $oldhits['hits'] + 1;
        
    mysql_query("UPDATE hits SET hits = '".$hits."'") or die ("Error in query.");
        
    $string $hits;
        
    $im = @imagecreate(5025)
           or die(
    "Cannot Initialize new GD image stream");
        
    $background_color imagecolorallocate($im000);
        
    $text_color imagecolorallocate($im255255255);
        
    imagestring($im555,  $string$text_color);
        
    imagejpeg($im"/home/user/public_html/images/hits.jpg"95);
        
    imagedestroy($im);

    that actually makes an image with the amount of hits inside it. You must have a table for hits though which you can make like this using the mysql client (make sure you're within the correct db by using "use yourdb")
    Code:
    create table hits (
    hits smallint not null
    );
    And that in a nutshell is my hit counter. To make that image you must have gd enabled with your php and also maybe libjpeg. I hope you're able to make sense of that and that I didn't leave anything out. I was pulling bits of code from lots of places cause it's all within includes and stuff. Peace.

  6. #6
    Senior Member
    Join Date
    Mar 2002
    Posts
    166
    ty all... but to exlpain u what i want to do is:....

    i have made this Private Servers Status to appear on a png file through fsockopen with php.
    http://www.worldofwarcraft.gr/stats.php

    and i want somewhere in the image to make appear the counter.

    i will try making it with God's Whore way ... looks easier and then if i fail i will try the other ways....

    ty guys


    Sick Dwarf... well i have all the control of the server.... mysql4... for the php i know some things... but i don't have enough time to try making the counter by myseld due to school....


    ty!!!!

  7. #7
    Senior Member
    Join Date
    Mar 2002
    Posts
    166
    ok i made it only because you helped me. Without you i wouldn't have made it

    ty for the priceless help

Posting Permissions

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