i have a simple html form which is used to http POST the information in the fields to a file register.php. In register.php an echo is performed successfully just to show that the file has been entered. The next line uses an include_once to include a file named UserManagement.php which contains a class of the same name. An attempt is then made to instantiate this class so its methods can be accessed, however the program flow never seems to make it past the first echo statement in register.php. I'm hoping someone more experienced will spot a rookie error straight awaw. i've spent a good while researching what the problem is and i cant seem to get it.
my 2 php files and my html form are below:
<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>
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;
}
}
?>
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;
}
}
?>
Your problem is the echo
line inside the class definition:
...
class UserManagement
{
echo "entered user management class";
private $conn;
...
The class definition clause can't be used to execute code since it violates the OOP principles.