我是php新手,我似乎无法使我的登录脚本正常工作。当我输入现有用户数据并单击“登录”时,它会将我发送到空白页。我认为这可能与我的代码中的if (isset($_POST['login-submit']))
有关。
即使在php.ini中打开了错误报告功能,该页面仍为空白。我将不胜感激!谢谢!
我的用户表结构:
下面,我为我的登录脚本和运行脚本的header.php
附加了代码。
login-inc.php:
<?php
ini_set('display_startup_errors', true);
error_reporting(E_ALL);
ini_set('display_errors', true);
if (isset($_POST['login-submit'])){
require 'dbh-inc.php';
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
if (empty($uid) || empty($pwd)) {
header("Location: ../index.php?error=emptyfields");
exit();
}
else{
$sql = "SELECT * FROM users WHERE user_uid=?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)){
header("Location: ../index.php?error=sqlerror");
exit();
}
else{
mysqli_stmt_bind_param($stmt, "s", $uid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)){
$pwdCheck = password_verify($pwd, $row['user_pwd']);
if ($pwdCheck == false){
header("Location: ../index.php?error=wrongpwd");
exit();
}
else if ($pwdCheck == true){
session_start();
//Stores the user's id, username, and user type in the session
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_uid'] = $row['user_uid'];
$_SESSION['user_usertype'] = $row['user_usertype'];
header("Location: ../index.php/?login=success");
exit();
}
else{
header("Location: ../index.php?error=wrongpwd");
exit();
}
}
}
//Closes the prepared statement and the db connection
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
}
?>
header.php:
<?php
ini_set('display_startup_errors', true);
error_reporting(E_ALL);
ini_set('display_errors', true);
require "includes/dbh-inc.php";
?>
<title> </title>
<link rel="stylesheet" type="text/css" href="interfacestyles.css">
<body>
<header>
<nav>
<div class=”main-wrapper”>
<div class=”nav-login”>
<a class="account" href="">My account</a>
<?php
// if user not logged in, show login button and input fields
if (!isset($_SESSION['user_id'])) {
echo '<form action="includes/login-inc.php" method="POST">
<input type=”text” name=”uid” placeholder=”Username”>
<input type=”password” name=”pwd” placeholder=”Password”>
<button type=”submit” name=”login-submit”>Login</button>
New? Register <a href="signup.php">here</a>
</form>';
}
// if user is logged in, show log out button
else if (isset($_SESSION['user_id'])) {
echo '<form action="includes/logout-inc.php" method="POST">
<button type=”submit” name=”login-submit”>Log out</button>
</form>';
}
?>
</div>
</div>
</nav>
</header>
答案 0 :(得分:1)
我认为您很想在header.php文件上启动会话,请尝试将session_start();
放在文件顶部