SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | session_start(); | |
4 | ||
5 | $pdo = new PDO("mysql:host=localhost;dbname=database", 'username', 'password'); | |
6 | ||
7 | if(isset($_POST['formreg'])) { | |
8 | $username = $_POST['user_input']; | |
9 | $password = $_POST['pass_input']; | |
10 | ||
11 | $query = $pdo->prepare("INSERT INTO users (user,pass) VALUES (:user_token,:pass_token)"); | |
12 | $values = array( | |
13 | "user_token" => $username, | |
14 | "pass_token" => sha1($password) | |
15 | ); | |
16 | $query->execute($values); | |
17 | } | |
18 | ||
19 | if(isset($_POST['formlog'])) { | |
20 | $username = $_POST['user_input']; | |
21 | $password = $_POST['pass_input']; | |
22 | ||
23 | $query = $pdo->prepare("SELECT * FROM users WHERE user=:user_token LIMIT 1"); | |
24 | $values = array( | |
25 | "user_token" => $username | |
26 | ); | |
27 | $query->execute($values); | |
28 | $row = $query->fetch(PDO::FETCH_ASSOC); | |
29 | if($row !== false) { | |
30 | if($row["pass"] === sha1($password)) { | |
31 | echo "Login success."; | |
32 | $_SESSION["username"] = $row["user"]; | |
33 | } else { | |
34 | echo "Wrong password."; | |
35 | } | |
36 | } else { | |
37 | echo "Username doesn't exist."; | |
38 | } | |
39 | } | |
40 | ||
41 | ?> | |
42 | ||
43 | <h1>Login</h1> | |
44 | <form action="index.php" method="post"> | |
45 | username: <input type="text" name="user_input"><br> | |
46 | password: <input type="password" name="pass_input"><br> | |
47 | <input type="hidden" name="formlog" value="1"> | |
48 | <input type="submit"> | |
49 | </form> | |
50 | ||
51 | <h1>Register</h1> | |
52 | <form action="index.php" method="post"> | |
53 | username: <input type="text" name="user_input"><br> | |
54 | password: <input type="password" name="pass_input"><br> | |
55 | <input type="hidden" name="formreg" value="1"> | |
56 | <input type="submit"> | |
57 | </form> |