站点动态:欢迎您!今天是 2024-04-29 星期一!

问题描述

PHP调试的时候出现了警告:It is not safe to rely on the system解决方法,其实就是时区设置不正确造成的,本文提供了3种方法来解决这个问题。

Warning: date(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Chongqing’ for ‘CST/8.0/no DST’ instead Warning: strtotime(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Chongqing’ for ‘CST/8.0/no DST’ instead Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Chongqing' for 'CST/8.0/no DST' instead in <b>/home/ftp/n/nimaboke/include/lib/function.base.php


问题分析

PHP函数password_hash

Password Hashing函数在 PHP 5.5 时被引入,这边主要讲讲password_hash加密函数、password_verify验证函数以及和md5加密的区别。


password_hash加密

password_hash() 使用足够强度的单向散列算法创建密码的散列(hash)。

password_hash(string $password, mixed $algo, array $options = ?): string|false

有3个参数:密码(必须),哈希算法(必须),选项(选填)

$pwd = '123456'; $hash = password_hash($pwd, PASSWORD_DEFAULT); echo $hash;

输出hash字符串:$2y$10$H7KD.QWJyY68zeKJXTHoW.zH9ZC69zlXALRBtN8BfiKQkk9YolQum

并不是唯一的hash字符串,通过刷新网页,可以输出不同的hash,也就是1个密码会出现不同的hash加密字符串。


password_verify验证

password_verify()验证密码是否和指定的散列值匹配。

password_verify(string $password, string $hash): bool

有2个参数:密码(必须),哈希值(必须)

$pwd = '123456'; $hash = '$2y$10$H7KD.QWJyY68zeKJXTHoW.zH9ZC69zlXALRBtN8BfiKQkk9YolQum'; $result = password_verify($pwd,$hash); var_dump($result); 输出bool(true)


MD5加密

使用《RSA 数据安全公司的 MD5 消息摘要算法》计算 string 的 MD5 散列值,并返回该散列值

md5(string $string, bool $binary = false): string

由于此函数依赖的算法已不足够复杂,不推荐使用此函数对明文密码加密。所以一般使用md5(password+salt)来计算md5值。

$pwd = '123456'; $md5 = md5($pwd); echo $md5;

输出md5字符串:e10adc3949ba59abbe56e057f20f883e

多次刷新网页保持这个字符串不变,由于网上直接加密的可以直接通过字典输出原始密码,导致非常不安全。

$pwd = '123456'; $salt = '77bx'; $md5 = md5($pwd.$salt); echo $md5; 输出md5字符串:caef045fe88f8efa2cf0b85cb91bad4d


加密性能测试

对比md5加密、md5+salt加密、password_hash加密和password_verify验证性能。

本次测试采用虚拟化

CPU:Intel Xeon Gold 6161 2.2GHz 2核4线程

内存:16GB

PHP:7.4

重复计算10次,取平均值得出以下截图结果

结论:

1、不建议使用password_hash和password_verify进行加密和验证,毕竟和md5+salt也有5万倍以上的性能差距。

2、建议使用md5+盐值的方式,毕竟1ms以下的时间对于性能来说影响不大。

3、安全性虽然password_hash比较安全,但是md5+salt的方式也是相对比较安全的。

PHP7下安装Emlog5.3.1

PHP7出来一段时间了,据说PHP7可以性能翻倍。而且我的服务器上也已经开通了PHP7,就开始折腾下Emlog5.3.1。


直接在php7安装emlog5.3.1各种报错。emlog5.3.1虽然已经出了使用mysqli连接类,但是为了兼容性还是默认是使用了mysql。因为PHP7已经不支持mysql扩展了,但是支持mysqli和pdo_mysql。所以这里还是介绍如何使用mysqli来安装emlog。


以下是修改emlog安装程序,无报错安装。如果是实际环境请在本地环境模拟后成功后再更换。

1、修改include\lib\option.php,大概11行修改为mysqli

冒泡排序算法是一个经典的常用排序算法,当然还有一些其他的常用排序算,比如选择排序,插入排序,希尔排序,快速排序。所以也是一种必学的算法,甚至很多公司面试的题目中也有冒泡排序的。

但是冒泡排序毕竟是一种效率低下的排序方法,在数据规模很小时,可以采用。数据规模比较大时,最好用其它排序方法。


冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面,直到达到升序排列。

比如:这个数组array(4,3,5,1,2)用冒泡排序算法进行升序排列。

第1个循环循环开始,当前排序:4,3,5,1,2

1、floor() 函数向下舍入为最接近的整数

<?php echo floor(5.1); //输出5 echo floor(-5.1); //输出-6 echo floor(5.9); //输出5 echo floor(-5.9); //输出-6 ?>


2、ceil() 函数向上舍入为最接近的整数

<?php echo ceil(5.1); //输出6 echo ceil(-5.1); //输出-5 echo ceil(5.9); //输出6 echo ceil(-5.9); //输出-5 ?>

PHP求1000以内的素数。

素数就是大于1的自然数中除了1和本身之外没有其他公因数的数。


方案1、判断素数的办法是用1到某个数之间的所有数去除这个数,如果能够整除这个数的数超过2个那么这个数就不是素数,反之如果能够整除这个数的数只有1个或者2个,那么他就是素数。