A non, c'est bon, j'ai réussi, j'ai bien compris...
Merci beaucoup pour ta réponse!
Mais un nouveau petit problème, je sais pas si sa vient de mon code ou de la table...
connexion.php
<?php
$con = mysqli_connect('localhost', 'root', '', 'userform');
?>
controllerUserData.php
<?php
session_start();
require "connection.php";
$email = "";
$name = "";
$errors = array();
//if user click signup button
if(isset($_POST['signup'])){
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
if($password !== $cpassword){
$errors['password'] = "Confirm password not matched!";
}
$email_check = "SELECT * FROM usertable WHERE email = '$email'";
$res = mysqli_query($con, $email_check);
if(mysqli_num_rows($res) > 0) {
$errors['email'] = "Email that you have entered is already exist!";
}
if(count($errors) === 0){
$encpass = password_hash($password, PASSWORD_BCRYPT);
$code = rand(999999, 111111);
$status = "notverified";
$insert_data = "INSERT INTO usertable (name, email, password, code, status)
values('$name', '$email', '$encpass', '$code', '$status')";
$data_check = mysqli_query($con, $insert_data);
if($data_check){
$subject = "Email Verification Code";
$message = "Your verification code is $code";
$sender = "From: pro.leotuto@gmail.com";
if(mail($email, $subject, $message, $sender)){
$info = "We've sent a verification code to your email - $email";
$_SESSION['info'] = $info;
header('location: user-otp.php');
exit();
}else{
$errors['otp-error'] = "Failed while sending code!";
}
}else{
$errors['db-error'] = "Failed while inserting data into database!";
}
}
}
//if user click verification code submit button
if(isset($_POST['check'])){
$_SESSION['info'] = "";
$otp_code = mysqli_real_escape_string($con, $_POST['otp']);
$check_code = "SELECT * FROM usertable WHERE code = $otp_code";
$code_res = mysqli_query($con, $check_code);
if(mysqli_num_rows($code_res) > 0){
$fetch_data = mysqli_fetch_assoc($code_res);
$fetch_code = $fetch_data['code'];
$email = $fetch_data['email'];
$code = 0;
$status = 'verified';
$update_otp = "UPDATE usertable SET code = $code, status = '$status' WHERE code = $fetch_code";
$update_res = mysqli_query($con, $update_otp);
if($update_res){
$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
header('location: home.php');
exit();
}else{
$errors['otp-error'] = "Failed while updating code!";
}
}else{
$errors['otp-error'] = "You've entered incorrect code!";
}
}
//if user click login button
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$check_email = "SELECT * FROM usertable WHERE email = '$email'";
$res = mysqli_query($con, $check_email);
if(mysqli_num_rows($res) > 0)
{
$fetch = mysqli_fetch_assoc($res);
$fetch_pass = $fetch['password'];
if(password_verify($password, $fetch_pass)){
$_SESSION['email'] = $email;
$status = $fetch['status'];
if($status == 'verified'){
$_SESSION['email'] = $email;
header('location: home.php');
}else{
$info = "It's look like you haven't still verify your email - $email";
$_SESSION['info'] = $info;
header('location: user-otp.php');
}
}else{
$errors['email'] = "Incorrect email or password!";
}
}else{
$errors['email'] = "It's look like you're not a yet member! Click on the bottom link to signup.";
}
}
?>
home.php
<?php require_once "controllerUserData.php"; ?>
<?php
$email = $_SESSION['email'];
if($email == false){
header('Location: login-user.php');
}
$fetch_data = "SELECT * FROM usertable WHERE email = '$email'";
$run_query = mysqli_query($con, $fetch_data);
$fetch_info = mysqli_fetch_assoc($run_query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $fetch_info['name'] ?> | Home</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
@import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap');
nav{
padding-left: 100px!important;
padding-right: 100px!important;
background: #6665ee;
font-family: 'Poppins', sans-serif;
}
nav a.navbar-brand{
color: #fff;
font-size: 30px!important;
font-weight: 500;
}
button a{
color: #6665ee;
font-weight: 500;
}
button a:hover{
text-decoration: none;
}
h1{
position: absolute;
top: 50%;
left: 50%;
width: 100%;
text-align: center;
transform: translate(-50%, -50%);
font-size: 50px;
font-weight: 600;
}
<body style="background-color: blue;">
</style>
</head>
<body>
<nav class="navbar">
<a class="navbar-brand" href="#">Brand name</a>
<button type="button" class="btn btn-light"><a href="logout-user.php">Logout</a></button>
</nav>
<h1>
Welcome <?php echo $fetch_info['name'] ?></h1>
</body>
</html>
login-user.php
<?php require_once "controllerUserData.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Form</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 offset-md-4 form login-form">
<form action="login-user.php" method="POST" autocomplete="">
<h2 class="text-center">
Login Form</h2>
<p class="text-center">
Login with your email and password.</p>
<?php
if(count($errors) > 0){
?>
<div class="alert alert-danger text-center">
<?php
foreach($errors as $showerror){
echo $showerror;
}
?>
</div>
<?php
}
?>
<div class="form-group">
<input class="form-control" type="email" name="email" placeholder="Email Address" required value="<?php echo $email ?>">
</div>
<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password" required>
</div>
<div class="form-group">
<input class="form-control button" type="submit" name="login" value="Login">
</div>
<div class="link login-link text-center">
Not yet a member? <a href="signup-user.php">Signup now</a></div>
</form>
</div>
</div>
</div>
</body>
</html>
logout-user.php
<?php
session_start();
session_unset();
session_destroy();
header('location: login-user.php');
?>
user-otp.php
<?php require_once "controllerUserData.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code Verification</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 offset-md-4 form">
<form action="user-otp.php" method="POST" autocomplete="off">
<h2 class="text-center">
Code Verification</h2>
<?php
if(isset($_SESSION['info'])){
?>
<div class="alert alert-success text-center">
<?php echo $_SESSION['info']; ?>
</div>
<?php
}
?>
<?php
if(count($errors) > 0){
?>
<div class="alert alert-danger text-center">
<?php
foreach($errors as $showerror){
echo $showerror;
}
?>
</div>
<?php
}
?>
<div class="form-group">
<input class="form-control" type="number" name="otp" placeholder="Enter verification code" required>
</div>
<div class="form-group">
<input class="form-control button" type="submit" name="check" value="Submit">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
signup-user.php
<?php require_once "controllerUserData.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Signup Form</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 offset-md-4 form">
<form action="signup-user.php" method="POST" autocomplete="">
<h2 class="text-center">
Signup Form</h2>
<p class="text-center">
It's quick and easy.</p>
<?php
if(count($errors) == 1){
?>
<div class="alert alert-danger text-center">
<?php
foreach($errors as $showerror){
echo $showerror;
}
?>
</div>
<?php
}elseif(count($errors) > 1){
?>
<div class="alert alert-danger">
<?php
foreach($errors as $showerror){
?>
<li><?php echo $showerror; ?></li>
<?php
}
?>
</div>
<?php
}
?>
<div class="form-group">
<input class="form-control" type="text" name="name" placeholder="Full Name" required value="<?php echo $name ?>">
</div>
<div class="form-group">
<input class="form-control" type="email" name="email" placeholder="Email Address" required value="<?php echo $email ?>">
</div>
<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password" required>
</div>
<div class="form-group">
<input class="form-control" type="password" name="cpassword" placeholder="Confirm password" required>
</div>
<div class="form-group">
<input class="form-control button" type="submit" name="signup" value="Signup">
</div>
<div class="link login-link text-center">
Already a member? <a href="login-user.php">Login here</a></div>
</form>
</div>
</div>
</div>
</body>
</html>
Merci!
13 sept. 2020 à 17:58