Advanced Search

Page 1 of 9 123 ... LastLast
Results 1 to 10 of 89

Thread: Site Counters ...

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Site Counters ...

    Hello all ,

    Well again as usual I need to know something and this time it is to do with site counters... I simply just want to know how to do that

    Any help will be greatly appreciated

    (by site counters i mean that every time the page is visited the counter goes up by one and anyone can see the updated value)

  2. #2
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It would need to be done server side. I would keep an entry that stores the number of hits somewhere in a database. Then when someone views the page, I would increment the count in the database, and then display the new count.

    I would probably also use a php session to keep track of users, so that the counter only gets incremented once if someone is browsing through the page.

    I could help you out setting one up if you want.
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    One of the very basic PHP counters (doesn't read I.P):
    Code:
    <?php
    $cdata = file_get_contents("counter.txt");
    $ndata = ($cdata+1);
    $handle = fopen("counter.txt","w+");
    fwrite($handle,$ndata);
    fclose($handle);
    echo "\n<br>".$cdata." visits";
    ?>
    - Mike

  4. #4
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mburt View Post
    One of the very basic PHP counters (doesn't read I.P):
    Code:
    <?php
    $cdata = file_get_contents("counter.txt");
    $ndata = ($cdata+1);
    $handle = fopen("counter.txt","w+");
    fwrite($handle,$ndata);
    fclose($handle);
    echo "\n<br>".$cdata." visits";
    ?>
    Wow, that's nifty. I never considered using a file.

    To expand on that, here's how you would use sessions to only increase the count once per visit:

    Code:
    // do this stuff from here ...
    session_start();
    
    $cdata = file_get_contents("counter.txt");
    $ndata = $cdata;
    
    if (!isset($_SESSION['user_id']))
    {
    	$_SESSION['user_id'] = 'user';
    	$ndata = ($cdata+1);
    	$handle = fopen("counter.txt","w+");
    	fwrite($handle,$ndata);
    	fclose($handle);
    }
    //... to here BEFORE sending any data to the browser.
    
    echo "\n<br>".$ndata." visits";
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  5. #5
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Wow,
    you guys are great at this !

    Except there may be a few problems - i do not know how to conect a php file to html or how php works (but i will start learning that to - just not yet)

  6. #6
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The file name needs to end in .php, and any php script needs to go between php tags (<?php and ?>).

    For example,

    Code:
    <html>
    <head>
    
    <?php
    
     echo '<title>This is php!</title>';
    
    ?>
    
    </head>
    <body>
    
    <?php
    
    echo 'this is more php!';
    
    ?>
    
    </body>
    </html>
    The server will parse this and send the following to your web browser:

    Code:
    <html>
    <head>
    
    <title>This is php!</title>
    
    </head>
    <body>
    
    this is more php!
    
    </body>
    </html>
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  7. #7
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Right, So how would add one of the above php codes into the html file

  8. #8
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Put this in your file:

    Code:
    <html>
    <head>
    
    <?php
    
     echo '<title>This is php!</title>';
    
    ?>
    
    </head>
    <body>
    
    <?php
    
    echo 'this is more php!';
    
    ?>
    
    </body>
    </html>
    Then save the file as something.php, upload it to your server, and then pull it up (from your server) in a web browser.

    It won't be an html file, it will be a php file, but it can still have html in it.

    For example, your main page would be index.php instead of index.html.
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  9. #9
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thats not what i ment
    You gave the counter script and thats what i want combined wit hsome html down below

  10. #10
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Before i do that though do i need to download php to my ("borrowed") server and my computer ?
    Do you know were to ge it from ? - i am using talktalk's server

Bookmarks

Posting Permissions

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