Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Clicking on a button for limited times

  1. #1
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Clicking on a button for limited times

    Hello dear friends ,

    Consider we have a button where you should click

    Is there anyone can help me and give the best idea how to make it limited

    i means after 20 clicking on that button , it goes de-active / or / image / or / text for example says ( no more clicking )


    Yes it could be very complex idea cause it may needs to recall a command at cronjob ( if i'm not wrong )



    So the full story :-

    i want a button that valid to be clicked 20 times per day , after the clicking 20 times , it gives msg says for example " come back 2morrow "


    thanks in advance and i know it maybe hard and may takes time
    but i really with that help , will open a new area into my mind
    Last edited by egturnkey; 07-31-2009 at 03:05 PM. Reason: thanks to " Jshor " for his amazing help

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

    Default

    is it 20 times per person per day or just 20 times per day? Either way, you could either write to a file or db to keep track of how many times it has been clicked either by ip address or total clicks.
    "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

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    egturnkey (07-26-2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    is it 20 times per person per day or just 20 times per day? Either way, you could either write to a file or db to keep track of how many times it has been clicked either by ip address or total clicks.
    It is 20 times per person per day

    just like some downloading websites, when you downloads many files after for example 20 downloads it gives you msg " no more download for you today "

    so it is yes as you've said 20 times per person per day


    i've tried to find any script has such function so i can isolate the code but i didn't find any , so i hope if someone can help me about that , making it as simple as it could

    thanks in advance

  5. #4
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    but it didn't works cause i've still some error and really not able to understand where to put thay query

    PHP Code:
     $time time(); 
    mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',$time)"); 
    so can you please check out the example and fix it
    Last edited by egturnkey; 07-29-2009 at 12:00 AM. Reason: removed the 1st example , read the next replies >>>

  6. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    In this case, your $time var is a timestamp [including mins, secs, etc]. Be less specific w/your time, so that it only includes the DATE and not anything else.

    This ought to fix it for ya:
    PHP Code:
    $time date("m/d/y");
      
    mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',$time)"); 
    Now, the next part would be to edit whatever is looking up the matching values from the db to associate w/a certain date:

    PHP Code:
    $date date("m/d/y");

    $qq mysql_query("SELECT * FROM ip_table WHERE ip='$_SERVER[REMOTE_ADDR]' AND date='$date'") or die(mysql_error());

    $num mysql_num_rows$qq ) or die(mysql_error());

    if(
    $num 20) {
    echo 
    "You can't click that. You've already clicked it 20 times.";
    } else {
    echo 
    "You can click that.";

    HTH
    - Josh

  7. The Following User Says Thank You to JShor For This Useful Post:

    egturnkey (07-28-2009)

  8. #6
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Have fixed the example as follow :-

    in the database your storing date as a varchar, change the type to be date. Mysql stores dates in the Y-m-d format so change $date = date ('Y-m-d'). You select query is also wrong to, it says date = when your insert (and db structure) is timestamp

    Code:
     $today_date = date("Y-m-d"); 
    mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".$_SERVER['REMOTE_ADDR']."','".$today_date."')"); 
    
    $qq = mysql_query("SELECT * FROM ip_table WHERE ip='".$_SERVER['REMOTE_ADDR']."' AND timestamp='".$today_date."'") or die(mysql_error()); 
    $num = mysql_num_rows( $qq ) or die(mysql_error()); 
    
    if($num > 20) { 
    echo "You can't click that. You've already clicked it 20 times."; 
    } else { 
    echo "You can click that."; 
    }


    Thanks to JShor from dynamicdrive.com forum ( idea of the code )
    Thanks to Shof515 from devshed.com forum ( fixed the code )


    Now we still have simple littel problem , if someone can help me


    it will store the IP/DATA when someone visit the page Not upon clicking on a button


    - can you please show me simple example where clicking on the button, will submit to the to the insert query to stroe IP and Data

    i mean , it count the click only on clicking a button not visiting the page .

    thanks in advance
    Last edited by egturnkey; 07-29-2009 at 10:23 AM.

  9. #7
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    OK, for this, the best solution would be to use ajax [or an iframe, if you're desperate].

    First page, page.php, where the object to be click will be:
    Code:
    <html>
    <head>
       var http_request = false;
       function makeRequest(url, parameters) {
          http_request = false;
          if (window.XMLHttpRequest) { // Mozilla, Safari,...
             http_request = new XMLHttpRequest();
             if (http_request.overrideMimeType) {
             	// set type accordingly to anticipated content type
                //http_request.overrideMimeType('text/xml');
                http_request.overrideMimeType('text/html');
             }
          } else if (window.ActiveXObject) { // IE
             try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
                try {
                   http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
             }
          }
          if (!http_request) {
             alert('Cannot create XMLHTTP instance');
             return false;
          }
          http_request.onreadystatechange = alertContents;
          http_request.open('POST', url + parameters, true);
          http_request.send(null);
       }
    </script>
    </head>
    <body>
    <?php $ip = $_SERVER[REMOTE_ADDR]; ?>
    Button:<br />
    <input type="button" value="Click here" onClick="makeRequest('get.php', '?IP=<?=$ip ?>');">
    Link: <br />
    <a href="javascript:makeRequest('get.php', '?IP=<?=$ip ?>');">Click here</a>
    Next, use the page to process the req [req.php]::
    PHP Code:
    <?php

    $ip 
    $_GET['ip'];

    $today_date date("Y-m-d"); 

    mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".$ip."','".$today_date."')"); 


    ?>
    It's pretty simple and straight forward, make changes at will.

    HTH
    - Josh

  10. The Following User Says Thank You to JShor For This Useful Post:

    egturnkey (07-30-2009)

  11. #8
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JShor View Post
    OK, for this, the best solution would be to use ajax [or an iframe, if you're desperate].
    thanks so much , however there are an error in javascript i guess

    here is the error :-




    here is the full example :-

    http://www.egcss.com/exp3.ra


    it is as following

    index.php ( with the js function makeRequest ) click on it will store IP and call

    get.php (req "req.php" which store the IP + it has the real download link )

    config.php --- dbconnection
    dabase.sql --- db


    so can you please check it out for that error

    thanks so much , i feel we almost done it

    your idea is very very great and simple , i did like it so much

  12. #9
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Alright, I made some changes to the script [and tested it on my server,works perfect]. The script will call ajax and record the number of times that the user clicked the object in the db, and after 20 clicks [or however many you set to] the object becomes disabled,a nd no longer clickable.

    Here's teh pages:

    index.php [script needs editing]
    Code:
    <?php
    
    include('config.php'); // connect to db
    
    $ip = $_SERVER[REMOTE_ADDR];
    $date = date("Y-m-d"); // mysql db format as requested.
    
    $qq = mysql_query("SELECT * FROM ip_table WHERE ip='$ip' AND timestamp='$date'") or die(mysql_error()); //select query
    
    if(mysql_num_rows( $qq ) > 0) {
    $int = mysql_num_rows( $qq ) or die(mysql_error()); // number of times clicked by user today
    } else {
    $int = 0;
    }
    
    ?>
    <html>
    <head>
    <script type="text/javascript"> 
       var myLinkURL = "http://mysite.com/"; // change this to what you want the link to say
       var timesToClick = 20; //times you wanna click the link before obj becomes disabled.
    // NO NEED TO EDIT BELOW THIS //
    
       var exp = parseInt(<?php echo $int; ?>); // number of times clicked [when page loaded] + 1more click..
       var http_request = false;
       function makeRequest(url, parameters) {
          http_request = false;
          if (window.XMLHttpRequest) { // Mozilla, Safari,...
             http_request = new XMLHttpRequest();
             if (http_request.overrideMimeType) {
             	// set type accordingly to anticipated content type
                //http_request.overrideMimeType('text/xml');
                http_request.overrideMimeType('text/html');
             }
          } else if (window.ActiveXObject) { // IE
             try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
                try {
                   http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
             }
          }
          if (!http_request) {
             alert('Cannot create XMLHTTP instance');
             return false;
          }
          http_request.open('POST', url + parameters, true);
          http_request.send(null);
          determineClickedTimes(); //function to call clicked times constraint
          
       }
    
    
    function determineClickedTimes() {
    	exp = parseInt(exp + 1);
          if(exp > parseInt(timesToClick - 1)) { //if obj is clicked {x} times
                document.getElementById('clicker').disabled = "disabled"; //disable the object
          } else {
                window.open(myLinkURL);
          }
    }
    </script>
    </head>
    <body>
    <?php $ip = $_SERVER[REMOTE_ADDR]; ?>
    <br /> <br />  Link:
    <a href="javascript:makeRequest('req.php', '?IP=<?php echo $ip; ?>');" id="clicker">Click here</a>
    </body>
    </html>
    get.php, what ajax calls:
    Code:
    <?php
    
    include('config.php');
    
    $ip = $_GET['IP'];
    
    $today_date = date("Y-m-d");
    
    mysql_query("INSERT INTO `ip_table` (`ip`,`timestamp`) VALUES ('".$ip."','".$today_date."')") or die(mysql_error());
    
    ?>
    HTH
    - Josh

  13. The Following User Says Thank You to JShor For This Useful Post:

    egturnkey (07-30-2009)

  14. #10
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JShor View Post
    Alright, I made some changes to the script [and tested it on my server,works perfect]
    thanks so much JShor
    that idea is also much more better , i've test it and works perfect

    However

    it may works for statics links but not dynamics one , for example
    at the following part

    Code:
    var myLinkURL = "http://mysite.com/"; // change the link
    how can i intert a dynmic link written in php as following, depends in other defines

    Code:
    <a href='".FILE_DOWNLOAD."?productID=".$productrow['productID']."'>

    that was the original
    ( if logged then download , if not then " Members Only " , if not both (lol) then the product not avl


    i've replaced the 1st part ( if logged ) with the code that limited the number of downloads which you've given to me ( all thanks to you ) but as i've told you , the problem will be in the link itself

    Code:
    <?
    if ($productrow['status']) {
    if ($productrow['type'] == 0 && ($isMember || $isAdmin)) {
    
    
    echo "<a href='#' onclick='append_cookie(\"cartcontent\", \"".$productrow['productID']."\", \"\")'>";
    echo "<img src='$addtocart_button' border=0>";
    echo "</a>";
    echo "<a href='".FILE_DOWNLOAD."?productID=".$productrow['productID']."'>";
    echo "<img src='$download_button' border=0>";
    echo "</a>";
    }
    
    else if ($productrow['type'] == 0 && (!$isMember && !$isAdmin)) {
    echo "<b>Member Download Only</b>";
    }
    
    
    } else { // product n/a for download or purchase
    echo "<img src='$na_button' border=0>";
    }
    ?>


    i know i've taken a lot of your time ,
    but it really helps me to release a simple php script and free to all , act as free/paid memberships for templates/scripts/downloadble downloads
    like dream templates and templates boxed art ..ect ( lol ) but much more simple

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
  •