简体   繁体   English

PHP脚本无法运行

[英]PHP script won't run

I'm currently coding a CMS in PHP in order to get back into PHP (I use to use it all the time). 我目前正在用PHP编写CMS,以便重新使用PHP(我一直都在使用它)。 However, for some odd reason, when "including" or "requiring" my classes file, it simply stops the php script, my login form (login.php's html) does not show up (whether I am logged in or not). 但是,出于某种奇怪的原因,当“包含”或“需要”我的类文件时,它只是停止了php脚本,所以我的登录表单(login.php的html)不会显示(无论我是否登录)。 Any help? 有什么帮助吗? Here are two of my scripts: 这是我的两个脚本:

login.php: 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>

and classes.php 和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;
    }
}

?>  

You have a scope problem. 您遇到范围问题。

$conn = mysqli(....)

should be $this->conn = mysqli(....) 应该是$this->conn = mysqli(....)

There are not lots of reasons for a required script to break the parent : the required file does not exist, it has an error or it calls exit() or die() . 所需的脚本破坏父级的原因没有很多:所需的文件不存在,错误或调用exit()die()

Are you sure that the file classes.php is in the same folder as your script, or in the include path ? 您确定文件classes.php与脚本位于同一文件夹中,还是位于include路径中?


Is this the exact code you are using ? 这是您使用的确切代码吗?

With a constructor like this : 用这样的构造函数:

function __construct() {
    $conn = new mysqli($dbname, $dbuser, $dbpass, $db);
}

How the hell do you connect to your database ? 你到底如何连接到数据库?

$mysql = new mySql();
function __construct() {
    $conn = new mysqli($dbname, $dbuser, $dbpass, $db);
}

Should Be 应该

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);
}

Something of that nature. 那种性质的东西。

error_reporting (1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM