server.php
<?php
session_start();// initializing variables
$user_name ="";
$user_email ="";
$errors = array();// connect to the database
$db_sys = mysqli_connect('localhost','root','','sys');// REGISTER USERif(isset($_POST['user_reg'])){// receive all input values from the form
$user_name = mysqli_real_escape_string($db_sys, $_POST['user_name']);
$user_email = mysqli_real_escape_string($db_sys, $_POST['user_email']);
$user_pass1 = mysqli_real_escape_string($db_sys, $_POST['user_pass1']);
$user_pass2 = mysqli_real_escape_string($db_sys, $_POST['user_pass2']);// form validation: ensure that the form is correctly filled ...// by adding (array_push()) corresponding error unto $errors arrayif(empty($user_name)){ array_push($errors,"Username is required");}if(empty($user_email)){ array_push($errors,"Email is required");}if(empty($user_pass1)){ array_push($errors,"Password is required");}if($user_pass1 != $user_pass2){
array_push($errors,"The two passwords do not match");}// check the database a user does not already exist with the same username and/or email
$user_check_query ="SELECT * FROM users WHERE user_name='$user_name' OR user_email='$user_email' LIMIT 1";
$result = mysqli_query($db_sysgo, $user_check_query);
$user = mysqli_fetch_assoc($result);if($user){// if user existsif($user['user_name']=== $user_name){
array_push($errors,"Username already exists");}if($user['user_email']=== $user_email){
array_push($errors,"email already exists");}}// Register user if there are no errorsif(count($errors)==0){//encrypt the password
$user_pass = md5($user_pass1);
$query ="INSERT INTO users (user_name, user_email, user_pass)
VALUES('$user_name', '$user_email', '$user_pass')";
mysqli_query($db_sys, $query);
$_SESSION['user_name']= $user_name;
$_SESSION['success']="You are now logged in";
header('location: index.php');}}// LOGIN USERif(isset($_POST['login'])){
$user_name = mysqli_real_escape_string($db_sys, $_POST['user_name']);
$user_pass = mysqli_real_escape_string($db_sys, $_POST['user_pass']);if(empty($user_name)){
array_push($errors,"Username is required");}if(empty($user_pass)){
array_push($errors,"Password is required");}if(count($errors)==0){
$user_pass = md5($user_pass);
$query ="SELECT * FROM users WHERE user_name='$user_name' AND user_pass='$user_pass'";
$results = mysqli_query($db_sys, $query);if(mysqli_num_rows($results)==1){
$_SESSION['user_name']= $user_name;
$_SESSION['success']="You are now logged in";
header('location: index.php');}else{
AQUI ESTÁ O ERRO QUE MOSTRA ---->>>> array_push($errors,"Wrong username/password");}}}?><?php if(count($errors)>0):?><div class="error"><?php foreach($errors as $error):?><p><?php echo $error ?></p><?php endforeach ?></div><?php endif ?>
Pergunta
INous
Scripts de registro e login de usuário:
- registro usuário.php
- server.php
- login.php
O de registro de usuário está ok. Faz o registro no bd (mysql).
Mas quando faço login, não recconhece user/email já registrado.
Linhas em local errado
Link para o comentário
Compartilhar em outros sites
2 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.