I have a done a PHP Login ad Registration script with PDO and OOP, all works but not the log out part and can't seem to get the errors solved. Please help me with this I am not so much clued up on php and mysql as I am still learning..
The index page is the page where you log into the site to can access the entire website and if I logout of the site it needs to go back to the login page (index.php)
Errors i receive:
Warning: Missing argument 1 for USER::__construct(), called in C:\wamp\www\hooked\session.php on line 6 and defined in C:\wamp\www\hooked\class.user.php on line 7
Notice: Undefined variable: DB_con in C:\wamp\www\hooked\class.user.php on line 9
Warning: Missing argument 1 for USER::__construct(), called in C:\wamp\www\hooked\logout.php on line 4 and defined in C:\wamp\www\hooked\class.user.php on line 7
Notice: Undefined variable: DB_con in C:\wamp\www\hooked\class.user.php on line 9
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\hooked\class.user.php:7) in C:\wamp\www\hooked\class.user.php on line 70
Fatal error: Uncaught Error: Call to undefined method USER::doLogout() in C:\wamp\www\hooked\logout.php on line 12 ( ! ) Error: Call to undefined method USER::doLogout() in C:\wamp\www\hooked\logout.php on line 12
logout.php
<?php
require_once('session.php');
require_once('class.user.php');
$user_logout = new USER();
if($user_logout->is_loggedin()!="")
{
$user_logout->redirect('index.php');
}
if(isset($_GET['logout']) && $_GET['logout']=="true")
{
$user_logout->doLogout();
$user_logout->redirect('index.php');
}
?>
this is the index.php
<?php
require_once 'dbconfig.php';
if($user->is_loggedin()!="")
{
$user->redirect('hooked_index.php');
}
if(isset($_POST['btn-login']))
{
$uname = $_POST['txt_uname_email'];
$umail = $_POST['txt_uname_email'];
$upass = $_POST['txt_password'];
if($user->login($uname,$umail,$upass))
{
$user->redirect('hooked_index.php');
}
else
{
$error = "Wrong Details !";
}
}
?>
class.user.php
<?php
class USER
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function register($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();
unset($_SESSION['user_session']);
return true;
}
}
?>
session.php
<?php
session_start();
require_once 'class.user.php';
$session = new USER();
// if user session is not active(not loggedin) this page will help 'home.php and profile.php' to redirect to login page
// put this file within secured pages that users (users can't access without login)
if(!$session->is_loggedin())
{
// session no set redirects to login page
$session->redirect('index.php');
}
?>
this is the page it goes to when logged in hooked_index.php
<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
$user->redirect('hooked_index.php');
}
$user_id = $_SESSION['user_session'];
$stmt = $DB_con->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
and this is only the logout "button" and the display of the user name:
<div class="nav navbar-right">
<label><a href="logout.php?logout=true"><i class="glyphicon glyphicon-log-out"></i> logout</a></label>
<br />
Welcome : <?php print($userRow['user_name']); ?>
</div>