I am trying to build Simple Rest api for my php website but i am having issue on creating the account
create.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers,
Authorization, X-Requested-With");
include_once 'database.php';
include_once 'data.php';
$database = new Database();
$db = $database->getConnection();
$item = new emmanuel_api($db);
$item->user_name = $_GET['user_name'];
$item->user_pass = $_GET['user_pass'];
$item->user_email = $_GET['user_email'];
$item->full_name = $_GET['full_name'];
if($item->createUsers()){
echo 'Account created successfully.';
} else{
echo 'Account could not be created.';
}
?>
I think i am getting error on my Create.php please help me to resolve this issue
Also how i can convert this user_pass into MD5
Thanks
My Answer:
There are a few issues in your code that need to be addressed:
1. You are using $_GET to retrieve the user input data, but since you are creating an account, it is better to use $_POST instead. Change $_GET to $_POST in your code.
2. To convert the user_pass into MD5, you can use the md5() function in PHP. Update the line where you assign the user_pass value to md5($_POST['user_pass']) to convert it into MD5.
Here is the updated code:
php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once 'database.php';
include_once 'data.php';
$database = new Database();
$db = $database->getConnection();
$item = new emmanuel_api($db);
$item->user_name = $_POST['user_name'];
$item->user_pass = md5($_POST['user_pass']);
$item->user_email = $_POST['user_email'];
$item->full_name = $_POST['full_name'];
if($item->createUsers()){
echo 'Account created successfully.';
} else{
echo 'Account could not be created.';
}
?>
Make sure to also update your frontend code to send the data using POST method instead of GET.
Additionally, using MD5 for password hashing is not recommended as it is not secure. Consider using more secure hashing algorithms like bcrypt or Argon2 for password hashing.
Rate this post
5 of 5 based on 9458 votesComments