网页继续调用未定义的变量,即使它是在 [duplicate] 之前定义的

Webpage keeps calling undefined variable even though it is defined before [duplicate]

提问人:Paul Heyman 提问时间:7/25/2020 最后编辑:Paul Heyman 更新时间:7/25/2020 访问量:60

问:

我已经为我的网站创建了一个登录功能,但它一直给我错误,即有一个未定义的变量。它所指的变量是我在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;

任何帮助将不胜感激。谢谢。

php html 函数 mysqli

评论

0赞 mufazmi 7/25/2020
您是否包含server.php文件
0赞 Paul Heyman 7/25/2020
我已经包含了文件
0赞 Paul Heyman 7/25/2020
我记住了这一点。谢谢

答:

-1赞 mufazmi 7/25/2020 #1

试试这段代码。

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;
        }
    }
?>

对于函数文件。

<?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");
        }
        
    }
?>

使用它来调用函数。

$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;

评论

0赞 mufazmi 7/25/2020
让我们在聊天中继续讨论