I'm fairly new to php, but I have an "API" set up on my web server to send/receive data to a MySQL database.
For some reason, when I send an http post request to my register.php file (registration entry point), the $_POST
variable remains empty. However, after using phpconfig()
and doing some digging, I noticed the parameters I sent were stored in the $_REQUEST
variable, so I changed my register.php accordingly and it worked.
My questions are why that is happening and whether using $_REQUEST
is wrong.
I send the http request using angularjs' $http:
var link = 'http://www.temporaryurl.com/register.php'
$http.post(link, {'name': 'Adam'},{'email': 'awbasham@test.com'},{'password': 'testingPass!'}).then(function(response) {
console.log("Http success!");
console.log(response);
}, function(response) {
console.log("Http failure");
console.log(response);
});
Here's register.php
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_REQUEST['name']) && isset($_REQUEST['email']) && isset($_REQUEST['password'])) {
// receiving the post params
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
echo("$email is not a valid email address");
$response["error"] = TRUE;
$response["error_msg"] = "Email is invalid!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
?>