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 )

示例:

$p = "123456";
//获取加密后的字符串
$hash = password_hash($p,PASSWORD_DEFAULT);

//验证密码
$input = "123456";
if(password_verify($input,$hash)){
    echo "密码正确";
}else{
    echo "密码错误";
}

http_build_query

将数组或对象生成 URL-encode 之后的请求字符串。

parse_url

解析 URL,返回其组成部分。

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的作用是设置比较到小数点第几位为止。

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

filter_var

使用特定的过滤器过滤一个变量,用于数据验证,详细

mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] )

str_repeat

 重复一个字符串

string str_repeat ( string $input , int $multiplier )

返回 input 重复 multiplier 次后的结果。

原文地址:https://www.cnblogs.com/praglody/p/6754443.html