%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/www/website_6ca89795/api/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/www/website_6ca89795/api/register.php
<?php

header("Content-Type: application/json");

include("../dbcon.php");

define('BASE_URL', 'https://meatandgo.fr/');

function response($status, $message, $data = [])
{
    echo json_encode([
        "status" => $status,
        "message" => $message,
        "data" => $data
    ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

/*
|--------------------------------------------------------------------------
| Support JSON Body
|--------------------------------------------------------------------------
*/

$json = json_decode(file_get_contents("php://input"), true);

/*
|--------------------------------------------------------------------------
| Receive Data
|--------------------------------------------------------------------------
*/

$username = trim(
    $_REQUEST['username']
    ?? $json['username']
    ?? ''
);

$email = trim(
    $_REQUEST['email']
    ?? $json['email']
    ?? ''
);

$phone = trim(
    $_REQUEST['phone']
    ?? $json['phone']
    ?? ''
);

$password = trim(
    $_REQUEST['password']
    ?? $json['password']
    ?? ''
);

/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/

if (
    empty($username) ||
    empty($email) ||
    empty($phone) ||
    empty($password)
) {

    response(false, "All fields are required", [

        "required_fields" => [
            "username",
            "email",
            "phone",
            "password"
        ],

        "example_url" =>
        BASE_URL .
        "api/register.php?username=Siddharth&email=siddharth@gmail.com&phone=9876543210&password=123456",

        "sample_request" => [

            "username" => "Siddharth",
            "email" => "siddharth@gmail.com",
            "phone" => "9876543210",
            "password" => "123456"
        ],

        "postman_form_data" => [

            [
                "key" => "username",
                "value" => "Siddharth"
            ],
            [
                "key" => "email",
                "value" => "siddharth@gmail.com"
            ],
            [
                "key" => "phone",
                "value" => "9876543210"
            ],
            [
                "key" => "password",
                "value" => "123456"
            ]
        ],

        "raw_json_example" => [

            "username" => "Siddharth",
            "email" => "siddharth@gmail.com",
            "phone" => "9876543210",
            "password" => "123456"
        ],

        "received_data" => [
            "request" => $_REQUEST,
            "json" => $json
        ]
    ]);
}

/*
|--------------------------------------------------------------------------
| Email Check
|--------------------------------------------------------------------------
*/

$check = $conn->prepare(
    "SELECT id FROM user WHERE email=?"
);

$check->bind_param("s", $email);
$check->execute();

$result = $check->get_result();

if ($result->num_rows > 0) {

    response(false, "Email already exists", [

        "email" => $email,

        "suggestion" =>
        "Please use another email address"
    ]);
}

/*
|--------------------------------------------------------------------------
| Insert User
|--------------------------------------------------------------------------
*/

$created_date = date('Y-m-d');
$usertype = '';

$stmt = $conn->prepare(
"
INSERT INTO user
(
username,
email,
password,
phone,
usertype,
created_date
)
VALUES
(
?,
?,
?,
?,
?,
?
)
"
);

$stmt->bind_param(
    "ssssss",
    $username,
    $email,
    $password,
    $phone,
    $usertype,
    $created_date
);

if ($stmt->execute()) {

    $user_id = $conn->insert_id;

    /*
    |--------------------------------------------------------------------------
    | Generate Token
    |--------------------------------------------------------------------------
    */

    $token = bin2hex(random_bytes(32));

    $updateToken = $conn->prepare(
        "UPDATE user SET api_token=? WHERE id=?"
    );

    $updateToken->bind_param(
        "si",
        $token,
        $user_id
    );

    $updateToken->execute();

    response(true, "Registration Successful", [

        "user" => [

            "id" => $user_id,
            "username" => $username,
            "email" => $email,
            "phone" => $phone,
            "token" => $token
        ],

        "next_step" => [

            "Login API" =>
            BASE_URL . "api/login.php"
        ]
    ]);

} else {

    response(false, "Registration Failed", [

        "mysql_error" => $stmt->error
    ]);
}

$stmt->close();
$check->close();

if (isset($updateToken)) {
    $updateToken->close();
}

$conn->close();

?>

VaKeR 2022