我使用mysql和php创建了一个简单的登录页面。
我不使用注册表格,因此我将用户数据手动添加到在此之前创建的数据库中。
如果登录失败,则该表单应将我重定向到欢迎页面或显示错误消息,但该表单仅显示php代码(波纹管)。如果没有其他要求,该表单至少应返回错误消息...我检查了php代码,对我来说似乎很好,所以我不知道该怎么办?
我正在使用Index.php,login.php,session.php,profile.php(欢迎页面)和logout.php。我在这里添加所有这些内容:
INDEX.PHP
expandtab
LOGIN.PHP
<?php
include('login.php');
if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Prijava</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="login">
<img src="http://granital.owjej.org/wp-content/uploads/2019/02/granital1-1-300x75.png" class="logo" alt="" width="300" height="75" />
<h2>Prijava v sistem</h2>
<form action="login.php" method="post">
<input id="name" name="username" placeholder="Uporabniško ime" type="text">
<input id="password" name="password" placeholder="Geslo" type="password"><br><br>
<input name="submit" type="submit" value=" Prijava">
<span><?php echo $error; ?></span>
</form>
</div>
</body>
</html>
LOGOUT.PHP
<?php
session_start();
$error = '';
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
} else {
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysqli_connect("localhost", "owjej_david", "dadadada", "owjej_kalkulator");
$query = "SELECT username, password from login where username=? AND password=? LIMIT 1";
$stmt = $conn->prepare($query);
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->bind_result($username, $password);
$stmt->store_result();
if ($stmt->fetch()) {
$_SESSION['login_user'] = $username;
header("location: profile.php");
}
mysqli_close($conn);
}
}
?>
PROFILE.PHP
<?php
session_start();
if(session_destroy()) {
header("Location: index.php");
}
?>
SESSION.PHP
<?php
include('session.php');
if(!isset($_SESSION['login_user'])){
header("location: index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout"><a href="logout.php">Log Out</a></b>
</div>
</body>
</html>
我一直重定向到包含此php代码的页面,所以我觉得我的php代码可能有问题吗?
感谢您的帮助,很抱歉很长的帖子。