%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/login.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 Form Data + x-www-form-urlencoded + Raw JSON
|--------------------------------------------------------------------------
*/

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

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

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

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

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

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

    response(false, "Email or Username and Password are required", [

        "required_fields" => [
            "username_or_email",
            "password"
        ],

        "example_url" =>
            BASE_URL .
            "api/login.php?email=test@gmail.com&password=123456",

        "sample_request" => [
            "email" => "test@gmail.com",
            "password" => "123456"
        ],

        "raw_json_example" => [
            "email" => "test@gmail.com",
            "password" => "123456"
        ]
    ]);
}

/*
|--------------------------------------------------------------------------
| Check User
|--------------------------------------------------------------------------
*/

$stmt = $conn->prepare("
    SELECT *
    FROM user
    WHERE username = ?
    OR email = ?
");

$stmt->bind_param(
    "ss",
    $username,
    $username
);

$stmt->execute();

$result = $stmt->get_result();

if ($result->num_rows == 0) {

    response(false, "User not found", [

        "login_input" => $username,

        "register_api" =>
            BASE_URL . "api/register.php"
    ]);
}

$user = $result->fetch_assoc();

/*
|--------------------------------------------------------------------------
| Password Check
|--------------------------------------------------------------------------
*/

if ($user['password'] != $password) {

    response(false, "Invalid Password");
}

/*
|--------------------------------------------------------------------------
| Generate New 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();

/*
|--------------------------------------------------------------------------
| Login Success
|--------------------------------------------------------------------------
*/

response(true, "Login Successful", [

    "user" => [

        "id"       => $user['id'],
        "username" => $user['username'],
        "email"    => $user['email'],
        "phone"    => $user['phone'] ?? '',
        "usertype" => $user['usertype'] ?? '',

        "profile_image" =>
            !empty($user['profile_image'])
            ? $user['profile_image']
            : "",

        "token"    => $token
    ],

    "token_info" => [

        "send_token_in" => [
            "form-data",
            "raw-json",
            "header"
        ],

        "example" => [
            "token" => $token
        ]
    ],

    "next_api" => [

        "home_api" =>
            BASE_URL . "api/home.php",

        "save_address_api" =>
            BASE_URL . "api/save_address.php"
    ]
]);

$updateToken->close();
$stmt->close();
$conn->close();

?>

VaKeR 2022