0

I'm making a login system using PDO and my code got some issues.

My problem is when i press the submit button i got an error that says : "Fatal error: Class 'User' not found" in my index.php.

Does anyone can help me find the solution to my problem please.

index.php:

<?php 
session_start();
include_once('connection.php');
if(isset($_POST['submit'])){
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $object = new User();
    $object->Login($user, $pass);
}
?>

<html lang="en">
<head>
</head>

<body>
<form method="post" action="index.php"> 
Username: <input type="text" name="user" />
Password: <input type="text" name="pass" />
<input type="submit" name="submit" value="Login" />
</form>
</body>
</html>

user.php:

<?php
include_once('connection.php');
class User{

    private $db;

    public function __construct(){
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function Login($user, $pass){
        if(!empty($user) && !empty($pass)){
            $st = $this->db->prepare("SELECT * from users WHERE username=? AND password=?");
            $st->bindParam(1, $user);
            $st->bindParam(2, $pass);
            $st->execute();

            if($st->rowCount() == 1){
                echo "User verifies, Access granted";
            } else {
                echo "Incorrect Username or Password";
            }
        }else{
            echo "Please enter Username and Password";
        }
    }
}
?>

connection.php:

<?php
class Connection{
    public function dbConnect(){
        return new PDO('mysql:host=localhost; dbname=test', 'root', 'password');
    }
}
?>
3
  • 1
    Where are you including user.php?
    – andrewsi
    May 28, 2014 at 13:54
  • @andrewsi ninja'd - that's what I was going to write, word for word weirdly O_o
    – CD001
    May 28, 2014 at 13:55
  • @CD001 - I think there's only so many ways to write the same thing!
    – andrewsi
    May 28, 2014 at 13:56

1 Answer 1

3

Ok, let's analyse that error message. Fatal error: Class 'User' not found

Fatal error

Ok, we have a Fatal error, so a error that stops the program. Let's see or we have some extra info.

Class 'User' not found

hmmm, it seems that php can't find the class User. Ok, lets look at our index.php file. hmm, indeed. No User class has been defined in index.php

Aha, that's the problem. So, let's tell php where to find the User class:

include 'User.php';

there you go, thats better.

Those Error messages are really helpfull


Edit:

This code smells

You are not using the word class but molesting it.

  • Your User class is tightly coupled with your Connection class.
  • A new PDO oebject is created everytime you call dbConnect()
  • Your User class is actually a Login function
  • Code convention is '404 not found' (look into PSR-1 and PSR-2)
  • If you include user.php you will also try to include the database. You work around this by using include_once. But this is just saying: 'I have no idea if it is included allready, so let me simply use include_once' (look into autoloading or use a boot.php script)

Sorry, I mean congrats to you!

Sorry for the hard bash, because let's face it. You are using PDO! And that is really good :) So keep up the good work using tools that work. Good luck!

3
  • oh sorry i forgot to include it...my bad :D May 28, 2014 at 13:57
  • Don't, include_once is slow. I think you misread. If you aren't using some form of autoloading. You should have 1 place where you include all the needed files. Don't write include_once all over your application. If you for instance want to change your connection.php files name you will have a nice search for every include_once
    – Pinoniq
    May 28, 2014 at 14:14
  • ah...I know what you mean now :D May 28, 2014 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.