function generatePostalCode($selected) { if (strcmp($selected, "United Kingdom") == 0) { // this isn't exactly correct as there are rules about which letters can be used in which format, but structurally // these cover it -- well, according to wikipedia at least http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Format $variant = rand(0, 5); switch ($variant) { case 0: // A9 9AA $postalCode = randomCharacter() . randomNumber() . ' ' . randomNumber() . randomCharacter(2); break; case 1: // A99 9AA $postalCode = randomCharacter() . randomNumber(2) . ' ' . randomNumber() . randomCharacter(2); break; case 2: // AA9 9AA $postalCode = randomCharacter(2) . randomNumber() . ' ' . randomNumber() . randomCharacter(2); break; case 3: // AA99 9AA $postalCode = randomCharacter(2) . randomNumber(2) . ' ' . randomNumber() . randomCharacter(2); break; case 4: // A9A 9AA $postalCode = randomCharacter() . randomNumber() . randomCharacter() . ' ' . randomNumber() . randomCharacter(2); break; case 5: // AA9A 9AA $postalCode = randomCharacter(2) . randomNumber() . randomCharacter() . ' ' . randomNumber() . randomCharacter(2); break; } } elseif (strcmp($selected, "United States") == 0) { // zip5 and zip9 $variant = rand(0, 1); // only does zip 5 $variant = 0; switch ($variant) { case 0: // 12345 $postalCode = randomNumber(5); break; case 1: // 12345-1234 $postalCode = randomNumber(5) . '-' . randomNumber(4); break; } } elseif (strcmp($selected, "Canada") == 0) { // A1A 1A1 $postalCode = randomCharacter() . randomNumber() . randomCharacter() . ' ' . randomNumber() . randomCharacter() . randomNumber(); } return $postalCode; }
/** * * 注册新用户, 要保证用户是要有银联帐号的 * * @param string username * @param string password * * @return * status: 1: 成功 * data: objectId, sessionToken * * status: 202 * msg: username is taken */ public function user_post() { $username = $this->post('username'); $password = $this->post('password'); $this->load->model('user2_m', 'user'); if (empty($username) || empty($password)) { return $this->output_error(ErrorEmptyUsernamePwd); } $count = $this->user->count_by('username', $username); if ($count > 0) { return $this->output_error(ErrorUsernameExists); } $data['username'] = $username; $data['password'] = $password; $data['sessionToken'] = randomCharacter(20); $data['expireDate'] = date('Y-m-d H:i:s', strtotime('+2 week')); // session有效期2周 $data['nickname'] = 'KQ_' . randomCharacter(8); $data['createdAt'] = NULL; $id = $this->user->insert($data); if (empty($id)) { // 如果没有注册成功,报出错 return $this->output_error(ErrorDBInsert); } $this->db->select('id,username,nickname,avatarUrl,sessionToken'); $user = $this->user->get($id); // 发送站内信 unset($data); $data['uid'] = $id; $data['title'] = '注册成功'; $data['text'] = '您已成功注册快券,多来这里看看哦,关注快券多一秒,更多优惠带给您'; $this->load->model('news2_m', 'news'); $newsId = $this->news->insert($data); if (empty($newsId)) { // 如果没有insert成功 log_message('error', 'Register insert news error, uid #' . $id); } return $this->output_results($user); }
/** * * 注册新用户, 要保证用户是要有银联帐号的 * * @param string username * @param string password * * @return * status: 1: 成功 * data: objectId, sessionToken * * status: 202 * msg: username is taken */ public function user_post() { $username = $this->post('username'); $password = $this->post('password'); $this->load->model('user2_m', 'user'); if (empty($username) || empty($password)) { return $this->output_error(ErrorEmptyUsernamePwd); } $count = $this->user->count_by('username', $username); if ($count > 0) { return $this->output_error(ErrorUsernameExists); } $data['username'] = $username; $data['password'] = $password; $data['sessionToken'] = randomCharacter(20); $data['expireDate'] = date('Y-m-d H:i:s', strtotime('+2 week')); // session有效期2周 $data['nickname'] = 'KQ_' . randomCharacter(8); $data['createdAt'] = NULL; $id = $this->user->insert($data); $this->db->select('id,username,nickname,avatarUrl,sessionToken'); $user = $this->user->get($id); //----- 银联注册 return $this->output_results($user); }
function get_rand_letters($length) { if ($length > 0) { $rand_id = ""; for ($i = 1; $i <= $length; $i++) { mt_srand((double) microtime() * 1000000); $num = mt_rand(1, 26); $rand_id .= randomCharacter($num); } } return $rand_id; }