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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Find difference in current time and time in db

    Hello I have the following code


    Code:
    $ip = getenv("REMOTE_ADDR") ; 
     Echo "Your IP is " . $ip; 
     echo "<br>";
     
    //find out if the ip address is inside the database
    $doesIpExsist = mysql_query("Select * from login_information where IP_Address like '$ip'", $link); 
    $num_rows = mysql_num_rows($doesIpExsist);
    
    echo "$num_rows Rows\n";
    
    if ($num_rows == 0)
    {
    	//create the field inside the database
    	$ip_query = "INSERT INTO login_information (IP_address, `Login_attempts`, Login_time, Total_login_attempts) VALUES ('$ip', '1', NOW(), '1')";
    	$ip_result = mysql_query($ip_query, $link) or die (mysql_error());
    }
    else  //the fields alaready exsist
    {
    $curDate = date("Y-m-d H:i:s", time());
    
    echo $curDate;
    
     
      
    	$select_ip_id = "SELECT id from login_information where IP_address like '$%ip'";
    	$ip_error = mysql_query($select_ip_id, $link) or die (mysql_error());
    	$get_ip_time = "Select Login_time from login_information where IP_address like '$ip'";
    	$get_ip_error = mysql_query($get_ip_time, $link) or die (mysql_error());
    	
    	$sql = mysql_query("SELECT TIMEDIFF($curDate, $get_ip_error) AS difference FROM Login_time"); 
    	$result = mysql_fetch_array($sql, $link) or die mysql_error()); 
    //echo $result['difference'];  
    	
    
    	
    
    	
    	//$timeDifference = time_diff ($get_p_error, $curDate);
    	//echo $timeDifference;
    	//$timeDifference = $get_ip_error - $curDate;
    	//echo "<br>";
    	//echo $timeDifference;
    	//mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
    	
    	//$ip_query = "UPDATE INTO login_information (IP_address, `Login_attempts`, Login_time, Total_login_attempts) VALUES ('$ip', '1', NOW(), '1')";
    echo "So far so good";
    
    
    }
    ?>

    But the problem is that I can't figure out how to subtract one date\time from another date\time.

    Basically I want to stop people from hacking into pages...I figured the best way was to take the IP address of the person, count how many times they have looked at the login page. Then I would ban them for 5 minutes if they looked at the login page more than 5 times. The login page only ask for the username and password and nothing else.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Tell us how this is formatted: Login_time
    Give an example of what one of those time values look like.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #4
    New Coder
    Join Date
    Feb 2008
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    One of the examples of the date\time is

    2012-01-30 02:43:13

    but some of them maybe days\months and possible years.

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by golffor1 View Post
    One of the examples of the date\time is

    2012-01-30 02:43:13

    but some of them maybe days\months and possible years.
    So the date times all take different formats? Different from YYYY-MM-DD HH:MM:SS? Why are they all different? You need to define a set standard for storing your data in order to make comparisons. Once you've done that, use the datetime object and dateinterval like Fou-Lu posted in the link I sent you.

    You might want to consider just using a timestamp instead of a visual date and time combination, then using the dateinterval to give a better display.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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