从登录页面加载我的 php 页面不会给出错误,但从我的应用程序中的其他页面加载它会出现错误

当我尝试从登录页面(index.php)加载上面的 php 页面时,没有错误,但是当我登录后尝试从应用程序中的任何其他页面加载它时,它在控制台上给我一个未捕获的语法错误。它指向脚本并表示 php 变量 $name 未定义。

<?php


if (isset($_POST["login"])) {

session_start();

$_SESSION["email"] = $_POST["email"];

$email = $_POST["email"];

$password = $_POST["password"];

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$dbname = 'attendance_system';


$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);


if ($conn->connect_error) {

    die("Failed to connect: " . $conn->connect_error);

} else {

    $success = false;

    $query = "SELECT * FROM student";

    $result = $conn->query($query);

    while ($row = $result->fetch_assoc()) {

        if ($row["email"] === $email && $row["password"] === $password) {

            $name = $row["name"];

            $age = $row["age"];

            $criteria = $row["attendance_criteria"];

            $course = $row["course"];

            $college = $row["college_name"];

            $attendance = $row["attendance"];

            $success = true;

            break;

        }

    }


    if ($success === false) {

        header("Location: index.php");

    }

}

}


if (isset($_POST["signup"])) {


session_start();

$_SESSION["email"] = $_POST["email1"];

$email = $_POST["email1"];

$password = $_POST["password1"];

$name = $_POST["name"];

$age = $_POST["age"];

$criteria = 100;

$course = null;

$college = null;

$attendance = 0;

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$dbname = 'attendance_system';


$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);


if ($conn->connect_error) {

    die("Failed to connect: " . $conn->connect_error);

} else {

    $query = "INSERT INTO student SET email = '$email', name = '$name', age = '$age', password = 

 '$password' ";

    $result = $conn->query($query);

}

}


?>


<!DOCTYPE html>

<html>


<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width">

<title>Attendance Manager - Home</title>

<link rel="stylesheet" href="../css/skeleton.css">

<link rel="stylesheet" href="../css/home.css">

<link rel="icon" type="image/jpg" href="../media/icon.png">

</head>



红颜莎娜
浏览 104回答 1
1回答

潇湘沐

如果您的页脚<script>始终运行并且始终期望$name被填充,那么您必须检查会话是否已登录。如果未登录,请将用户重定向到登录页面,或指定默认值。另外,您可能应该重写用于验证用户身份的 SQL。首先,将密码保存为哈希值。其次,使用WHERE语句来获取与电子邮件和密码匹配的任何行,而不是循环访问应用程序中的结果。您可能还想修复var pasword拼写错误,我建议您将$success变量重命名为更具描述性的名称,例如$loggedIn.&nbsp;对于这个特定目的来说,这可能并不重要,但养成正确命名变量的习惯是一件好事。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript