【问题标题】:Php microtime() is not working on live site. Looking for workaroundphp microtime() 无法在实时站点上运行。寻找解决方法
【发布时间】:2015-09-07 19:23:01
【问题描述】:

我在 Windows 上安装了 Zend Server 6.3 和 Php 5.4。并且系统运行良好。现在我将代码移动到实时站点,该站点使用 DirectAdmin 在 Ubuntu 服务器上运行 Php 5.3.29。所有其他网站都在那里运行良好。但是我当前的网站给了我这个错误(该网站在 WordPress 4.3 上):

Warning: mysql_connect(): Headers and client library minor version mismatch.
Headers:50541 Library:50623 in /home/cheapauto/domains/*DOMAIN*/public_html/wp-includes/wp-db.php on line 1482
Parse error: syntax error, unexpected '[' in /home/*USER*/domains/*DOMAIN*/public_html/wp-content/plugins/*MY-PLUGIN*/includes/final.class.NRSBooking.php on line 101 

行是这样的:

$now = explode(' ', microtime())[1];

而我的整个插件功能是这样的:

private function getIncrementalHash($length = 5)
{
    //$charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    $charset = "ABCDEFGHIJKLMNPRSTUVYZ"; // fits LT & EN, O is skipped to similarity to Zero
    $charsetLength = strlen($charset);
    $result = '';

    $now = explode(' ', microtime())[1];
    while ($now >= $charsetLength)
    {
        $i = $now % $charsetLength;
        $result = $charset[$i] . $result;
        $now /= $charsetLength;
    }
    return substr($result, -$length);
}

任何想法如何让它在现场工作?

根据 PHP 参考 http://php.net/manual/en/function.microtime.php 它说:

microtime() 以微秒为单位返回当前的 Unix 时间戳。这 功能仅在支持以下功能的操作系统上可用 gettimeofday() 系统调用。

我使用该函数生成唯一的新预订代码:

$newBookingCode = "R".$validNextMySQLInsertId."A".$this->getIncrementalHash(5);

谢谢!

【问题讨论】:

  • 你服务器上的 PHP 版本?
  • PHP 5.3.29 在服务器上。
  • 如果测试环境具有不同版本的 PHP,尤其是如果测试环境具有比 LIVE 环境更新的 PHP 版本,那么它并不是一个非常好的测试环境
  • PHP 5.3 已超过其end of life 一年多。如果可能的话,也许是时候升级测试环境和服务器了。

标签: php wordpress hash uniqueidentifier microtime


【解决方案1】:

数组解引用,即从返回数组的函数中获取结果,如:

$now = explode(' ', microtime())[1];

php5.4

开始可用

对于 php5.3 及更早版本的使用:

$now = explode(' ', microtime());
$now = $now[1];

【讨论】:

  • 为了完整起见,PHP5.3 代码将适用于所有较新版本的 PHP
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 2021-01-10
相关资源
最近更新 更多