/
register.php
146 lines (106 loc) · 3.99 KB
/
register.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE HTML>
<html>
<head>
<title>Register</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="theme.css">
<script src="clock.js" type="text/javascript"></script>
</head>
<body>
<?php
include 'header.php';
$message = "Your username may contain letters and digits.\nYour password must be at least 4 characters long and and contain at least 1 digit and 1 letter.";
$userErr = $passErr = "";
$user = $pass = "";
$userTrue = $passTrue = false;
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["user"])) {
$userErr = "Please enter a username.";
}
else {
$user = $_POST["user"];
if(!preg_match("/^[a-zA-Z0-9 ]*$/",$user)) {
$userErr = "Your username can only contain letters and digits.";
} else {
$userTrue = true;
}
}
if(empty($_POST["pass"])) {
$passErr = "Please enter a password.";
}
else {
$pass = $_POST["pass"];
if (strlen($pass) < 4) {
$passErr = "Password must contain at least 4 characters";
}
elseif (!preg_match("#[0-9]+#", $pass)) {
$passErr = "Password must contain at least 1 number";
}
elseif (!preg_match("#[a-zA-Z]+#", $pass)) {
$passErr = "Passsword must contain at least one letter";
} else {
$passTrue = true;
}
}
}
if($userTrue && $passTrue) {
$info = "$user:$pass\n";
$handle = fopen('login.txt', 'r');
$temp = "";
$line = "";
while(!feof($handle)) {
$line = fgets($handle);
$arr = explode(":",$line);
$temp = $arr[0];
if(strcmp($temp,$user) == 0) {
$message = "That account already exists!";
$userTrue = false;
}
}
fclose($handle);
if($userTrue) {
$handle = fopen('login.txt', 'a+');
if(fwrite($handle, $info)) {
$message = "Account Created!";
}
fclose($handle);
}
}
?>
<div id="mainBody">
<div id="sideMenu">
<ul>
<a href="index.php"><li class="menuItem">Home Page</li></a>
<a href="findAPet.php"><li class="menuItem">Find a Pet</li></a>
<a href="dogCare.php"><li class="menuItem">Dog Care</li></a>
<a href="catCare.php"><li class="menuItem">Cat Care</li></a>
<a href="login.php"><li class="menuItem">Login</li></a>
<a href="contact.php"><li class="menuItem">Contact</li></a>
<a href="register.php"><li class="menuItem" style="background-color:#69C26A;">Create an Account</li></a>
</ul>
</div>
<div id="contentArea">
<h1 class="pageTitle">Register</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<fieldset>
<legend>Account Information</legend>
<label>Username:
<input type="text" name="user"></label>
<span class="red"> <?php echo $userErr ?> </span>
<br/><br/>
<label>Password:
<input type="password" name="pass"></label>
<span class="red"> <?php echo$passErr ?></span>
<br/><br/>
<input type="submit" value="Submit">
</fieldset>
</form>
<?php
echo $message;
?>
</div>
<div id="bottomLeft"></div>
</div>
<?php include 'footer.php' ?>
</body>
</html>