doufei6456 2019-02-28 17:52
浏览 49

调用成员函数..在字符串上,我如何通过此错误

creating a log in page where if a user is already logged in they are redirected to the home page, however upon execution I get the error code :

call to a member function isloggiedin() on string

what am I doing wrong here?

<?php
include "H:\p3t\phpappfolder\public_php\FYP\includes\api\config\database.php";


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

if(isset($_POST['btn-login'])) // line  11 where new error exists
{
    $user = $_POST['username'];
    $password = $_POST['password'];

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

the following code may also help as it is my class.user.php file:

<?php
class USER
{
    private $db;

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



    public function login($user,$password)
    {
        try
        {
            $stmt = $this->db->prepare("SELECT * FROM user WHERE user_name=:username OR user_email=:password LIMIT 1");
            $stmt->execute(array(':username'=>$user, $password));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() > 0)
            {
                if(password_verify($password, $userRow['user_pass']))
                {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        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 logout()
    {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }
}
?>

as you can see I have the function "isloggedin" so I don't understand why I am getting the error

after following the advice in the answer I was presented with the error message :

Parse error: syntax error, unexpected 'if' (T_IF) in H:\p3t\phpappfolder\public_php\FYP\includes\api\login.php on line 11

  • 写回答

1条回答 默认 最新

  • douyiqi9640 2019-02-28 17:57
    关注

    $user is the string parameter, not a User object, so you can't use $user->is_loggedin().

    The is_loggedin() method doesn't use any class properties, so it can be a static method.

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

    Then you can use

    if (User::is_loggedin())
    

    Also, you shouldn't set $user and $password before

    if (isset($_POST['btn-login']))
    
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作