POST /register.php HTTP/1.1 Content-Length: 43 Content-type: application/x-www-form-urlencoded email=xiao@hello.world&password=nihao
<?php
try {
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if (!$email) {
throw new Exception('Invalid email');
}
$password = filter_iput(INPUT_POST, 'password');
if (!$password || mb_strlen($password) < 8) {
throw new Exception('Password must contain 8+ characters');
}
//创建密码的哈希值
$passwordHash = password_hash(
$password,
PASSWORD_DEFAULT,
['cost' => 12]
);
if ($passwordHash === false) {
throw new Exception('Password hash failed');
}
//创建用户账户,这里是虚构的代码
$user = new User();
$user->email = $email;
$user->password_hash = $passwordHash;
$user->save();
header('HTTP/1.1 302 Redirect');
header('Location: /login.php');
} catch (Exception $e) {
header('HTTP1.1 400 Bad Request');
echo $e->getMessage();
}
POST /login.php HTTP1.1 Content-length: 43 Content-Type: application/x-www-form-urlencoded email=xiao@hello.wordl&pasword=nihao
session_start();
try {
$email = filter_input(INPUT_POST, 'email');
$password = filter_iinput(INPUT_POST, 'password');
$user = User::findByEmail($email);
if (password_verify($password, $user->password_hash) === false) {
throw new Exception(''Invalid password);
}
//如果需要的话,重新计算密码的哈希值
$currentHasAlgorithm = PASSWORD_DEFAULT;
$currentHashOptions = array('cost' => 15);
$passwordNeedsRehash = password_needs_rehash(
$user->password_hash,
$currentHasAlgorithm,
$currentHasOptions
);
if ($passwordNeedsRehash === true) {
$user->password_hash = password_hash(
$password,
$currentHasAlgorithm,
$currentHasOptions
);
$user->save();
}
$_SESSION['user_logged_in'] = 'yes';
$_SESSION['user_email'] = $email;
header('HTTP/1.1 302 Redirect');
header('Location: /user-profile.php');
} catch (Exception) {
header('HTTP/1.1 401 Unauthorized');
echo $e->getMessage();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有