我有以下代码登录用户,我希望能够使用注销功能。但出于某种原因,当我去logout.php它给我以下错误:
Notice: Undefined variable: user in /var/www/www.test.com/logout.php on line 2
Fatal error: Call to a member function logout() on null in /var/www/www.test.com/logout.php on line 2
我的index.php
有以下代码<?php
require_once(ROOT . 'include/dbconfig.php');
if($user->is_loggedin()!="")
{
if($_SESSION['type'] = "Admin"){
$user->redirect(DOMAIN . 'admin/index.php');
}else{
$user->redirect(DOMAIN . 'customer/index.php');
}
}
if(isset($_POST['btn-login']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if($user->login($username,$password))
{
if($_SESSION['type'] = "Admin"){
$user->redirect(DOMAIN . 'admin/index.php');
}else{
$user->redirect(DOMAIN . 'customer/index.php');
}
}
else
{
$error = "There was an error with the login";
}
}else{
?>
<html lang="en" class="body-full-height">
<body>
<form action="index.php" method="post">
<table>
<?php
if(isset($error))
{
?>
<tr>
<td><?php echo $error; ?></td>
<?php
}
?>
<tr>
<td>User Name</td>
<td>Password</td>
</tr>
<tr>
<td><input type="text" name"username"></td>
<td><input type="text" name"password"></td>
</tr>
<tr>
<td colspan="2"><button name="btn-login" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>
以下是class.user.php的代码
<?php
class USER
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function register($fname,$lname,$uname,$umail,$upass)
{
try
{
$new_password = MD5($upass);
$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($username,$password)
{
try
{
$stmt = $this->db->prepare("SELECT * FROM users WHERE username=:username
AND password=:password
AND status = 'active' LIMIT 1");
$stmt->execute(array(':username'=>$username, ':password'=>$password));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
if($userRow['isadmin']){
$_SESSION['type'] = "Admin";
$_SESSION['fname'] = $userRow['fname'];
$_SESSION['uid'] = $userRow['uid'];
return true;
}else{
$_SESSION['type'] = "Customer";
$_SESSION['fname'] = $userRow['fname'];
$_SESSION['uid'] = $userRow['uid'];
return true;
}
}else{
return false;
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function is_loggedin()
{
if(isset($_SESSION['uid']))
{
return true;
}
}
public function redirect($url)
{
header("Location: $url");
}
public function logout()
{
session_destroy();
unset($_SESSION['uid']);
return true;
}
}
?>
以下是logout.php的代码
<?php
$user->logout();
?>