Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Saving current time in variable

    This is probably a real newbie question, but I was wondering how one would set the value of the current time in a variable to compare with the new time after some interval has passed. The following code doesn't work but illustrates my reasoning more or less:

    <?

    $var = time() + 30;

    if (time() < $var) {
    echo "Not enough time has passed.";
    exit;
    }
    else {
    echo "You may try logging in again.";
    }

    ?>

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you're wanting to store the time and then check when the person last tried logging in then the best way is to use a session variable. If you assign a session variable when the person posts their login form, then if they try and post it again before X time has passed, generate an error message, eg, this would need some work to fit in with your login form but should give you the basic idea:
    PHP Code:
    <?php
    session_start
    ();
    if(isset(
    $_POST['login'])) {

        
    // let's assume we process the form here and the person isn't logged in

        
    if(!isset($_SESSION['login_time'])) {
            
    $_SESSION['login_time'] = time(); // if the time isn't stored (ie it's the first login attempt) then store it now
        
    } else {
            
    $limit 30// limit logins to once every 30 seconds
            
            
    if((time() - $_SESSION['login_time']) < $limit) {
                echo 
    'You can only make one login attempt every ' $limit ' seconds.';
            } else {
                
    // show your login form here
                
    unset($_SESSION['login_time']); // let's unset the session now so that if the login fails again the process starts over
            
    }
        }
    }
    ?>

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    try
    $now = time();
    $future = time() + 30;

    if(($future-$now)>30){
    error
    }else{
    success
    }

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That makes no sense. Using delinear's example would be better.

  • #5
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That seems to work, thanks!

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Dezit
    That seems to work, thanks!
    which way worked for you, i personally used the one i posted

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by emehrkay
    which way worked for you, i personally used the one i posted
    I'm not entirely sure what that's meant to do. Surely if you do:

    $now = time();
    $future = time() + 30;

    Then $future - $now will almost always return exactly 30. Since both $now and $future will almost always have the same timestamp in that script it would be the same as saying (0+30) - 0.

    The only time you won't return 30 is if the server has a problem and it takes over a second to process that bit of code, or if the script just happens to run as the time increments by one second, in which case it will always return 31.

  • #8
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by delinear
    I'm not entirely sure what that's meant to do. Surely if you do:

    $now = time();
    $future = time() + 30;

    Then $future - $now will almost always return exactly 30. Since both $now and $future will almost always have the same timestamp in that script it would be the same as saying (0+30) - 0.

    The only time you won't return 30 is if the server has a problem and it takes over a second to process that bit of code, or if the script just happens to run as the time increments by one second, in which case it will always return 31.
    lol yeah you're right. i used it with a timestamp that i put in the database, so thats why it works for me. yeah the old time would have to be something that has been set before.


  •  

    Posting Permissions

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