Skip to content

Commit c73e70c

Browse files
committed
php add pizza login
1 parent ab4fe8e commit c73e70c

File tree

6 files changed

+165
-7
lines changed

6 files changed

+165
-7
lines changed

add.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
include('../tuto/config/db_connect.php');
3-
3+
session_start();
4+
if(!isset($_SESSION['username'])){
5+
header('Location:index.php');
6+
}
47
if(isset($_POST['submit'])) {
58
session_start();
69

config/db_connect.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@
44
echo 'connection errors' . mysqli_connect_error();
55
}else{
66

7-
} ?>
7+
}
8+
9+
$connB = mysqli_connect('localhost', 'admin', 'admin', 'login_register') or die('connection failed');
10+
11+
if(!$connB){
12+
echo 'connection errors' . mysqli_connect_error();
13+
}else{
14+
15+
}
16+
?>

login.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
include 'config/db_connect.php';
3+
session_start();
4+
if(isset($_POST['submit'])){
5+
$email = $_POST['email'];
6+
$password = md5($_POST['password']);
7+
8+
$result = mysqli_query($connB, "SELECT * FROM `users` WHERE email='$email' AND password='$password'");
9+
10+
11+
if($result->num_rows > 0){
12+
$row = mysqli_fetch_assoc($result);
13+
$_SESSION['username'] = $row['username'];
14+
header("Location: add.php");
15+
16+
}else{
17+
echo 'something wrong with password or email';
18+
}
19+
}
20+
?>
21+
<!DOCTYPE html>
22+
<html lang="en">
23+
24+
<?php include('templates/header.php') ?>
25+
<div class="container">
26+
<form action="" method="POST" class="login-email">
27+
<p class="login-text" style="font-size:2 rem; font-weight: 800;">
28+
login
29+
</p>
30+
<p><?php echo $success ?></p>
31+
<div class="input-group">
32+
<input required type="email" placeholder="email" value="<?php echo $email ?>" name="email" class="email">
33+
</div>
34+
<div class="input-group">
35+
<input required type="password" value="<?php echo $_POST['password']; ?>" name="password" placeholder="password" class="password">
36+
</div>
37+
<div class="input-group">
38+
<button class="btn" name="submit">login</button>
39+
</div>
40+
<p class="login-register-text">don't have a account? <a href="register.php">register</a></p>
41+
</form>
42+
</div>
43+
44+
<?php include('templates/footer.php') ?>
45+
46+
47+
</html>

logout.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
4+
session_start();
5+
session_destroy();
6+
7+
header("Location: index.php");
8+
?>

register.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
include 'config/db_connect.php';
3+
4+
5+
if(isset($_POST['submit'])){
6+
$username = $_POST['username'];
7+
$email = $_POST['email'];
8+
$password = md5($_POST['password']);
9+
$cpassword = md5($_POST['cpassword']);
10+
11+
if($password == $cpassword){
12+
$result = mysqli_query($connB, "SELECT * FROM `users` WHERE email='$email'");
13+
14+
15+
if($result->num_rows > 0){
16+
$already = "user already exist";
17+
18+
}else{
19+
$result = mysqli_query($connB, "INSERT INTO `users` (username, email, password) VALUES ('$username', '$email', '$password')");
20+
21+
22+
if($result){
23+
24+
$username = "";
25+
$email = "";
26+
$_POST['password'] = "";
27+
$_POST['cpassword'] = "";
28+
$success = "you are register";
29+
30+
header('location:login.php');
31+
32+
}else {
33+
echo "<script>alert('eror register')</script>";
34+
35+
}
36+
}
37+
38+
}else{
39+
echo "<script>alert('eror register')</script>";
40+
41+
}
42+
}
43+
?>
44+
45+
<!DOCTYPE html>
46+
<html lang="en">
47+
<?php include('templates/header.php') ?>
48+
49+
<div class="container">
50+
<form action="register.php" method="POST" class="login-email">
51+
<p class="login-text" style="font-size:2 rem; font-weight: 800;">
52+
register
53+
</p>
54+
<div class="input-group">
55+
<input required type="text" value="<?php echo $username ?>" placeholder="Username" name="username">
56+
</div>
57+
58+
<div class="input-group">
59+
<input required type="email" placeholder="Email" value="<?php echo $email ?>" name="email">
60+
</div>
61+
<div class="input-group">
62+
<input required type="password" value="<?php echo $password ?>"
63+
placeholder="password" name="password" class="password">
64+
</div>
65+
<div class="input-group">
66+
<input required type="password" value="<?php echo $cpassword ?>" placeholder="Confirm password" name="cpassword" class="password">
67+
</div>
68+
<p><?php echo $errorCpassword; ?></p>
69+
<p><?php echo $already; ?></p>
70+
71+
<div class="input-group">
72+
<button name="submit" class="btn">register</button>
73+
</div>
74+
<p class="login-register-text">have a account? <a href="index.php">register</a></p>
75+
</form>
76+
</div>
77+
<script src="script.js"></script>
78+
<?php include('templates/footer.php') ?>
79+
</html>

templates/header.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
session_start();
33
if($_SERVER['query_string'] == 'noname'){
4-
unset($_SESSION['name']);
4+
unset($_SESSION['username']);
55
}
6-
$name = $_SESSION['name'] ?? 'Guest';
7-
$gender = $_COOKIE['gender'] ?? 'unknow';
6+
$name = $_SESSION['username'] ?? 'Guest';
7+
88
?>
99
<head>
1010
<meta charset="UTF-8">
@@ -39,8 +39,20 @@
3939
<div class="container">
4040
<a href="index.php" class="brand-logo brand-text">Ninja pizza</a>
4141
<ul id="nav-mobile" class="right hide-on-small-and-down">
42-
<li class="grey-text">hello<?php echo htmlspecialchars($name)?>(<?php echo htmlspecialchars($gender)?>)</li>
43-
<li><a href="add.php" class="btn brand z-depth-0">add a pizza</a></li>
42+
<li class="grey-text">hello:<?php echo htmlspecialchars($name)?></li>
43+
44+
45+
46+
47+
<?php if( isset($_SESSION['username']) && !empty($_SESSION['username']) )
48+
{
49+
?>
50+
<li><a href="logout.php" class="btn brand z-depth-0">logout</a></li>
51+
<?php }else{ ?>
52+
<li><a href="login.php" class="btn brand z-depth-0">login</a></li>
53+
<li><a href="register.php" class="btn brand z-depth-0">register</a></li>
54+
<?php } ?>
55+
4456
</ul>
4557
</div>
4658
</nav>

0 commit comments

Comments
 (0)