<?php
session_start();
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to db
$db = mysqli_connect('localhost','root','','login_db') or die("could not connect to database");
// register users
$username = mysqli_real_escape_string($db , $_POST['username']);
$email = mysqli_real_escape_string($db , $_POST['email']);
$password_1 = mysqli_real_escape_string($db , $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db , $_POST['password_2']);
// form validation
if(empty($username)) {array_push($errors, "username is required");}
if(empty($email)) {array_push($errors, "Email is required");}
if(empty($password_1)) {array_push($errors, "password is required");}
if($password_1 != $password_2) {array_push($errors, "passwords do not match");}
// check db for existing with same username or email
$user_check_query = " SELECT * FROM userlogin WHERE username = '$username' OR email = '$email' LIMIT 1 ";
$results = mysqli_query($db , $user_check_query);
$user = mysqli_fetch_assoc($results);
if($user){
if($user['username'] === $username) {array_push($errors, "username already exists");}
if($user['email'] === $email) {array_push($errors, "email id is already registered");}
}
// register the user if no error
if(count($errors) == 0){
//$password = md5($password_1) // this will encrypt the password
$query = "INSERT INTO userlogin (username , password , email) VALUES ('$username' , '$password_1' , '$email')";
mysqli_query($db , $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "you are now logged in";
header(" location: home.php");
}
//login users
$username = mysqli_real_escape_string($db,$_POST['username']);
$password = mysqli_real_escape_string($db,$_POST['password_1']);
if(empty($username)){
array_push($errors , "username is required");
}
if(empty($password_1)){
array_push($errors, "password is required");
}
if(count($errors)==0){
$query = "SELECT * FROM userlogin WHERE username = '$username' AND password = '$password'";
$results = mysqli_query($db,$query);
if(mysqli_num_rows($results)){
$_SESSION['username'] = $username;
$_SESSION['success'] = "Logged in successfully";
header('location: home.php');
}
else{
array_push($errors, "Wrong username/password ");
}
}
?>
注意:未定义的索引:第17行的C:\ xampp \ htdocs \ demo \ config.php中的用户名
注意:未定义索引:第18行中C:\ xampp \ htdocs \ demo \ config.php中的电子邮件
注意:未定义的索引:第19行的C:\ xampp \ htdocs \ demo \ config.php中的password_1
注意:未定义的索引:第20行的C:\ xampp \ htdocs \ demo \ config.php中的password_2
注意:未定义的变量:导致第34行的C:\ xampp \ htdocs \ demo \ config.php
警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在第34行的C:\ xampp \ htdocs \ demo \ config.php中给出空值
注意:未定义的索引:第59行的C:\ xampp \ htdocs \ demo \ config.php中的用户名
注意:未定义的索引:第60行的C:\ xampp \ htdocs \ demo \ config.php中的password_1