dqzlqfqeh845799833 2015-12-23 03:10 采纳率: 0%
浏览 58
已采纳

调试致命错误

i'm trying to make a login page using PDO but i'm getting this error,i have checked similar queries about this error here but they seem to be different from mine

Notice: Undefined variable: user in C:\xampp\htdocs\soft\index.php on line 5

Fatal error: Call to a member function is_loggedin() on null in C:\xampp\htdocs\soft\index.php on line 5

what causes the fatal error and how can i fix this because to my own understanding the code seems to be ok. this is my login page code

<?php
require_once 'dbconfig.php';

if($user->is_loggedin()!="")
{
    $user->redirect('home.php');
}

if(isset($_POST['btn-login']))
{
    $uname = $_POST['txt_uname_email'];
    $umail = $_POST['txt_uname_email'];
    $upass = $_POST['txt_password'];

    if($user->login($uname,$umail,$upass))
    {
        $user->redirect('home.php');
    }
    else
    {
        $error = "Wrong Details !";
    }   
}
?>

this is the login function

public function is_loggedin()
{
    global $db;

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

and this is my user class

<?php
class USER
{
    private $db;

    function __construct($DB_con)
    {
        $this->db = $DB_con;
    }

    public function register($fname,$lname,$uname,$umail,$upass)
    {
        try
        {
            $new_password = MD5($upass);

            $stmt = $this->db->prepare("INSERT INTO users(user_name,user_email,user_pass) 
                                                       VALUES(:uname, :umail, :upass)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);                                        

            $stmt->execute();   

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

    public function login($uname,$umail,$upass)
    {
        try
        {
            $stmt = $this->db->prepare("SELECT * FROM users WHERE user_name=:uname OR user_email=:umail LIMIT 1");
            $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() > 0)
            {
                if($userRow['user_pass']==MD5($upass))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

    public function is_loggedin()
    {
        global $db;

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

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

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

i'm just a newbie and i would like to know what im doing wrong thanks alot

  • 写回答

2条回答 默认 最新

  • dongrang2140 2015-12-26 01:42
    关注

    check the dbconfig.php file make sure you have the following code

    include_once 'class.user.php';
    $user = new USER($DB_con);
    

    i think thats where you need to declare your user.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数