我是php编码新手
我创建了包含用户名和密码的登录页面,登录网页显示,当我输入用户和密码时出现错误,我发现很多方法,但我找不到任何解决方案,它&#39 ; s不工作,
这是我错误的地方
这是登录代码
<?php include('server.php');?>)
<!DOCTYPE html>
<html>
<head>
<title> user registration system using php</title>
<link rel ="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class ="header">
<h2> login</h2>
</div>
<form method="post" action="login.php">
<?php include('errors.php'); ?>
<div class ="input-group">
<label>Username</label>
<input type = "text" name = "Username">
</div>
<div class ="input-group">
<label>password</label>
<input type = "text" name = "password">
</div>
<div class ="input-group">
<button type ="submit" name ="register" class="btn">login</button>
</div>
<p>
not yet a member? <a href="register.php">sign up</a>
</p>
</form>
</body>
</html>
servercode:
<?php
session_start();
$username = "";
$email = "";
$errors = array();
$db = mysqli_connect('localhost','root','','registration');
//print_r(!empty($_POST['registration']));exit;
if (empty($_POST['registration'])){
$username = ($_POST['username']);
$email = ($_POST['email']);
$password = ($_POST['password']);
//echo 'Hi';
if (empty($username)) {
array_push($errors, "username is required");
}
if (empty($email)) {
array_push($errors, "email is required");
}
if (empty($password)) {
array_push($errors, "password is required");
}
if (count($errors) == 0) {
$sql = "INSERT INTO users (username,email,password) VALUES('$username','$email','$password')";
mysqli_query($db,$sql);
$_SESSION['username'] = $username;
$_SESSION['success'] = "you are now logged in";
header('location: index.php');
}
}
if(isset($_POST['login'])) {
$username = ($_POST['username']);
$password = ($_POST['password']);
//echo 'Hi';
if (empty($username)) {
array_push($errors, "username is required");
}
if (empty($password)) {
array_push($errors, "password is required");
}
if (count($errors)==0) {
$query = "SELECT * FROM users where username = '$username' AND password ='$password'";
$result = mysqli_query($db,$query);
if(mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "you are now logged in";
header('location: index.php');
}else{
array_push($errors, "wrong username/password combination");
header('location: login.php');
}
}
}
if(isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header('location: login.php');
}
?>
答案 0 :(得分:0)
签入Html表单,您将名称命名为Username,在服务器端,您使用username获取值。有帽子和小写字母的问题。 以html格式删除用户名中的U的大写字母并替换为u。