I want to create a registration and login with rest api using PHP mysql in my Application.
This is my code
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credential: true');
header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Authorization, Accept, X-Requested-With, x-xsrf-token');
header('Content-Type: application/json; charset=utf-8');
include'config.php';
$postjson = json_decode(file_get_contents('php://input'), true);
$today = date('Y-m-d H:i:s');
if($postjson['aksi'] == 'registration_progress'){
$emailcheck = mysqli_fetch_array(mysqli_query($mysqli, "SELECT email FROM users WHERE email = '$postjson[email]'"));
if($emailcheck['email'] == $postjson['email']){
$result = json_decode(array('success' => false, 'msg' => 'Email Sudah Terdaftar'));
}else{
$password= md5($postjson['password']);
$query = mysqli_query($mysqli, "INSERT INTO users SET
nama_user = '$postjson[nama_user],
email = '$postjson[email],
password = '$password,
createat = '$today',
");
if($query) $result = json_encode(array('success' => true, 'msg' => 'Registrasi Berhasil !!'));
else $result = json_encode(array('success' => false, 'msg' => 'Registrasi Gagal !!'));
echo $result;
}
}elseif($postjson['aksi'] == 'login_progress'){
$password = md5($postjson['password']);
$logindata = mysqli_fetch_array(mysqli_query($mysqli, "SELECT * FROM users WHERE email = '$postjson[email]' AND password = '$password'"));
$data = array(
'id_user' => $logindata['id_user'],
'nama_user' => $logindata['nama_user'],
'email' => $logindata['email']
);
if($logindata){
$result =json_encode(array('success' => true, 'result' => $data));
}else{
$result =json_encode(array('success' => false));
}
echo $result;
}
I don’t know my error
Notice Trying to access array offset on value of type null in C:xampphtdocsapi-ionicapi.php on line 13
Notice: Trying to access array offset on value of type null in C:xampphtdocsapi-ionicapi.php on line 31
3
Answers
I am writing this answer not just to answer your question but to also highlight some critical issues with your code:
First; the answer:
$postjson['aksi']
is line 13. The varible$postjson
doesn’t exist or is valued asnull
. You should pre-empt this check with a qualifier to check that this variable is an array and contains the referenced array entity.And do likewise on line 31.
See
is_array()
And
array_key_exists()
From PHP 8 onwards these notices are now
WARNING
level errors so try and resolve them sooner rather than later.Warning
STOP USING MD5 AS A PASSWORD. You should be using
password_hash()
Read Here.CLEAN THE USER DATA ENTERING YOUR DATABASE. Where does this JSON/File data come from? Because you’re absolutely not checking or cleaning it before it goes into your database, you are wide open to compromise. There are very good improvements available Read how to use them.
The display message is not a Error, just a PHP warning/notice. After PHP 8 whenever a array key doesn’t exist, it always display’s a NOTICE.
Knowing that i can assume that your variable
does not ‘get or has’ the specific key ‘aksi’.
You could try this :
Line 13:
$postjson variable does not have index ‘aksi’.
Example:
If you do not know if array contains index, you should first check if it exist:
More on isset() function
https://www.php.net/manual/en/function.isset.php