无法在PHP中注销
我正在尝试使用PHP进行注册/登录,但是我无法注销.我不断收到以下错误.
I'm trying to make a signup / sign-in form in PHP, but I have trouble logging out. I keep getting the following error.
致命错误:未捕获ArgumentCountError:函数USER :: __ construct()的参数太少,第4行的C:\ xampp \ htdocs \ recepten \ logout.php中传递了0,而在C:\ xampp \ htdocs中恰好期望了1 \ recepten \ class.user.php:6堆栈跟踪:#0 C:\ xampp \ htdocs \ recepten \ logout.php(4):USER-> __ construct()#1 {main}放入C:\ xampp \ htdocs \ recepten \ class.user.php在第6行
Fatal error: Uncaught ArgumentCountError: Too few arguments to function USER::__construct(), 0 passed in C:\xampp\htdocs\recepten\logout.php on line 4 and exactly 1 expected in C:\xampp\htdocs\recepten\class.user.php:6 Stack trace: #0 C:\xampp\htdocs\recepten\logout.php(4): USER->__construct() #1 {main} thrown in C:\xampp\htdocs\recepten\class.user.php on line 6
有人可以建议我做些什么以及如何解决该问题吗?
Can somebody advise me on what I'm doing what and how to fix the problem?
这是我的注销脚本代码行:
This is my line of code for the logout script:
<?php
session_start();
require_once 'class.user.php';
$user = new USER();
if(!$user->is_logged_in())
{
$user->redirect('indexlogin.php');
}
if($user->is_logged_in()!="")
{
$user->logout();
$user->redirect('indexlogin.php');
}
?>
这是我的呼叫用户脚本代码:
This is my code for the call-user script:
<?php
class USER
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function register($fname,$lname,$uname,$umail,$upass)
{
try
{
$new_password = password_hash($upass, PASSWORD_DEFAULT);
$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(password_verify($upass, $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();
$_SESSION['user_session'] = true;
}
}
?>
如果有人可以帮助,那就太好了!
It would be nice if someone could help!
在class.user.php
中,您有一个:
function __construct($DB_con)
{
$this->db = $DB_con;
}
,以及在logout.php
中使用它的时间:
and when you use it in logout.php
:
$user = new USER();
您必须将$DB_con
传递给__constructor
,或创建一个没有参数的__constructor
,并添加另一个函数来初始化DB
:
You have to pass the $DB_con
to __constructor
, or create a __constructor
that has no arguments, and add another function to initialize the DB
:
function __construct()
{
}
public function initDB($DB_con)
{
$this->db = $DB_con;
}
然后您可以像这样使用它:
and then you can use it like that:
$YourDB = whatever_get_DB();
$user = new USER();
// And when you need:
$user.initDB($YourDB);
或仅此一项:
$YourDB = whatever_get_DB();
$user = new USER($YourDB);