-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·137 lines (123 loc) · 4.08 KB
/
index.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
<?php
include("database.php");
// $username = "";
// $password = "";
// $hash = password_hash($password, PASSWORD_DEFAULT);
// $sql = "insert into users (user, password) values ('felicity', '99')";
// $insert_sql = "insert into users (user, password) values ('$username', '$hash')";
// $get_sql = "select * from users";
// start of 'how to insert and query php database' example
// if($_SERVER["REQUEST_METHOD"] == "POST")
// {
// if(isset($_POST['insert']))
// {
// try
// {
// mysqli_query($conn, $insert_sql);
// echo "user is now registered.";
// }
// catch(mysqli_sql_exception $e)
// {
// echo "Could not register user because:<br>" . $e->getMessage();
// }
// }
// else if(isset($_POST['get']))
// {
// try
// {
// $result = mysqli_query($conn, $get_sql);
// if(mysqli_num_rows($result) > 0)
// {
// while($row = mysqli_fetch_assoc($result))
// {
// echo $row["id"] . "<br>";
// echo $row["user"] . "<br>";
// echo $row["password"] . "<br>";
// echo $row["register_date"] . "<br>";
// }
// }
// else
// {
// echo "No user found";
// }
// }
// catch(mysqli_sql_exception $e)
// {
// echo $output . $e->getMessage();
// }
// }
// }
// end of 'how to insert and query php database' example
// start of 'registration form' example
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_SPECIAL_CHARS);
$password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_SPECIAL_CHARS);
if(empty($username) && empty($password))
{
echo "please enter a username and password";
}
else if(empty($username))
{
echo "please enter a username";
}
else if(empty($password))
{
echo "please enter a password";
}
else
{
try
{
$hash = password_hash($password, PASSWORD_DEFAULT);
$sql = "insert into users (user, password) values ('$username', '$hash')";
mysqli_query($conn, $sql);
echo "you are now registered";
}
catch(mysqli_sql_exception $e)
{
echo "Could not register user because:<br>" . $e->getMessage();
}
}
}
mysqli_close($conn);
?>
<!-- // start of html for 'how to insert and query php database' example -->
<!-- <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="index.php" method="post">
<input type="submit" name="insert" value="insert">
<input type="submit" name="get" value="get">
</form>
</body>
</html> -->
<!-- // end of html for 'how to insert and query php database' example -->
<!-- // start of html for 'registration form' example -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<fieldset>
<legend>
<h2>Welcome to Fakebook!</h2>
</legend>
<form action="<?php htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method ="post">
<label for="">username:</label><br>
<input type="text" name="username"><br><br>
<label for="">password:</label><br>
<input type="password" name="password"><br><br>
<input type="submit" name="register" value="register">
</form>
</fieldset>
</body>
</html>