我目前正在使用PHP编写CMS,以便重新使用PHP(我总是使用它)。但是,由于一些奇怪的原因,当“包含”或“要求”我的类文件时,它只是停止php脚本,我的登录表单(login.php的html)没有显示(我是否登录)。有帮助吗?这是我的两个脚本:
的login.php:
<?php
session_start();
include "classes.php";
if(isset($_GET['logout'])) {
setupSession(2);
}
if($_SESSION['status'] == "online") header("location: admin.php");
if($_POST && isset($_POST['username']) && isset($_POST['password'])) {
$un = $_POST['username'];
$pwd = $_POST['password'];
$mysql = new mySql();
$mysql->validateUser($un, $pwd);
} else $attempt = 2;
?>
<html>
<head>
<title>Log In</title>
</head>
<body>
<form method="post" action="">
<label for="username">username: </label>
<input type="text" name="username" />
<label for="password">password: </label>
<input type="password" name="password" />
<input type="submit" value="Log In" name="submit" />
</form>
</body>
</html>
和classes.php
<?php
class mySql {
protected $dbname;
protected $dbuser;
protected $dbpass;
protected $db;
private $conn;
function __construct() {
$conn = new mysqli($dbname, $dbuser, $dbpass, $db);
}
public function validateUser($username, $password) {
$query = "SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1";
if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('ss', $username, $password);
$stmt->execute();
if($stmt->fetch()) {
$stmt->close();
setupSession(1);
} else $attempt = 1;
}
}
}
function setupSession($status) {
switch($status) {
case 1:
$_SESSION['status'] = "online";
//other user variables
header("location: admin.php");
break;
case 2:
unset($_SESSION['status']);
if(isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time() - 1000);
}
session_destroy();
break;
default:
session_start();
if($_SESSION['status'] != "online") header("location: login.php");
break;
}
}
?>
答案 0 :(得分:3)
您有范围问题。
$conn = mysqli(....)
应为$this->conn = mysqli(....)
答案 1 :(得分:2)
所需脚本破坏父级的原因不多:所需文件不存在,出错或调用 exit()或 die()< / em>的。
您确定文件classes.php与脚本位于同一文件夹中,还是位于包含路径中?
这是您正在使用的确切代码吗?
使用这样的构造函数:
function __construct() {
$conn = new mysqli($dbname, $dbuser, $dbpass, $db);
}
你怎么连接数据库?
$mysql = new mySql();
答案 2 :(得分:1)
function __construct() {
$conn = new mysqli($dbname, $dbuser, $dbpass, $db);
}
应该
function __construct($dbname, $dbuser, $dbpass, $db) {
$this->dbname = $dbname;
$this->dbuser = $dbuser;
$this->dbpass = $dbpass;
$this->db = $db;
$this->connect();
}
function connect()
{
$this->conn = new mysqli($this->dbname, $this->dbuser, $this->dbpass, $this->db);
}
那种性质。
答案 3 :(得分:0)
error_reporting (1);