我有一个简单的html表单,用于将字段中的信息http POST到文件register.php。在register.php中,成功执行echo以显示已输入文件。下一行使用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>
&#13;
register.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";
}
?>
UserManagement.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;
}
}
&GT;
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;
}
}
&GT;
答案 0 :(得分:1)
您的问题是类定义中的echo
行:
...
class UserManagement
{
echo "entered user management class";
private $conn;
...
类定义子句不能用于执行代码,因为它违反了OOP原则。