timing and php?

Discussion in 'PHP' started by whiteblue1942, Apr 22, 2008.

  1. #1
    how do i make timed events like once a user clicks somthing run the script in 20 minutes? or once they run a script they cant do it again for another 10 minutes or somthign?
     
    whiteblue1942, Apr 22, 2008 IP
  2. superyetkin

    superyetkin Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    PHP is certainly not the best language to achieve such effects, what you can only do is to use sleep function to stop execution of a script for a specified amount of time.
     
    superyetkin, Apr 22, 2008 IP
  3. whiteblue1942

    whiteblue1942 Peon

    Messages:
    573
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hmm not the response i was hoping for haha thanks anyways tho
     
    whiteblue1942, Apr 22, 2008 IP
  4. superyetkin

    superyetkin Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Could you specify what you are looking for exactly? I would do it in Javascript if I were you...
     
    superyetkin, Apr 22, 2008 IP
  5. itcn

    itcn Well-Known Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #5
    THis is easy. You can use PHP to simply log the IP address and timestamp of the user that clicked the action into a MySQL table. Then whenever someone clicks your action, check their IP against the table. If the timestamp < 20 minutes ago, just disable the action. Easy as pie.
     
    itcn, Apr 22, 2008 IP
  6. whiteblue1942

    whiteblue1942 Peon

    Messages:
    573
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yea but what is the actual code for < 20 minutes ago? i just cant type that in
     
    whiteblue1942, Apr 23, 2008 IP
  7. itcn

    itcn Well-Known Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #7
    Well, I guess it's easy if you've been coding for a few years :)

    You do a select against the time column in the table. For example, to make sure someone can't click the action again, you would run the MYSQL statement, "SELECT myIpAddress FROM myTable WHERE myIpAddress = '$userIpAddress' AND hour(myDate)> 17 and minute(myDate) > 52"

    This will make sure the time is greater than 17:52, or 5:52. You'll have to do the PHP statement which subtracts 20 minutes from the current time, but this SQL should help get you started.
     
    itcn, Apr 23, 2008 IP
  8. madmax728

    madmax728 Banned

    Messages:
    620
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Use cookies to store time. Every time user browses your web pages use time() function to check the current time and the one stored in the cookie. Code is going to be a bit complex though.
    More secure way is to store IP address and first visit time in mysql table and then continuously check the time in the database with that of the current time. This will waste time though and is a inefficient way if you get 1000's of visitors.
     
    madmax728, Apr 23, 2008 IP
  9. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #9
    Here're the "complete" scripts. I haven't checked / run this script, sorry if there is an error.

    Create a table : named something, i use tbl_user_log for an example.
    Here are the columns :
    1. log_id => int primary key
    2. user_ip => varchar 64 not null
    3. last_action => big int 15 not null default 0
    For example, there is a simple form.
    Every 20 mins, a user can submit the form and get "OK" message.
    If that user submits more than once every 20 mins, s/he will get "ERROR" message.

    We'll create 3 file :
    1. config.php => for configuration or such
    2. index.php => where the user views the form
    3. submit.php => here's the form target
    ----------

    config.php
    
    <?php
    define('TIME_LIMIT', 60 * 20);// in seconds
    
    // put your connection code here
    mysql_connect('localhost', 'root', '') or die(mysql_error());
    mysql_select_db('your_db') or die(mysql_error());
    ?>
    
    PHP:
    ----------

    index.php
    
    <html>
    ...
    <form action="submit.php">
    <input type="submit" value="Let's go" />
    </form>
    ...
    </html>
    
    PHP:
    ----------

    submit.php
    
    <?php
    require('config.php');
    
    // Delete expired record(s)
    $now = time();
    
    // we'll delete record(s) with time less or equal to this value
    $limit = $now - TIME_LIMIT;
    
    $q = "DELETE FROM tbl_user_log WHERE last_action <= $limit";
    mysql_query($q);
    
    
    
    
    // Get user's ip
    $user_ip = $_SERVER['REMOTE_ADDR'];
    // $user_ip .= $_SERVER['HTTP_X_FORWARDED_FOR'];
    
    // just to make all records have same length :)
    $user_ip = md5($user_ip);
    
    // Lets check in our database
    $q = "SELECT COUNT(*) FROM  tbl_user_log WHERE user_ip = '$user_ip'";
    $q = mysql_query($q);
    $r = mysql_fetch_array($q);
    
    // flag to define if the submission is OK
    $isOk = false;
    
    $isOk = ($r[0] < 1);
    
    if ($isOk) {
          // everything's OK
          // let's record it
          $q = "INSERT INTO tbl_user_log (user_ip, last_action) VALUES ('$user_ip', $now)";
          mysql_query($q);
    
          // echo our message
          echo("OK");
    } else {
          echo("Error, please wait.");
    }
    ?>
    
    PHP:
     
    xrvel, Apr 23, 2008 IP
  10. itcn

    itcn Well-Known Member

    Messages:
    795
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #10
    xrvel, that is a great example of my recommendation!
     
    itcn, Apr 23, 2008 IP
  11. nihcer

    nihcer Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #11
    You could use shared memory(application cache), eAccelerator, xCache, APC, or something
     
    nihcer, Apr 24, 2008 IP