将 PHP 中的时间作为字符串进行比较

发布于 2024-12-04 10:45:16 字数 212 浏览 2 评论 0原文

我需要一个方法来获取两个表示 DateTime 的字符串(在 MySql 语法中)并返回它们之间的时间差。
然后我需要将该时间与 3 秒 进行比较,以便我可以阻止对我的服务器的暴力攻击。

我在 Google 上搞砸了很多事情,我设法获得了 DateTime 对象的字符串表示形式,但我无法设法转换和比较它们。

I need a method that gets two strings that represents a DateTime (in the MySql syntax) and returns the time difference between them.
Then I need to compare that time to 3 seconds so I could block a Brute Force attack on my server.

I've messed a lot with Google and I managed to get the string representation of the DateTime object, but I can't manage to convert and compare them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰之心 2024-12-11 10:45:16
$time_str1 = '2011-09-10 19:59:23'; // first string datetime from DB
$time_str2 = '2011-09-10 19:59:24'; // second string datetime from DB

// first DateTime object created based on the MySQL datetime format
$dt1 = DateTime::createFromFormat('Y-m-d H:i:s', $time_str1);

// second DateTime object created based on the MySQL datetime format
$dt2 = DateTime::createFromFormat('Y-m-d H:i:s', $time_str2);

// difference comparison to check if at least 3 seconds have passed
if ( ($dt2->format('U') - $dt1->format('U')) > 3) {
    echo 'Ok, no brute force'; // yes, three seconds have passed
} else{
    echo 'We got you newbie'; // nope, three second haven't passed
}
$time_str1 = '2011-09-10 19:59:23'; // first string datetime from DB
$time_str2 = '2011-09-10 19:59:24'; // second string datetime from DB

// first DateTime object created based on the MySQL datetime format
$dt1 = DateTime::createFromFormat('Y-m-d H:i:s', $time_str1);

// second DateTime object created based on the MySQL datetime format
$dt2 = DateTime::createFromFormat('Y-m-d H:i:s', $time_str2);

// difference comparison to check if at least 3 seconds have passed
if ( ($dt2->format('U') - $dt1->format('U')) > 3) {
    echo 'Ok, no brute force'; // yes, three seconds have passed
} else{
    echo 'We got you newbie'; // nope, three second haven't passed
}
复古式 2024-12-11 10:45:16
$diffInSeconds = $dateTimeLater->format('U') - $dateTimeFirst->format('U');
$diffInSeconds = $dateTimeLater->format('U') - $dateTimeFirst->format('U');
一梦等七年七年为一梦 2024-12-11 10:45:16

strtotime( $timeStr ) 它将转换为自纪元以来的秒数 http:// /en.wikipedia.org/wiki/Unix_epoch 。然后你就可以使用标准的数学运算符。请注意,strtotime 有时可能不准确。要转换回来,date("mdY H:i:s", $time)

strtotime( $timeStr ) which will convert to the amount of seconds since epoch http://en.wikipedia.org/wiki/Unix_epoch . Then you can just use standard mathematical operators. Be warned, strtotime can be inaccurate sometimes. To convert back, date("m-d-Y H:i:s", $time)

む无字情书 2024-12-11 10:45:16

这是使用会话的替代方法,无需查询数据库的时间戳:

<?php 
session_start();

function post_check($limit){
    //Check for first time
    if(isset($_SESSION['post_check'])){
        //Check for count on failed 
        if(isset($_SESSION['post_check_count'])){
            //If fail count is more then 3 block till user closes down browser
            if($_SESSION['post_check_count']>$limit){
                die('Too many requsets to the server, please close down your browesr and try again.');
            }
        }else{
             //Set for count on failed
            $_SESSION['post_check_count']=0;
        }

        //Check (time-limit) against timestamp held in session
        if(($_SESSION['post_check']+$limit)<=time()){
            //Update timestamp
            $_SESSION['post_check']=time();
            //Ok
            return true;
        }else{
            //Update Fail count
            $_SESSION['post_check_count']++;
            //Fail
            return false;
        }
    }else{
        //Set for first time
        $_SESSION['post_check']=time();
        return true;
    }
}


//Pretty self explanitry here
if(post_check('3')===true){
    echo 'Allowed: handle post stuff here';
}else{
    echo'Too many requests within given time limit do nothing';
}
?>

Heres an alternative method using a session, no need to query a db for a timestamp:

<?php 
session_start();

function post_check($limit){
    //Check for first time
    if(isset($_SESSION['post_check'])){
        //Check for count on failed 
        if(isset($_SESSION['post_check_count'])){
            //If fail count is more then 3 block till user closes down browser
            if($_SESSION['post_check_count']>$limit){
                die('Too many requsets to the server, please close down your browesr and try again.');
            }
        }else{
             //Set for count on failed
            $_SESSION['post_check_count']=0;
        }

        //Check (time-limit) against timestamp held in session
        if(($_SESSION['post_check']+$limit)<=time()){
            //Update timestamp
            $_SESSION['post_check']=time();
            //Ok
            return true;
        }else{
            //Update Fail count
            $_SESSION['post_check_count']++;
            //Fail
            return false;
        }
    }else{
        //Set for first time
        $_SESSION['post_check']=time();
        return true;
    }
}


//Pretty self explanitry here
if(post_check('3')===true){
    echo 'Allowed: handle post stuff here';
}else{
    echo'Too many requests within given time limit do nothing';
}
?>
苦行僧 2024-12-11 10:45:16

在 MySQL 端:

SELECT UNIX_TIMESTAMP(my_time) FROM my_table

在 PHP 端,您将获得以秒为单位的 UNIX 时间戳,您可以对其进行比较。

On the MySQL side:

SELECT UNIX_TIMESTAMP(my_time) FROM my_table

On the PHP side, you then have UNIX timestamps in seconds, which you can compare.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文