forked from breeah-wm/Shopping-Cart-Project
/
trial.php
84 lines (74 loc) · 2.19 KB
/
trial.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
<?php
require_once('connect.php');
function login($conn) {
setcookie('token', "", 0, "/");
$username = $_POST['username'];
$password = sha1($_POST['password']);
$sql = 'SELECT * FROM users WHERE username = ? AND password = ?';
$stmt = $conn->prepare($sql);
if ($stmt->execute(array($username, $password))) {
$valid = false;
while ($row = $stmt->fetch()) {
$valid = true;
$token = generateToken();
$sql = 'UPDATE users SET token = ? WHERE username = ?';
$stmt1 = $conn->prepare($sql);
if ($stmt1->execute(array($token, $username))) {
setcookie('token', $token, 0, "/");
echo 'Login Successful';
}
}
if(!$valid) {
echo 'Username or Password Incorrect';
}
}
}
function register($conn) {
$username = $_POST['username'];
$password = sha1($_POST['password']);
$email = $_POST['email'];
$token = generateToken();
$sql = 'INSERT INTO users (username, password, email, token) VALUES (?, ?, ?, ?)';
$stmt = $conn->prepare($sql);
try {
if ($stmt->execute(array($username, $password, $email, $token))) {
setcookie('token', $token, 0, "/");
echo 'Account Registered';
}
}
catch (PDOException $e) {
echo $e->getMessage();
//echo 'Username or Email Already Registered';
}
}
function generateToken() {
$date = date(DATE_RFC2822);
$rand = rand();
return sha1($date.$rand);
}
if(isset($_POST['login'])) {
login($dbh);
}
if(isset($_POST['register'])) {
register($dbh);
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="text" name="username" placeholder="Username"/>
<input type="password" name="password" placeholder="Password"/>
<input type="submit" name="login" value="LOGIN"/>
</form>
<br>
<form method="post" action="">
<input type="text" name="username" placeholder="Username"/>
<input type="password" name="password" placeholder="Password"/>
<input type="text" name="email" placeholder="Email"/>
<input type="submit" name="register" value="REGISTER"/>
</form>
</body>
</html>