duanbichou4942 2012-08-20 09:29
浏览 57
已采纳

致命错误:在非对象上调用成员函数AddUser()

so after hours on the web searching for smallest details or solution for this error I still can't fix it

The errors :

Notice: Undefined variable: Login in C:\xampp\htdocs\up\administration\adduser.php on line 3

Fatal error: Call to a member function AddUser() on a non-object in C:\xampp\htdocs\up\administration\adduser.php on line 3

These errors are from adduser.php which contains

<?php
require_once('LoginClass.php');
$Login->AddUser('Test','test312');
?>

And this is the LoginClass.php

<?php

class Login {

//Username Variables
private $username;
private $password;

//MySQL Variables
private $Host;
private $MySQLUsername;
private $MySQLPassword;
private $Database;
private $Conn;


//Constructor
public function Login()
{
    session_start();
    $this->Host = "localhost";
    $this->MySQLUsername = "root";
    $this->MySQLPassword = "";
    $this->Database = "up";

    $this->Connection();

    unset($this->Host);
    unset($this->MySQLUsername);
    unset($this->MySQLPassword);
    unset($this->Database);
}
//**********************
//Mysql Functions
//********************** 
public function Connection()
{
    $this->Conn = @mysql_connect($this->Host,$this->MySQLUsername,$this->MySQLPassword);

    if($this->Conn)
    {
        mysql_select_db($this->Database) OR die('Could not select DB');
    }
    else
    {
        die(mysql_error());
    }  
}

public function Query($sql)
{
    $result = mysql_query($sql);

    if(!$result)
    {
        die(mysql_error());
    }

    return $result;
}

public function Disconnect()
{
    mysql_close($this->Conn);
}

//Escapes bad values for MySQL to prevent SQL injections.
public function EscapeString($badstring)
{
    if(!get_magic_quotes_gpc())
    {
        $goodstring = addslashes($badstring);
    }
    else
    {
        $goodstring = stripslashes($badstring);
    }

    $goodstring = mysql_real_escape_string($badstring);

    return $goodstring;
}

public function EncryptPassword($password)
{
  return sha1(md5($password));  
} 
//Check if the user can login
public function CheckLogin($username,$password)
{
    $this->username = $this->EscapeString($username); 
    $this->password = $this->EscapeString($this->EncryptPassword(($password)));

    $result = $this->Query("SELECT * FROM `users` WHERE `username` = '$this->username' AND `password` = '$this->password' LIMIT 1");

    //If we get one result we know the login is right.
    if(mysql_num_rows($result) == 1)
    {
        $this->username = $username;
        $_SESSION['username'] = $this->username;
        $_SESSION['authorized'] = 1; 

        header('location:Private.php');
    }
    else 
    {
        die('Invalid Login');
    }

}
//Add a user
public function AddUser($username,$password)
{
    //$username = $this->EscapeString($username);
    //$password = $this->EscapeString($this->EncryptPassword($password));
    $username = $this->$username;
    $password = $this->$this->EncryptPassword($password);

    $result = $this->Query("INSERT INTO `users` (username,password) VALUES ('$username','$password')");
}
//Takes the result of a query and puts the information into an array
public function Result_To_Array($result)
{
    $result_array = array();

    for ($i=0; $row = mysql_fetch_array($result); $i++) 
    {
        $result_array[$i] = $row;
    }

    return $result_array;

}
//Delete user
public function DeleteUser($username)
{
    $username = $this->EscapeString($username);

    $result = $this->Query("DELETE FROM `users` WHERE `username` = '$username' LIMIT 1");

}
//Checks if the user is authorized or not
public function IsAuth()
{
    if(isset($_SESSION['username']) && $_SESSION['authorized'] == 1)
    return true;

    else
    {
        die('You are not authorized to view this information');
        header('login.html');
    }    
}
//Shows user's IP
public function GetIP()
{
    return $_SERVER['REMOTE_ADDR'];
}
//Display all users
public function ShowUsers()
{
    $users = $this->Result_To_Array($this->Query("SELECT * FROM `users`"));

    foreach($users as $user)
    {
        echo $user['username']."<br />";
    } 
}

public function LogOut()
{

     session_destroy();

     header('location:login.html');
}
}
?>
  • 写回答

1条回答 默认 最新

  • dongxiejie9387 2012-08-20 09:32
    关注

    you should create new object Login() :

    require_once('LoginClass.php');
    $Login = new Login();
    $Login->AddUser('Test','test312');
    

    or call the function directly like this :

    require_once('LoginClass.php');
    Login::AddUser('Test','test312');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?