-2

I have created a login function for my website but it keeps on giving me the error that there is an undefined variable. The variable it is referring to is the $conn variable which i have defined in server.php file. Below is the code for my function:

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

Here is the code for the server.php file as well and yes i have included the server.php file in the main file

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

I am calling the function from login.php. This is the code:

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

Any help would be appreciated. Thanks.

1 Answers1

-1

Try this code.

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;
mufazmi
  • 1,103
  • 4
  • 18
  • 37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218540/discussion-between-mufazmi-and-paul-heyman). – mufazmi Jul 24 '20 at 19:29