\Session::put('test',182360000000);
\Session::save();
用来存储php storage/framework/sessions文件中
可以通过php下面方法获得session值
\Session::get();
a:6:{s:6:"_token";s:40:"q5HcZpFkIu7gjNr0ASeBcrQdSOCbGgnhuwJZrICj";s:22:"PHPDEBUGBAR_STACK_DATA";a:0:{}s:9:"_previous";a:1:{s:3:"url";s:30:"http://od.local:82/favicon.ico";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:4:"test";i:1111;s:9:"_sf2_meta";a:3:{s:1:"u";i:1478587984;s:1:"c";i:1478587165;s:1:"l";s:1:"0";}}
我们可以使用一个反序列化的dd函数来反向输出(顺便说说,php的编程时候的几种断点打印输出)
1、dd()函数反序列化
2、dump(\Session::all());die;
3、var_dump(\Session::all());
和echo '
';能够显得更加结构化
4、trace_log();
可以直接输入找到System.log文件。
接下来:说一些关于验证码、注册登录的一些内容
functions.php
function get_value_by_key($array, $key, $default = '')
{
if (is_array($array) && isset($array[$key])) {
return $array[$key];
}
return $default;
}
user.php
public function onValidateRegister()
{
$field = post('field', 'phone');
$validation = $this->validateRegister(post());
$msg = get_value_by_key($validation['result'], $field);
return array('status' => $validation['status'], 'msg' => $msg, 'is_registed' => $validation['result']['is_registed']);
}
public function validateRegister($post = [])
{
$phone = get_value_by_key($post, 'phone');
$verify_code = get_value_by_key($post, 'verify_code');
$sms_code = get_value_by_key($post, 'sms_code');
$sms_template_code = get_value_by_key($post, 'sms_template_code', 'register_message');
$password = get_value_by_key($post, 'password');
$re_password = get_value_by_key($post, 're_password');
$validation['status'] = 1;
if (!preg_match("/^1[34578]{1}\d{9}$/", $phone)) {
$validation['status'] = 0;
$validation['phone'] = '';
$validation['result']['is_registed'] = 0;
} else {
$user = User::findByPhone($phone);
if ($user) {
$validation['status'] = 0;
$validation['result']['phone'] = '该手机已注册';
$validation['result']['is_registed'] = 1;
} else {
$validation['result']['is_registed'] = 0;
}
}
//万能验证码
if ($verify_code != SessionManger::get('verify_code')) {
//if ($verify_code != SessionManger::get('verify_code')) {
$validation['status'] = 0;
$validation['result']['verify_code'] = '验证码输入有误 请重新输入';
}
//万能验证码
$sms = new Sms($sms_template_code);
if (!$sms->checkCode($phone, $sms_code)) {
//if (!$sms->checkCode($phone, $sms_code) && $sms_code !='123456') {
$validation['status'] = 0;
$validation['result']['sms_code'] = '短信验证码输入有误, 请重新输入';
}
if (!preg_match("/^[\x21-\x7E]{6,20}$/", $password) || $password != $re_password) {
//echo 123;die;
$validation['status'] = 0;
$validation['result']['password'] = '';
}
return $validation;
}
/**
* Register the user
*/
public function onRegister()
{
try {
if (!UserSettings::get('allow_registration', true)) {
throw new ApplicationException(Lang::get('zhijin.user::lang.account.registration_disabled'));
}
/*
* Validate input
*/
$data = post();
$validation = $this->validateRegister($data);
if (!$validation['status']) {
return $validation;
}
/*
* Register user
*/
$requireActivation = UserSettings::get('require_activation', true);
$automaticActivation = UserSettings::get('activate_mode') == UserSettings::ACTIVATE_AUTO;
$userActivation = UserSettings::get('activate_mode') == UserSettings::ACTIVATE_USER;
$user = Auth::register($data, $automaticActivation);
$pw_intensity = $this->checkPasswordIntensity($data['password']);
$this->saveProfile(['nickname' => str_random(mt_rand(12, 20)),
'pw_intensity' => $pw_intensity
]);
/*
* Activation is by the user, send the email
*/
// if ($userActivation) {
// $this->sendActivationEmail($user);
// Flash::success(Lang::get('zhijin.user::lang.account.activation_email_sent'));
// }
/*
* Automatically activated or not required, log the user in
*/
if ($automaticActivation || !$requireActivation) {
Auth::login($user);
}
/*
* Redirect to the intended page after successful sign in
*/
$redirectUrl = $this->pageUrl($this->property('redirect'))
? : $this->property('redirect');
if ($redirectUrl = post('redirect', $redirectUrl)) {
return Redirect::intended($redirectUrl);
}
} catch (Exception $ex) {
if (Request::ajax())
throw $ex;
else
Flash::error($ex->getMessage());
}
}
public function checkPasswordIntensity($str)
{
$score = 0;
if (preg_match("/[0-9]+/", $str)) {
$score ++;
}
if (preg_match("/[a-z]+/", $str)) {
$score ++;
}
if (preg_match("/[A-Z]+/", $str)) {
$score ++;
}
if (preg_match("/[!@#$%^&*()-+]+/", $str)) {
$score ++;
}
//长度小于8,弱
if ($score > 1 && strlen($str) < 8) {
$score = 1;
}
return $score;
}
/**
* Activate the user
* @param string $code Activation code
*/
public function onActivate($code = null)
{
try {
$code = post('code', $code);
/*
* Break up the code parts
*/
$parts = explode('!', $code);
if (count($parts) != 2) {
throw new ValidationException(['code' => Lang::get('zhijin.user::lang.account.invalid_activation_code')]);
}
list($userId, $code) = $parts;
if (!strlen(trim($userId)) || !($user = Auth::findUserById($userId))) {
throw new ApplicationException(Lang::get('zhijin.user::lang.account.invalid_user'));
}
if (!$user->attemptActivation($code)) {
throw new ValidationException(['code' => Lang::get('zhijin.user::lang.account.invalid_activation_code')]);
}
Flash::success(Lang::get('zhijin.user::lang.account.success_activation'));
/*
* Sign in the user
*/
Auth::login($user);
} catch (Exception $ex) {
if (Request::ajax())
throw $ex;
else
Flash::error($ex->getMessage());
}
}
/**
* Update the user
*/
public function onUpdate()
{
if (!$user = $this->user()) {
return;
}
$user->fill(post());
$user->save();
/*
* Password has changed, reauthenticate the user
*/
if (strlen(post('password'))) {
Auth::login($user->reload(), true);
}
Flash::success(post('flash', Lang::get('zhijin.user::lang.account.success_saved')));
/*
* Redirect
*/
if ($redirect = $this->makeRedirection()) {
return $redirect;
}
}
未完待续........