Salve. Sto provando a creare un nuovo progetto e ho difficoltà sul signup/signin.
in elenco prima il codice html e poi i 2 codici php.
<div class="login__forms">
<form name="LogForm" action="http://localhost/ecommerce/assets/login.php" class="login__registre" method="post" id="login-in">
<h1 class="login__title">Sign In</h1>
<div class="login__box">
<i class='bx bx-user login__icon'></i>
<input type="text" name="usex" placeholder="Username" class="login__input" required>
</div>
<div class="login__box">
<i class='bx bx-lock-alt login__icon'></i>
<input type="password" name="pwx" placeholder="Password" class="login__input" required>
</div>
<a href="#" class="login__forgot">Forgot password?</a>
<input type="submit" value="Login" name="Submit" id="login__test" />
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<p>Login soon..</p>
</div>
</div>
<div>
<span class="login__account">Don't have an Account ?</span>
<span class="login__signin" id="sign-up">Sign Up</span>
</div>
</form>
<!-- REGIST FORM -->
<form name="RegForm" method="post" action="http://localhost/ecommerce/assets/registrer.php" class="login__create none" id="login-up">
<h1 class="login__title">Create Account</h1>
<div class="login__box">
<i class='bx bx-user login__icon'></i>
<input type="text" name="regfirstname" placeholder="First name" class="login__input" required>
</div>
<div class="login__box">
<i class='bx bx-user login__icon'></i>
<input type="text" name="reglastname" placeholder="Last name" class="login__input" required>
</div>
<div class="login__box">
<i class='bx bx-user login__icon'></i>
<input type="text" name="regnuserame" placeholder="Username" class="login__input"required >
</div>
<div class="login__box">
<i class='bx bx-at login__icon'></i>
<input type="text" name="regmail" placeholder="Email" class="login__input" required>
</div>
<div class="login__box">
<i class='bx bx-lock-alt login__icon'></i>
<input type="password" name="regpass" placeholder="Password" class="login__input" required>
</div>
<div class="login__box">
<i class='bx bx-lock-alt login__icon'></i>
<input type="password" name="confpass" placeholder="Confirm password" class="login__input" required>
</div>
<input type="submit" value="Sign Up" name="signup" id="login__test" >
<div>
<span class="login__account">Already have an Account ?</span>
<span class="login__signup" id="sign-in">Sign In</span>
</div>
questo è il codice con i 2 form, con javascript si possono alternare e la funzione è ok.
ora i 2 codici php con cui ho difficoltà.
SIGN IN
<?php
session_start();
require_once( 'db.php');
if (empty($_POST)) {
die();
}
$errors = '';
$username = $_POST['usex'] ?? '';
$password = $_POST['pwx'] ?? '';
if (empty($password) || strlen($password < 4)) {
$errors .= 'Field password is required and length >= 4';
}
$sql = 'SELECT username, password from users where username=?';
$stm = $mysqli->prepare($sql);
if (!$stm) {
die($mysqli->error);
}
$stm->bind_param('s', $username);
$res = $stm->execute();
$result = $stm->get_result();
if ($res && $result->num_rows) {
$row = $result->fetch_assoc();
if ( !$row || password_verify($password, $row['password']) === false) {
$errors .= 'Wrong username or password';
$stm->close();
header("location: http://localhost/ecommerce/assets/index.html");
} else {
session_regenerate_id();
$_SESSION['session_id'] = session_id();
$_SESSION['session_user'] = $user['username'];
header("location: http://localhost/ecommerce/assets/ecomm.html");
}
}
?>
e SIGN UP
<?php
session_start();
require ('db.php');
if (empty($_POST)) {
die();
}
$msg = '';
$name = $_POST['regfirstname'] ?? '';
$lastname = $_POST['reglastname'] ?? '';
$username = $_POST['regusername'] ?? '';
$email = $_POST['regmail'] ?? '';
$password = $_POST['regpass'] ?? '';
$passtrue = $_POST['confpass'] ?? '';
$isUsernameValid = filter_var(
$username,
FILTER_VALIDATE_REGEXP, [
"options" => [
"regexp" => "/^[a-z\d_]{3,20}$/i"
]
]
);
$pwdLenght = mb_strlen($password);
if (empty($username) || empty($passtrue) || empty($email) || empty($lastname) || empty($password)) {
$msg .= 'Compila tutti i campi %s';
} elseif (false === $isUsernameValid) {
$msg .= 'Lo username non è valido. Sono ammessi solamente caratteri
alfanumerici e l\'underscore. Lunghezza minina 3 caratteri.
Lunghezza massima 20 caratteri';
} elseif ( $password != $passtrue ) {
$msg .= 'Le 2 password non corrispondono';
} elseif ($pwdLenght < 4 || $pwdLenght > 20) {
$msg .= 'Lunghezza minima password 4caratteri.
Lunghezza massima 20 caratteri';
} else {
$query = " SELECT username, email FROM users WHERE (username=?) AND (email=?) ";
$check = $mysqli->prepare($query);
if (!$check){
die($mysqli->error);
}
$check->bind_Param('ss', $username, $email, PDO::PARAM_STR);
$result = $check->execute();
$rescheck = $check->get_result();
if ($result && $rescheck ->num_rows){
$msg .= 'Email or Username already taken';
}
$check->close();
$query1 = " INSERT INTO users ( username, password, email, nome, cognome) VALUES ( ?, ?, ?, ?, ? )";
$check = $mysqli->prepare($query);
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$resBind = $check->bind_Param('sssss', $username, $password_hash, $email, $name, $lastname);
$result= $check->execute();
if ($result && $check->affected_rows) {
$_SESSION['msg'] = 'User registered correctly';
die();
} else {
$_SESSION['msg'] = $mysqli->error;
die();
}
}
?>
le variabili vengono prese tranquillamente dal $_post ho fatto una verifica con echo/die. Sono bloccato alla seconda parte del login ( quello in cui confronta password) e nell'insert del sign up.
Non riesco a capire l'errore, ho utilizzato diverse guide e le ho confrontate ma nulla. Sono inesperto pertanto vorrei un aiuto grazie