-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
67 lines (58 loc) · 1.89 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
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $password = "";
$username_err = $password_err = "";
$isLoggedIn = $flag=false,;
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(!empty(trim($_POST["email"]))){
$username = trim($_POST["email"]);
}
if(!empty(trim($_POST["password"]))){
$password = trim($_POST["password"]);
}
if(empty($username_err) && empty($password_err)){
$sql = "SELECT id, username, password FROM users WHERE username = $username , password =$password";
if($conn->query($sql)!=null){
$flag=true;
$isLoggedIn =true;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<div><?php if($flag && $isLoggedIn) echo 'login success' else 'login failed'?></div>
<h2>Login</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" onSubmit="validate()">
<label>Email</label>
<input type="text" name="email" value="<?php echo $username; ?>">
<span ><?php echo $username_err; ?></span>
<label>Password</label>
<input type="password" name="password">
<span ><?php echo $password_err; ?></span>
<input type="submit" value="Login">
</form>
<script>
function validate(){
var email = document.getElementsByName("email")[0].value;
var pass = document.getElementsByName("password")[0].value;
if(email.length == 0 || !email.includes("@")){
alert('Invalid email');
}
else if( pass.length == 0){
alert('Password cannot be empty');
}
}
</script>
</body>
</html>