我已经完成了一个带有PDO和OOP的PHP登录广告注册脚本,所有工作但不是注销部分,似乎无法解决错误。请帮助我这个我没有太多关于php和mysql的知识,因为我还在学习..
索引页面是您登录站点以访问整个网站的页面,如果我注销该站点,则需要返回登录页面(index.php)
我收到的错误:
警告:调用USER :: __ construct()时缺少参数1 C:\ wamp \ www \ hooked \ session.php在第6行并在中定义 第7行的C:\ wamp \ www \ hooked \ class.user.php
注意:未定义的变量:DB_con in 第9行的C:\ wamp \ www \ hooked \ class.user.php
警告:调用USER :: __ construct()时缺少参数1 C:\ wamp \ www \ hooked \ logout.php在第4行并在中定义 第7行的C:\ wamp \ www \ hooked \ class.user.php
注意:未定义的变量:DB_con in 第9行的C:\ wamp \ www \ hooked \ class.user.php
警告:无法修改标头信息 - 已发送的标头 (输出从C:\ wamp \ www \ hooked \ class.user.php:7开始) 第70行的C:\ wamp \ www \ hooked \ class.user.php
致命错误:未捕获错误:调用未定义的方法 第12行的C:\ wamp \ www \ hooked \ logout.php中的USER :: doLogout()(!) 错误:调用未定义的方法USER :: doLogout()in 第12行的C:\ wamp \ www \ hooked \ logout.php
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');
}
?>
这是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');
}
?>
这是登录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);
?>
这只是注销“按钮”和用户名的显示:
<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>
答案 0 :(得分:0)
您必须在User对象中传递$DBcon
变量,
您需要更改 logout.php 代码,
$user_logout = new USER($DBCon);
因为你有,
function __construct($DB_con)
{
$this->db = $DB_con;
}
在class.user.php
并替换hooked_index.php
代码,
if(!$user->is_loggedin())
{
$user->redirect('hooked_index.php');
}
有了这个,
if(!$user->is_loggedin())
{
$user->redirect('index.php'); // or 'login.php'
}
在index.php
中,从
if($user->is_loggedin()!="")
要,
if($user->is_loggedin())