/** * 用户登录处理方法 * * @author NJ * @ctime 2016年1月23日17:24:17 * @return json 登录结果 * */ public function login_action() { // 下面的代码可以打印前台页面用户提交过来的数据 // var_dump($this->post('email'));exit; $email = trim($this->post('email')); $password = trim($this->post('pwd')); $this->load->model('common/User_m', 'user_m'); // 加载用户表模型 $reg_check_cond['email'] = $email; // 获取用户传递过来的邮箱 $mail_check_res = $this->user_m->repeat_check($reg_check_cond); // 调用用户表模型中的邮箱检重方法,用来检测该邮箱是否已注册 if (count($mail_check_res) === 0) { // 如果数量等于0的话,则说明未注册 $fin_res['status'] = 0; $fin_res['msg'] = '邮箱未注册'; echo json_encode($fin_res); exit; } else { $login_cond['email'] = $email; // $login_cond['password'] = pwd_encrypt($password)['password']; // 获取用户传递过来的邮箱 // $login_cond['salt'] = pwd_encrypt($password)['salt']; // 获取用户传递过来的邮箱 $user_data = $this->user_m->login($login_cond); // vde(pwd_encrypt($password, $user_data['password'], $user_data['salt'])); // var_dump($user_data);exit; if (pwd_encrypt($password, $user_data['password'], $user_data['salt'])) { // 登录成功 // 用户数据存入session $this->load->library('session'); $sess_arr['front_sess'] = $user_data; $this->session->set_userdata($sess_arr); // 返回登录的结果 $fin_res['status'] = 1; $fin_res['msg'] = '登录成功'; echo json_encode($fin_res); exit; } else { // 登录失败 $fin_res['status'] = 0; $fin_res['msg'] = '登录失败'; echo json_encode($fin_res); exit; } } }
<?php confirm_logged(); ?> <?php if (isset($_POST['submit'])) { // validations for input fields username and password $required_fields = array("username", "password"); validate_presences($required_fields); $fields_with_max_lengths = array("username" => 20); validate_max_lengths($fields_with_max_lengths); if (empty($errors)) { // Create users $username = mysql_prep($_POST["username"]); $hashed_password = pwd_encrypt($_POST["password"]); $query = "INSERT INTO admins ("; $query .= " username, password"; $query .= ") VALUES ("; $query .= " '{$username}', '{$hashed_password}'"; $query .= ")"; $result = mysqli_query($connection, $query); if ($result) { // ok $_SESSION["message"] = "Admin created."; redirect_to("manage_admins.php"); } else { // not ok $_SESSION["message"] = "Admin creation failed."; } }