Я новичок в PHP и пытаюсь создать экран входа в систему с помощью PHP ... но я всегда получаю эту ошибку
(!) Неустранимая ошибка: вызов участникаФункция login () для необъекта в C: \ wamp \ www \ photo_gallery \ public \ admin \ login.php в строке 12
Вот мой код:
Логин.php:
<?php
require_once("../../includes/database.php");
require_once("../../includes/user.php");
require_once("../../includes/functions.php");
require_once("../../includes/session.php");
?>
<?php
if(isset($_POST["submit"])){
$obj = User::auth();
if($obj){
$session->login($obj);
redirect_to("index.php");
} else {
$message= "USER NOT FOUND.";
}
echo '<p id="message">';
echo output_message($message);
echo "</p>";
}
?>
<html>
<head>
<title>Hello</title>
<link rel="stylesheet" href="stylesheets/main.css">
</head>
<body>
<header id="header">
<h1>Welcome</h1>
</header>
<div id="main">
<h3>Please Login</h3>
<form action="login.php" method="post">
<p><label for="username">Username :</label><input type="text" name="username" placeholder="Username"/></p>
<p><label for="password">Password :</label><input type="text" name="password" placeholder="Password"/></p>
<input type="submit" value="Submit" name="submit" />
</form>
</div>
</body>
session.php
<?php
require_once("database.php");
require_once("user.php");
require_once("functions.php");
?>
<?php
class Session{
private $logged = false;
public $user_id;
function __construct(){
session_start();
if($this->is_logged()){
redirect_to("index.php");
} else {
$message = "Try Again!";
}
}
public function is_logged(){
return $logged;
}
public function login($user){
if($user){
$this->user_id = $_SESSION["user_id"] = $user->id ;
$this->logged = true ;
}
}
public function logout(){
unset($_SESSION["user_id"]);
unset($this->user_id);
redirect_to("login.php");
}
}
?>
user.php
<?php
require_once("database.php");
require_once("session.php");
class User {
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
public static function find_all(){
global $db;
$object = new self;
$query = "SELECT * FROM users";
$result = $db->add_query($query);
$object = array();
while($found = $db->fetch_array($result)){
$object[] = self::resolve_data($found);
}
return $object;
}
public static function find_by_id($id){
global $db;
$object = new self;
$query = "SELECT * FROM users WHERE id={$id}";
$result = $db->add_query($query);
$found = $db->fetch_array($result);
$object = self::resolve_data($found);
return $object;
}
public static function auth(){
global $db;
global $session;
$username = trim($_POST["username"]);
$password = trim($_POST["password"]);
$query = "SELECT * FROM users
WHERE username = '{$username}' AND password = '{$password}' LIMIT 1";
$result = $db->add_query($query);
if($data = $db->fetch_array($result)){
$obj = self::find_by_id($data["id"]);
return $obj;
} else {
$message="user not found.";
}
}
private static function resolve_data($arr){
$object = new self;
$object->id = $arr["id"];
$object->username = $arr["username"];
$object->password = $arr["password"];
$object->first_name = $arr["first_name"];
$object->last_name = $arr["last_name"];
return $object;
}
}
?>
database.php
<?php
class Database{
private $connection ;
function __construct(){
$this->open_connection();
}
public function open_connection(){
$this->connection = mysql_connect("localhost" , "root" , "smokn");
if(!$this->connection){
die("Error while opening connection: " . mysql_error() );
} else {
$db_select = mysql_select_db("photo_gallery" , $this->connection);
if(!$db_select){
die("Error while selecting DB: " . mysql_error() );
}
}
}
public function close_connection(){
if(isset($this->connection)){
mysql_close($this->connection);
unset($this->connection);
}
}
public function add_query($query){
$result = mysql_query($query , $this->connection);
if(!$result){
die("Error while adding query: " . mysql_error());
} else {
return $result;
}
}
public function fetch_array($result){
return mysql_fetch_array($result);
}
public function escape_value ($value){
$value = mysql_real_escape_string($value);
return $value;
}
public function num_rows ($result){
return mysql_num_rows($result);
}
public function inserted_id(){
return mysql_insert_id($this->connection);
}
public function affected_rows(){
return mysql_affected_rows($this->connection);
}
}
$db = new Database() ;
?>
Это действительно расстраивает.Я потратил около 2 часов, пытаясь понять это, но безуспешно ..