我使用 mySQL 和 php 创建了这个登录/注册/注销页面,带有索引页面和主页。
在signup.php上,您的名字、用户名、电子邮件和密码存储在链接数据库中,该数据库工作正常。然后,在登录页面(login.php)上,理论上,您应该能够使用用户名|电子邮件和密码登录您的帐户,然后将您带到主页(welcome.php)。但是,当您输入登录详细信息时,代码所做的只是在页面左上角输出您的密码,然后您将停留在 login.php 上。
登录页面确实设法验证您输入的用户名|电子邮件和密码是否正确,如果不正确则给出错误,但是一旦完成,代码不会将用户发送到家(欢迎。 php)我想要的页面。
我尝试过移动代码,尝试使用人工智能(这很糟糕),并使用了教程,但没有一个起作用。
我正在使用login.php、welcome.php、signup.php、index.php、connectionl.php、logout.php、navbar、php。代码如下:
<?php
session_start();
if(isset($_SESSION['username'])){
header("Location: welcome.php");
}
?>
<?php
$login = false;
include('connection.php');
if (isset($_POST['submit'])) {
$username = $_POST['user'];
$password = $_POST['pass'];
echo $password;
$sql = "select * from signup where username = '$username'or email = '$username'";
$sql = "select * from user_table where username = '$username' or email = '$username'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if($row){
if(password_verify($password, $row["password"])){
$login=true;
session_start();
$sql = "select username from signup where username = '$username'or email = '$username'";
$r = mysqli_fetch_array(mysqli_query($conn, $sql), MYSQLI_ASSOC);
$_SESSION['username']= $r['username'];
$_SESSION['loggedin'] = true;
header("Location: welcome.php");
}
}
else{
echo '<script>
alert("Login failed. Invalid username or password!!")
window.location.href = "login.php";
</script>';
}
}
?>
<?php
include("connection.php");
include("navbar.php");
?>
<html>
<head>
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
<br><br>
<div id="form">
<h1 id="heading">Login Form</h1>
<form name="form" action="login.php" method="POST" required>
<label>Enter Username/Email: </label>
<input type="text" id="user" name="user"></br></br>
<label>Password: </label>
<input type="password" id="pass" name="pass" required></br></br>
<input type="submit" id="btn" value="Login" name = "submit"/>
</form>
</div>
<script>
function isvalid(){
var user = document.form.user.value;
if(user.length==""){
alert(" Enter username or email id!");
return false;
}
}
</script>
</body>
</html>
<?php
session_start();
if(!isset($_SESSION['name'])){
header("location:login.php");
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
include "navbar.php";
?>
<br><br><br>
<div id="form">
<h1>Welcome <?php echo $_SESSION['name'] ?></h1>
</div>
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>
<?php
session_start();
if(isset($_SESSION['name'])){
header("Location: welcome.php");
}
?>
<?php
if(isset($_POST['submit'])){
include "connection.php";
$first_name = mysqli_real_escape_string($conn, $_POST['fname']);
$username = mysqli_real_escape_string($conn, $_POST['user']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, $_POST['pass']);
$cpassword = mysqli_real_escape_string($conn, $_POST['cpass']);
$sql = "select * from user_table where username='$username'";
$result = mysqli_query($conn, $sql);
$count_user = mysqli_num_rows($result);
$sql = "select * from user_table where email='$email'";
$result = mysqli_query($conn, $sql);
$count_email = mysqli_num_rows($result);
if($count_user==0 || $count_email==0){
if($password==$cpassword){
$hash = password_hash($password, PASSWORD_DEFAULT);
$sql = "insert into user_table(first_name, username, email, password) values('$first_name', '$username', '$email', '$hash')";
$result = mysqli_query($conn, $sql);
if($result){
header("Location: login.php");
}
}
else{
echo '<script>
alert("Passwords do not match.");
window.location.href = "signup.php";
</script>';
}
}
else{
if($count_user>0){
echo '<script>
window.location.href="index.php";
alert("Username already exists!!");
</script>';
}
if($count_email>0){
echo '<script>
window.location.href="index.php";
alert("Email already exists!!");
</script>';
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sign up page</title>
<link rel="stylesheet" href = "style.css">
rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<?php
include "navbar.php";
?>
<div id="form">
<h1>Sign up</h1>
<form name="form" action="signup.php" method="POST">
<label>Enter First Name</label>
<input type="text" id="fname" name="fname" required><br><br>
<label>Enter Username</label>
<input type="text" id="user" name="user" required><br><br>
<label>Enter Email</label>
<input type="email" id="email" name="email" required><br><br>
<label>Enter Password</label>
<input type="password" id="pass" name="pass" required><br><br>
<label>Retype Password</label>
<input type="password" id="cpass" name="cpass" required><br><br>
<input type="submit" id="btn" value="Sign up" name="submit"/>
</form>
</div>
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Logged out homepage</title>
rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body style="overflow:hidden;">
<?php
include "navbar.php";
?>
<div style="display:flex; justify-content:center; ">
1200px-Cat_using_computer.jpg">
</div>
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db_name = "users";
$conn = new mysqli($servername, $username, $password, $db_name);
if($conn->connect_error){
die("Connection failed".$conn->connect_error);
}
echo "";
?>
<?php
session_start();
session_destroy();
header("Location: index.php");
?>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">Grocery Store</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="welcome.php">Home</a>
</li>
</ul>
<form class="d-flex">
<a class="btn btn-outline-success mx-2" type="submit" href="signup.php">Signup</a>
<a class="btn btn-outline-primary mx-2" type="submit" href="login.php">Login</a>
<a class="btn btn-outline-danger mx-2" type="submit" href="logout.php">Logout</a>
</form>
</div>
</div>
</nav>
我不是专业人士,但也许在welcome.php中,您正在检查 $_SESSION['name'] 是否存在,以确定用户是否已登录。但是,您正在设置 $_SESSION[ login.php 中的 'username'],而不是 $_SESSION['name']。您可以将welcome.php中的检查更改为$_SESSION['username']。