necesito ayuda para mi función de inicio de sesión pdo

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.


    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $object = new User();
    $object->Login($user, $pass);

<html lang="en">

<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" />


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

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


class Connection{
    public function dbConnect(){
        return new PDO('mysql:host=localhost; dbname=test', 'root', 'password');

preguntado el 28 de mayo de 14 a las 14:05

Where are you including user.php? -

@andrewsi ninja'd - that's what I was going to write, word for word weirdly O_o -

@CD001 - I think there's only so many ways to write the same thing! -

1 Respuestas

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

error grave

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


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!

Respondido el 20 de junio de 20 a las 10:06

oh sorry i forgot to include bad :D - user3671584

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

ah...I know what you mean now :D - user3671584

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.