Logout error, no connection class found

Asked

Viewed 40 times

1

I have the following logout code:

<?php
    require_once 'session.php';
    require_once 'User.php';

    $user_logout = new User();
    if($user_logout->is_loggedin()!="")
    {
        $user_logout->redirect('./index.php');
    }
    if(isset($_GET['logout']) && $_GET['logout']=="true")
    {
        $user_logout->doLogout();
        $user_logout->redirect('../../login.php');
    }
?>

Where he applies for class User.php that stores all user functions. In it exists the function doLogout():

<?php

require_once './connect/Connect.php';

class User
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;      
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function register($stName, $stUser, $stEmail, $stPhone, $stPassword, $stDepartment, $stLevel, $stAbout)
    {

        try
        {
            $new_password = password_hash($stPassword, PASSWORD_DEFAULT);

            $stmt = $this->conn->prepare("INSERT INTO 
                                            TB001_USUARIO 
                                            (TB001_NOME, 
                                             TB001_USUARIO, 
                                             TB001_EMAIL, 
                                             TB001_TELEFONE_1, 
                                             TB001_SENHA, 
                                             TB002_DEPARTAMENTO_CODIGO, 
                                             TB001_LEVEL, 
                                             TB001_SOBRE) 
                                          VALUES (:name, :user, :email, :phone, :password, :departments, :level, :sobre)");

            $stmt->bindparam(":name", $stName);
            $stmt->bindparam(":user", $stUser);
            $stmt->bindParam(":email", $stEmail);
            $stmt->bindParam(":phone", $stPhone);
            $stmt->bindParam(":password", $new_password);
            $stmt->bindParam(":departments", $stDepartment);
            $stmt->bindparam(":level", $stLevel);
            $stmt->bindparam(":sobre", $stAbout);

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }


    public function doLogin($uname,$umail,$upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT 
                                            TB001_CODIGO, 
                                            TB001_USUARIO, 
                                            TB001_EMAIL, 
                                            TB001_SENHA 
                                          FROM 
                                            TB001_USUARIO 
                                          WHERE 
                                            TB001_USUARIO=:uname 
                                          OR 
                                            TB001_EMAIL=:umail");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($upass, $userRow['TB001_SENHA']))
                {
                    $_SESSION['user_session'] = $userRow['TB001_CODIGO'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    public function selectUser()
    {
        try 
        {
            $stmt = $this->conn->prepare("SELECT 
                                            TB001_USUARIO.*, 
                                            TB002_DEPARTAMENTO.TB002_NOME 
                                          AS 
                                            TB002_DEPARTAMENTO_CODIGO 
                                          FROM 
                                            TB001_USUARIO 
                                          INNER JOIN 
                                            TB002_DEPARTAMENTO 
                                          ON 
                                            TB002_DEPARTAMENTO.TB002_CODIGO = TB001_USUARIO.TB002_DEPARTAMENTO_CODIGO;");           
            $stmt->execute();   

            return $stmt->fetchAll( PDO::FETCH_ASSOC);

        }catch (PDOException $e){
            echo $e->getMessage();
        }
    }

    public function update($usersid, $name, $user, $email, $phone, $level, $active, $departments)
    {
        try
        {
            $stmt = $this->conn->prepare("UPDATE Users 
                                          SET name = :name, user = :user, email = :email, phone = :phone, level = :level, active = :active, departments = :departments 
                                          WHERE usersid = :usersid");
            $stmt->bindParam(':usersid', $_POST['usersid']);
            $stmt->bindParam(':name', $_POST['name']);
            $stmt->bindParam(':user', $_POST['user']);
            $stmt->bindParam(':email', $_POST['email']);
            $stmt->bindParam(':phone', $_POST['phone']);
            $stmt->bindParam(':level', $_POST['level']);
            $stmt->bindParam(':active', $_POST['active']);
            $stmt->bindParam(':departments', $_POST['departments']);            
            $stmt->execute();

            return $stmt;

        }catch (PDOException $e){
            echo $e->getMessage();          
        }
    }

    public function is_loggedin()
    {
        if(isset($_SESSION['user_session']))
        {
            return true;
        }
    }

    public function redirect($url)
    {
        header("Location: $url");
    }

    public function doLogout()
    {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }
}
?>

When I click on the logout button it shows the following error:

Warning: require_once(./connect/Connect.php): failed to open stream: No such file or directory in C: wamp www onsistem connect User.php on line 3

Another application that has the same code is working, it’s pretty much the same thing, does anyone have any idea what the error might be?

  • you have session_start(); first of all?

  • Post your User.php code please

  • Yes, there is @Miguel

  • I amended the post @Davidalves

  • How is your project folder scheme? You can take a print?

2 answers

1


The problem there is that your require_once() cannot find the file Connect.php, probably because User.php is in a different folder than the other file you mention is working. For this you should put the right folder path in the require_once().

The command to upload a folder is ../ so if you want to upload another folder use ../../connect/Connect.php and so on.

Remember that you have to make that path go from where the file that calls require is.

In your specific case, call the require so:

require_once('../Connect.php');

or

require_once('/connect/Connect.php');

or

require_once(__DIR__.'/../Connect.php');
  • He can locate the Class Connect.php on login, I don’t know why I couldn’t find on logout.

  • Sorry I got your answer wrong.

  • I can handle it if I put require_once('C:\wamp\www\onsistem\connect\Connect.php') but it gets really bad code.

  • I left the explanation generic in the answer. But what would make it work for you would be: require_once('../Connect.php'). This means that you will exit the user folder and access the Connect.php file inside the connect folder. I edited the answer to get more complete.

  • I’ve tried, but it doesn’t work like that.

  • How is your project folder scheme? You can take a print?

  • Folder image link: link

  • 1

    It’s just the way I was imagining it. Try require_once(__DIR__.'/../Connect.php');

  • Now it worked, can you just explain to me why I had to use the magic constant (DIR)?

  • 1

    Oops. So, as far as I know it wasn’t necessary hehe. But as it didn’t work I gave you this resource. The constant DIR ensures that you will point out the path you want from the current directory of the file in which you call it.

Show 5 more comments

0

According to the error, PHP is looking for the included file in the path ./connect/Connect.php. This path is relative to the file path where it is being used, i.e., C:\wamp\www\onsistem\connect\User\.

Try to use require_once('../Connect.php');

  • If I use require_once(C:\wamp\www\onsistem\connect\Connect.php) he can logout, but I wouldn’t want to do that, if I use as you mentioned he gets lost.

  • Yes I put the quotes. It appears the same error for all the pages @Guilhermebrügger.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.