-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.php
74 lines (65 loc) · 2.52 KB
/
signup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
session_start();
include_once "./adodb5/adodb.inc.php"; // include adodb essential
include_once "./adoConnection.php";
$username = $email = $password = $result = "";
$returnData = array();
$returnData[ "message" ] = "";
$returnData[ "code" ] = -1;
//-------------------------------------------------------
// get data from front-end
if ( isset( $_POST[ "username" ] ) && $_POST[ "username" ] != "" ) {
$username = $_POST[ "username" ];
}
if ( isset( $_POST[ "email" ] ) && $_POST[ "email" ] != "" ) {
$email = $_POST[ "email" ];
}
if ( isset( $_POST[ "password" ] ) && $_POST[ "password" ] != "" ) {
$password = $_POST[ "password" ];
}
// back-end check
if ( $username == "" || !preg_match( "/[a-z]/i" , $username ) ) {
$returnData[ "code" ] = 1;
$returnData[ "message" ] = "使用者名稱須包含至少一個英文字母";
}
if ( $email == "" || !preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i" , $email ) ) {
$returnData[ "code" ] = 1;
$returnData[ "message" ] = "電子郵件格式錯誤!!";
}
if ( $password == "" || !preg_match( "/[a-z0-9]/i" , $password ) ) {
$returnData[ "code" ] = 1;
$returnData[ "message" ] = "密碼格式錯誤!!";
}
else {
$password = password_hash( $password , PASSWORD_DEFAULT );
}
if ( $returnData[ "message" ] != "" ) {
echo json_encode( $returnData );
exit();
}
$sql = "insert into remind_user( name , password , email ) values( ? , ? , ? )";
$sql = $conn->prepare( $sql );
$result = $conn->execute( $sql , array( $username , $password , $email ) );
if ( $result == true ) {
$returnData[ "code" ] = 0;
$returnData[ "message" ] = "";
// get userid
$sql = "select id from remind_user where email = ?";
$sql = $conn->prepare( $sql );
$result = $conn->execute( $sql , array( $email ) );
if ( $result == false ) {
$returnData[ "code" ] = 1;
$returnData[ "message" ] = "查詢錯誤!! 請重試";
}
// register session
$_SESSION[ "username" ] = $username;
$_SESSION[ "id" ] = $result->fields[ "id" ];
$_SESSION[ "email" ] = $email;
}
else {
$returnData[ "code" ] = 1;
$returnData[ "message" ] = "查詢錯誤!! 請重試";
}
echo json_encode( $returnData );
$conn->close(); // close database connection ( optional choice
?>