Goedemiddag mede php'ers,
Ik volg een cursus op udemy.com die betrekking heeft op php, sql en swift(iOS). Nu krijg ik in mijn class in access.php steeds een melding: Notice: Undefined variable: returnArray in /Applications/XAMPP/xamppfiles/htdocs/twitter/secure/access.php on line 91
Daarnaast is mijn JSON output bij meerdere "waardes" null.
{"status":"200","message":"Succesfully registered","id":null,"username":null,"email":null,"fullname":null,"ava":null}
Ik heb het idee dat deze twee fouten met elkaar te maken hebben.
Ik kom er na veel googelen niet meer uit.
Zou iemand mij kunnen helpen?
access.php:
<?php
// create class to access this php file
class access {
// connection global variables
var $host = null;
var $user = null;
var $pass = null;
var $name = null;
var $conn = null;
// constructing class
function __construct($dbhost, $dbuser, $dbpass, $dbname) {
$this->host = $dbhost;
$this->user = $dbuser;
$this->pass = $dbpass;
$this->name = $dbname;
}
// connection function
public function connect() {
// establish connection and store it in $conn
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->name);
//if error
if (mysqli_connect_errno()) {
echo 'could not connect to database';
}
// support all languages
$this->conn->set_charset("utf-8");
}
// disconnect function
public function disconnect() {
if ($this->conn != null) {
$this->conn->close();
}
}
// insert user details
public function registerUser($username, $password, $salt, $email, $fullname) {
$sql = "INSERT INTO users SET username=?, password=?, salt=?, email=?, fullname=?";
$statement = $this->conn->prepare($sql);
if (!$statement) {
try{
// code that may throw an exception
} catch(Exception $statement){
echo $$statement->getMessage();
}
// throw new Exception($statement->error);
}
$statement->bind_param("sssss", $username, $password, $salt, $email, $fullname);
$returnValue = $statement->execute();
return $returnValue;
}
// get user information
public function selectUser($username) {
//sql command
$sql = "SELECT * FROM users WHERE username='".$username."'";
//assign result we got from $sql to $result var
$result = $this->conn->query($sql);
// if we have at least 1 result returned
if ($result != null && (mysqli_num_rows($result) >= 1 )) {
// assing results we got to $ row as associative array
$row = $result->fetch_array(MYSQLI_ASSOC);
if (empty($row)) {
$returnArray = $row;
}
}
return $returnArray;
}
// save email conformation token
public function saveToken($table, $id, $token) {
// sql statement
$sql = "INSERT INTO $table SET id=?, token=?";
// prepare statement to be executed
$statement = $this->conn->prepare($sql);
if (!$statement) {
try{
// code that may throw an exception
} catch(Exception $statement){
echo $statement->getMessage();
}
}
// bind param to sql statement
$statement->bind_param("is", $id, $token);
// launch / execute and store feedback in $returnValue
$returnValue = $statement->execute();
return $returnValue;
}
}
register.php
<?php
// step 1. declare parms of user info
// securing info and storing in variables
$username = htmlentities($_REQUEST["username"]);
$password = htmlentities($_REQUEST["password"]);
$email = htmlentities($_REQUEST["email"]);
$fullname = htmlentities($_REQUEST["fullname"]);
// check for empty get or post
if (empty($username) || empty($password) || empty($email) || empty($fullname)) {
$returnArray["status"] = "400";
$returnArray["message"] = "Missing required information";
return;
}
// secure password
$salt = openssl_random_pseudo_bytes(20);
$secured_password = sha1($password . $salt);
// step 2. Build connection
// Secure way to build connection
$file = parse_ini_file("../../../twitter.ini");
// store in php var info from ini var
$host = trim($file["dbhost"]);
$user = trim($file["dbuser"]);
$pass = trim($file["dbpass"]);
$name = trim($file["dbname"]);
// include access.php
require("secure/access.php");
$access = new access($host, $user, $pass, $name);
$access->connect();
// step 3. Insert user info
$result = $access->registerUser($username, $secured_password, $salt, $email, $fullname);
if ($result) {
//got currend registered user information
$user = $access->selectUser($username);
// declare information to feedback to user App as JSON
$returnArray["status"] = "200";
$returnArray["message"] = "Succesfully registered";
$returnArray["id"] = $user["id"];
$returnArray["username"] = $user["username"];
$returnArray["email"] = $user["email"];
$returnArray["fullname"] = $user["fullname"];
$returnArray["ava"] = $user["ava"];
// step 4. Emailen
// include email.php
require ("secure/email.php");
// store all class in $email
$email = new email();
// store generated token in $token
$token = $email->generateToken(20);
// save info in emailtoken table
$access->saveToken("emailTokes", $user["id"], $token);
// reffer emailing information
$details = array();
$details["subject"] = "email confirmation on twitter";
$details["to"] = $user["email"];
$details["fromName"] = "Tom Buyvoets";
$details["fromEmail"] = "[email protected]";
// access template file
$template = $email->confirmationTemplate();
// replace {token} from confirmationTemplate.html by $token and store all in $tamplate
$template = str_replace("{token}", $token, $template);
$details["body"] = $template;
$email->sendEmail($details);
} else {
$returnArray["status"] = "400";
$returnArray["message"] = "Could not register with provided information";
}
// Step 5. close connection
$access->disconnect();
// step 6. Json data
echo json_encode($returnArray);
echo $returnArray["id"];
?>
Alvast bedankt.
Groet,
Tom
2.096 views