Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: click counter when link is pressed

  1. #1
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default click counter when link is pressed

    Hi,

    I would like to incorporate a counter that counts up every time a link is clicked.

    I don't want to use a database...just one link that says "click here" and a counter displayed right next to the link that starts at 0 and counts up every time the link is clicked. I would also like the browser to be automatically reloaded after the link is clicked so that the user can see the number go up by one each time it is clicked.

    Is this easy using php?

    Any help would be appreciated. Maybe someone could just point me in the right direction, I don't even know where to start.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Ft. Smith, AR
    Posts
    795
    Thanks
    57
    Thanked 129 Times in 116 Posts

    Talking

    I'm not real good with PHP, but I wrote a small javascript that will do what you're asking for...

    Code:
    <HTML>
     <HEAD>
      <TITLE> Clicker Counter Thingamajig </TITLE>
    </head>
    
    <body>
    
    
    <script type="text/javascript">
    
    var clicks = 0;
    function linkClick(){
        document.getElementById('clicked').value = ++clicks;
    }
    
    document.write('<a href="#" onclick="linkClick()">Click Me!</a>');
    
    
    </script>
    
    You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.
    
    
    
     </BODY>
    </HTML>
    --------------------------------------------------
    Reviews, Interviews, Tutorials, and STUFF
    --------------------------------------------------
    Home of the SexyBookmarks WordPress plugin

  3. #3
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Quote Originally Posted by kimberlyhello View Post
    I would like to incorporate a counter that counts up every time a link is clicked.

    [...]

    Is this easy using php?
    For a beginner I would say, "Yes and no"; it's very simple, but the hard part is the one that actually records and retrieves the count. And it must be somehow recorded outside of PHP, because a PHP execution is both finite and one-for-one (i.e. one execution of PHP can only respond to one client request).

    Quote Originally Posted by kimberlyhello View Post
    I don't want to use a database...
    If you don't want a database (a collection of huge files) then how about a simple file?

    Quote Originally Posted by kimberlyhello View Post
    Any help would be appreciated. Maybe someone could just point me in the right direction, I don't even know where to start.
    Start with the Filesystem Functions, particularly file_get_contents, file_put_contents, and file_exists.

    And make sure to ask in this thread if you get stuck.
    Last edited by Jesdisciple; 07-20-2008 at 07:14 PM.

  4. #4
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
    <?php
    if(!file_exists('counter.txt')){
      
    file_put_contents('counter.txt''0');
    }
    if(
    $_GET['click'] == 'yes'){
      
    file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
      
    header('Location: ' $_SERVER['SCRIPT_NAME']);
      die;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>counter example</title>
    </head>
    <body>
      <h1><?php echo file_get_contents('counter.txt'); ?></h1>
      <a href="?click=yes">clickMe</a>
    </body>
    </html>

  5. #5
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default thx for helping...

    Thanks Nyne Lyvez, Jesdisciple, and techietim. I really appreciate all of your help.

    Nyne Lyvez - thanks for your javascript. I just wished that the numbers on the counter were permanent.

    Jesdisciple - Yes, a simple file would be ok to use instead of a database. Sort of like what techietim posted here.


    techietim - I tried using your script. First I made a txt file called counter.txt and then I uploaded the script that you posted. But it didn't work. And how would I get the numbers to be displayed on the same page?

    I will try to use this script and just keep working on it. Thank you again.

  6. #6
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    @kimberlyhello

    You may have to CHMOD the counter.txt file to 666

  7. #7
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    techietim - How do I do the CHMOD thing? How do I change it to 666?

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    When in your FTP client, you should be able to right click on the file and choose something along the lines of permissions or chmod or something to that effect.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks. I got the chmod to 666. But the script is still not working.

    Any other ideas? Am I still doing something wrong?

    I am using (techietim's script): I named it test.html and also have a text file named counter.txt in the same folder as test.html

    PHP Code:
    <?php
    if(!file_exists('counter.txt')){
      
    file_put_contents('counter.txt''0');
    }
    if(
    $_GET['click'] == 'yes'){
      
    file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
      
    header('Location: ' $_SERVER['SCRIPT_NAME']);
      die;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>counter example</title>
    </head>
    <body>
      <h1><?php echo file_get_contents('counter.txt'); ?></h1>
      <a href="?click=yes">clickMe</a>
    </body>
    </html>
    !

    Just after writing the above post, I realized that test.html should be test.php. After I changed that, it worked perfectly!

    Thanks to all who helped me. I am still learning and so I always appreciated the help.

    One last question (which is very important for what I am trying to accomplish on my website) - How do I make it so that I can have multiple different unique links that all work the same way as this one. For example, if I have a link like this: http://www.mysite.com/profile.php?uid=30 but I also what to have all the other user id links as well (http://www.mysite.com/profile.php?uid=29 and http://www.mysite.com/profile.php?uid=27 and so on). But I want them to all count up independently. Can I just add "?click=yes" to the end of the link some how? help!

    Thanks.
    Last edited by kimberlyhello; 07-25-2008 at 04:11 AM. Reason: I figured it out!

  10. #10
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    techietim??? Are you still there?

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
  •