php include_once 使我的代码停止运行
Posted
技术标签:
【中文标题】php include_once 使我的代码停止运行【英文标题】:php include_once makes my code stop running 【发布时间】:2015-12-28 14:10:58 【问题描述】:我有一个简单的 html 表单,用于将字段中的信息 http POST 到文件 register.php。在 register.php 中成功执行回显只是为了表明文件已被输入。下一行使用 include_once 包含一个名为 UserManagement.php 的文件,该文件包含一个同名的类。然后尝试实例化这个类,以便可以访问它的方法,但是程序流似乎永远不会超过 register.php 中的第一个 echo 语句。 我希望更有经验的人能直接发现一个菜鸟错误。我花了很长时间研究问题所在,但我似乎无法理解。
我的 2 个 php 文件和我的 html 表单如下:
<form action="register.php" method="POST">
Username: <input type="text" name="uname" /><br />
FirstName: <input type="text" name="fname" /><br />
Last Name: <input type="text" name="lname" /><br />
Date of Birth: <input type="date" name="dob" /><br />
Telephone: <input type="mob" name="tel" /><br />
Email: <input type="email" name="email1" /><br />
Confirm Email: <input type="email" name="email2" /><br />
Password: <input type="password" name="pass1" /><br />
Confirm Password: <input type="password" name ="pass2" /><br />
<input type="submit" value="Register" name="sub" />
<br/><A HREF="login.php">Already Registered? Login Here</A><br/>
</form>
注册.php
<?php
echo "entered register.php";
require_once('UserManagement.php');
echo "userman was included";
$um = new UserManagement();
echo "usermanagement object created";
$response = array("error" => FALSE);
//check that all fields were populated by the http POST.
echo "about to check if fields are all populsted";
if( isset($_POST['uname']) &&
isset($_POST['fname']) &&
isset($_POST['lname']) &&
isset($_POST['tel']) &&
isset($_POST['dob']) &&
isset($_POST['email1']) &&
isset($_POST['email2']) &&
isset($_POST['pass1']) &&
isset($_POST['pass2']))
echo "all fields were populated";
//take values from http POST
$uname = $_POST['uname'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$tel = $_POST['tel'];
$dob = $_POST['dob'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$um->registerUser($uname,$fname,$lname,$tel,$dob,$email1,$pass1);
else
echo "not all fields were populated";
?>
用户管理.php
<?php
require_once('DB_Connect.php');
//Contains functions for all user management related isues i.e. add, remove, edit user.
//only user management - essentially CRM.
class UserManagement
echo "entered user management class";
private $conn;
function __construct()
echo "user management constructo end";
$db = new DB_Connect();
$this->conn = $db->connect();
echo "user management constructo end";
function __destruct()
//TODO
function registerUser($username,$firstname,$lastname,$telephone,$dob,$email,$password)
echo "you chose to register a new user.";
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$sql = "INSERT INTO User(`id`,`username`,`first_name`,`last_name`,`telephone`,`email`,`password`) VALUES (NULL,?,?,?,?,?,?)";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param('ssssss',$username,$firstname,$lastname,$telephone,$email,$encrypted_password);
$result = $stmt->execute();
echo "user added";
echo $result;
$stmt->close();
function unregisterUser($uname,$pass1,$pass2)
echo "you chose to deregister a user.";
public function hashSSHA($password)
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
public function checkhashSSHA($salt, $password)
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
?>
DB_Connect.php
<?php
class DB_Connect
private $conn;
// Connecting to database
public function connect()
// Connecting to mysql database
require_once('Config.php');
$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
// return database handler
return $this->conn;
?>
【问题讨论】:
请在您的 php 文件顶部添加 error_reporing( -1 ); 以获取错误并在此处分享以寻求帮助:-) 显示 require_once('DB_Connect.php') 的内容;或删除该行进行测试。 您的班级名称是 UserManagement,您正在调用 $um->registerUser 把它改成 $UserManagement->registerUser . 是error_reporting
,不是error_reporing
【参考方案1】:
您的问题是类定义中的echo
行:
...
class UserManagement
echo "entered user management class";
private $conn;
...
类定义子句不能用于执行代码,因为它违反了 OOP 原则。
【讨论】:
以上是关于php include_once 使我的代码停止运行的主要内容,如果未能解决你的问题,请参考以下文章