网页不断调用未定义的变量,即使它之前已定义

Webpage keeps calling undefined variable even though it is defined before

我已经为我的网站创建了一个登录功能,但它一直给我一个错误,说有一个未定义的变量。它所指的变量是我在 server.php 文件中定义的 $conn 变量。下面是我的函数的代码:

function loginValidation($username, $password, $rememberMe){
    $newPassword = md5($password);
    $userValidation = mysqli_query($conn, "SELECT * FROM users WHERE username='$username' && password='$newPassword' OR email='$username' && password='$newPassword' ");
    $userValidationNum = mysqli_num_rows($userValidation);
    if($userValidationNum == 1):
        $_SESSION["cf_username"] = $username;
            if($rememberMe == 1):
                $hour = time() + 3600 * 24 * 30;
                setcookie('username', $username, $hour);
                setcookie('password', $password, $hour);
            endif;

        header("location:index.php");
    else:
        header("location:login.php?msg=Incorrect Username or Password");
    endif;
    
}

这也是 server.php 文件的代码,是的,我在主文件中包含了 server.php 文件

$conn = mysqli_connect("localhost", "root", "protocol1", "crossfield-portal");
// Check connection
if(mysqli_connect_errno()):
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
endif;

session_start();

我正在调用 login.php 中的函数。这是代码:

if(isset($_POST['submit'])):
if(isset($_POST['rememberMe'])):
  $rememberMe = $_POST['rememberMe'];
else:
  $rememberMe = 0;
endif;
loginValidation($_POST['email'], $_POST['password'], $rememberMe);
endif;

如有任何帮助,我们将不胜感激。谢谢

试试这个代码。

Server.php

<?php
    class Server{
        private $conn;
        function connect()
        {
            $this->conn = mysqli_connect("localhost", "root", "protocol1", "crossfield-portal");
            // Check connection
            if(mysqli_connect_errno())
            {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
                return false;
            }
            return $this->conn;
        }
    }
?>

For function file.

<?php

class Functions
{
    private $conn;  //Database Connection Variable

    function __construct()
    {
        require_once dirname(__FILE__) . '/server.php';   //Check and include properly your server.php file
        $db = new Server;
        $this->conn =  $db->Connect();
    }
    
    
    function loginValidation($username, $password, $rememberMe){
        $newPassword = md5($password);
        $query = "SELECT * FROM users WHERE username=? AND password=? OR email=? AND password=?";
        $stmt = $this->conn->prepare($query);
        $stmt->bind_param("ssss",$username,$newPassword,$username,$newPassword);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows()>0) 
        {
            $_SESSION["cf_username"] = $username;
            if($rememberMe == 1)
            {
                $hour = time() + 3600 * 24 * 30;
                setcookie('username', $username, $hour);
                setcookie('password', $password, $hour);    
            }
            header("location:index.php");
        }
        else
        {
            header("location:login.php?msg=Incorrect Username or Password");
        }
        
    }
?>

Use this for calling the function.

$functions = new Functions;

if(isset($_POST['submit'])):
    if(isset($_POST['rememberMe'])):
        $rememberMe = $_POST['rememberMe'];
    else: $rememberMe = 0; endif;
        $functions->loginValidation($_POST['email'], $_POST['password'], $rememberMe);
endif;