melding: Notice: Undefined variable en json output is null
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:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?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
[code]<?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
// 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
[code]<?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
Gewijzigd op 24/03/2017 15:39:03 door Tom Buyvoets
Ook is het handig als je ook beveiligt tegen sql injections, dit script is zo lek als een mandje.
Je hebt helemaal gelijk. Er moet inderdaad gecontroleerd worden of $row niet leeg is.
Ik heb dit veranderd en nu werkt het.
Je hebt gelijk dat dit nog zo lek is als een mandje. Ik ben nog maar een beginner. Dit script zal niet openlijk gaan draaien. Dit is alleen een opdracht uit de udemy cursus.
utf-8 bestaat niet in MySQL. De meest elementaire UTF-8 set in MySQL is utf8 (zonder streepje). Ook doe je er verstandig aan om de return-value van set_charset() te controleren om na te gaan of het instellen van de character encoding is geslaagd. Mocht dit namelijk niet het geval zijn kan dit voor allerlei rare vertaalproblemen zorgen.
Om je een idee te geven welke UTF-8 character encoderingen jouw MySQL installatie ondersteunt zou je de volgende query kunnen uitvoeren: