I'm trying to make a signup / sign-in form in PHP, but I have trouble logging out. I keep getting the following error.
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!