验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
片段 1 片段 2
登陆注册功能
原始数据 复制代码
<?php
ob_start();
$timezone = date_default_timezone_get("Asia/Shanghai");
$conn = mysqli_connect("localhost","root","","red");
if (mysqli_connect_errno()) {
echo "数据库连接失败:".mysqli_connect_errno();
}
?>
Accout.php
原始数据 复制代码
<?php
class Accout{
private $errorArray;
private $con;
public function __construct($conn){
$this->con = $conn;
$this->errorArray = array();
}
public function login($username,$password){
$password = md5($password);
$sql = "SELRCT * FROM users WHERE username='$username' AND password='$password'";
$query = mysqli_query($this->con,$sql);
if (mysqli_num_rows($query) == 1) {
return true;
}else{
array_push($this->errorArray, Constants::$loginFailed);
return false;
}
}
public function register($username,$name,$email,$password,$password2){
$this->validateUsername($username);
$this->validateName($name);
$this->validateEmail($email);
$this->validatePassword($password,$password2);
if (empty($this->errorArray) == true) {
return $this->inserUserDetails($username,$name,$email,$password);
}else{
return false;
}
}
public function inserUserDetails($username,$name,$email,$password){
$password = md5($password);
$profilePic = "assets/images/profile-pics/head_emerald.png";
$date = date("Y-m-d");
$sql = "INSERT INTO users VALUES('','$username','$name','$email','$password','$date','profilePic')";
$result = mysqli_query($this->con,$sql);
return $result;
}
public function getError($error){
if (!in_array($error, $this->errorArray)) {
$error = "";
}
return "<span class='erroeMessage'>$error</span>";
}
private function validateUsername($un){
if (strlen($un) > 25 || strlen($un) < 5) {
array_push($this->errorArray, Constants::$usernameCharacters);
return;
}
$SQL = "SELECT username FROM users WHERE username='un'";
$chekUsername = mysqli_query($this->con,$sql);
if (mysqli_num_rows($chekUsername) != 0) {
array_push($this->errorArray, Constans::$usernameTaken);
return;
}
}
private function validateName($nm){
if (strlen($nm) > 25 || strlen($nm) < 2) {
array_push($this->errorArray, Constanes::$nameCharacters);
return;
}
}
private function validateEmail($em){
if (!filter_var($em,FILTER_VALIDATE_EMAIL)) {
array_push($this->errorArray, Constanes::$emailInvalid);
return;
}
$SQL = "SELECT username FROM users WHERE email='em'";
$chekEmail = mysqli_query($this->con,$sql);
if (mysqli_num_rows($chekEmail) != 0) {
array_push($this->errorArray, Constans::$emailTaken);
return;
}
}
private function validatePassword($pw,$pw2){
if ($pw != $pw2) {
array_push($this->errorArray, Constanes::$passwordNotMatch);
}
if (preg_match('/[^A-Za-z0-9]/', $pw)) {
array_push($this->errorArray, Constanes::$passwordWithNumAndLetter);
return;
}
if (strlen($pw) > 25 || strlen($pw) < 6) {
array_push($this->errorArray, Constanes::$passwordCharacters);
return;
}
}
}
?>

评论列表( 1 )

teamzishi 2018-12-07 02:37

哈哈哈哈,我找到问题了

你可以在登录后,发表评论

搜索帮助