how can i make a php function to continue to run with the while loop in background?

83 views Asked by At

how can i make the otpChange() function to run in background and Immediately return the response and basically not to wait for otpChange() function to end(run it in background). i thought of running it in the background or use return; and still run somehow but if you have any solution for that please write.

<?php
include 'db.php';
// Check if the request method is POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Check if action is provided
    if (isset($_POST["action"])) {
        // Handle the action
        handleAction($_POST["action"]);
    } else {
        // Return an error response if action is not provided
        $response = array(
            "re" => 1,
            "re_err" => "Action not provided",
            "re_err_num" => 1003
        );
        header('Content-Type: application/json');
        echo json_encode($response);
    }
} else {
    // Handle non-POST requests
    http_response_code(405); // Method Not Allowed
    echo "Method Not Allowed";
}

$phone = '';
$otp = '';
function login($conn) {
    //return array("message" => "Function 1 called");
    global $phone, $otp;
    $phone = $_POST['phone'];
    $sql = "";
    
    $result = $conn->query($sql);
    $otp = rand(100000, 999999);
    // return;
    //$response['re_data'][] = "No specific action performed";
    return;
}
function otpChange($conn, $phone, $otp) {
    $counter = 60;
    // $conn->query($setOtpExpiry);
    while ($counter >= 0) {
        $update_otp = "";
        $conn->query($update_otp);
        sleep(1);
        $counter--;
    }
    $response['re_data'][] = "No specific action performed";
    // return;
}

// login_check();
function handleAction($action) {
    // Initialize response variables
    $response = array(
        "re" => 0,
        "re_data" => array(),
        "re_err" => "",
        "re_err_num" => 0
    );

    // Check for specific actions
    if ($action == 'login') {
        global $conn, $phone, $otp;
        login($conn);
        $response['re_data'][] = "Login Action";
        otpChange($conn, $phone, $otp); //i dont want that loop to interfere with the login action, i want that when i call the login action it will Immediately $response['re_data'][] = "Login Action"; and not wait for the loop
    } elseif ($action == 'login_check') {
        // login_check();
    } else {
        // Implement your condition here
        // For example, check if any data has changed
        $dataChanged = false; // Assume no change initially

        // If data has changed, modify the response accordingly
        if ($dataChanged) {
            // Respond with custom data
            $response["re"] = 1;
            $response["re_err"] = "Data has changed";
            $response["re_err_num"] = 1001;
        } else {
            // Respond with default data
            $response['re_data'][] = "No specific action performed";
        }
    }

    // Return the response as JSON with ordered keys
    header('Content-Type: application/json');
    echo json_encode($response);
}
?>
0

There are 0 answers