php 函数小技巧(一)

密码加密与验证

password_hash — 创建密码的哈希(hash)

string password_hash ( string $password , integer $algo [, array $options ] )
当前支持的算法($algo的参数):
1. PASSWORD_DEFAULT - 使用 bcrypt 算法 (PHP 5.5.0 默认)。 注意,该常量会随着 PHP 加入更新更高强度的算法而改变。 所以,使用此常量生成结果的长度将在未来有变化。 因此,数据库里储存结果的列可超过60个字符(最好是255个字符)。
2. PASSWORD_BCRYPT - 使用 CRYPT_BLOWFISH 算法创建哈希。 这会产生兼容使用 "$2y$" 的 crypt()。 结果将会是 60 个字符的字符串, 或者在失败时返回 FALSE。

password_verify — 验证密码是否和哈希匹配

boolean password_verify ( string $password , string $hash )

示例:

$password = "123456";
//获取加密后的字符串
$hash = password_hash($password,PASSWORD_DEFAULT);
//验证密码
$input = "123456";
if(password_verify($input,$hash))
    echo "密码正确";
else
    echo "密码错误";
get_headers

取得服务器响应一个 HTTP 请求所发送的所有标头。

array get_headers ( string $url [, int $format = 0 ] )

参数:

url - 目标 URL。

format - 如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。

bccomp

比较两个任意精度的数字,用于高精度浮点数比较。

int bccomp ( string $left_operand , string $right_operand [, int $scale = int ] )

参数:$scale的作用是设置比较到小数点第几位为止。

返回值:
如果两个数左边的数left_operand比较右边的数right_operand相等返回0,
左边的数left_operand比较右边的数right_operand大返回1,
左边的数left_operand比较右边的数right_operand小返回-1.

原文地址:https://www.cnblogs.com/phpper/p/6756406.html