-
Notifications
You must be signed in to change notification settings - Fork 0
/
newuser.php
95 lines (77 loc) · 2.8 KB
/
newuser.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
<?php
error_reporting(E_ERROR | E_PARSE);
if(
isset($_POST['username']) and
isset($_POST['password']) and
isset($_POST['name'])
){
require("inc/dbinfo.inc");
try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Test for existing user
$stmt_finduser = $conn->prepare("SELECT id FROM users WHERE username=?");
$stmt_finduser->execute(array($_POST['username']));
if($stmt_finduser->rowCount() >= 1){
$code = "userexists";
$urlsuffix = "?".join("&",array(
"msg=".$code,
"username=".$_POST['username'],
"name=".$_POST['name']
));
header("Location: ".basename(__FILE__).$urlsuffix);
exit;
}
// Create new user
$stmt_makeuser = $conn->prepare("
INSERT INTO users(username, password, name)
VALUES( ?, ?, ? )
");
$stmt_makeuser->execute(array(
$_POST['username'],
hash("sha256", $_POST['password']),
$_POST['name']
));
}catch(PDOException $e){
echo "Error: " . $e->getMessage();
exit;
}
header("Location: login.php?msg=newuser");
esit;
}else{
?>
<!DOCTYPE html>
<html>
<head>
<title>Log-in</title>
<link type="text/css" rel="stylesheet" href="css/reset.css"/>
<link type="text/css" rel="stylesheet" href="css/main.css"/>
<script src="js/jquery-2.0.3.js"></script>
<script src="js/newuser.js"></script>
</head>
<body>
<?php require("header.php");
if(isset($_GET['msg'])){
switch($_GET['msg']){
case "userexists":
?><span class="form-msg">Username already exists!</span><br><?php
break;
}
}
?>
<div id="title">Your info:</div>
<form method="post" id="newuser-form" action="<?php echo basename(__FILE__); ?>">
Username: <input type="text" id="username" name="username"
<?php echo isset($_GET['username']) ? 'value='.$_GET['username'] : ""; ?> /><br>
Password: <input type="password" id="password" name="password" autocomplete="off" /><br>
Re-enter Password: <input type="password" id="password-clone" name="password" autocomplete="off" /><br>
Name: <input type="text" id="name" name="name" autocomplete="off"
<?php echo isset($_GET['name']) ? 'value='.$_GET['name']: ""; ?> /><br>
<input type="submit" value="Submit"/>
</form>
<?php require("footer.php"); ?>
</body>
</html>
<?php
}
?>