Send the value js to php

46 Views Asked by At
const fpPromise = import('https://fpjscdn.net/v3/sample')
  .then(FingerprintJS => FingerprintJS.load());

// Analyze the visitor when necessary.
fpPromise
  .then(fp => fp.get())
  .then(result => {
    console.log(result.requestId, result.visitorId);
    var visitorId = result.visitorId;
    console.log(visitorId);
    // Call the function to send the value to PHP
    send_value_php(visitorId);
  })
  .catch(error => console.error(error));

function send_value_php(visitorId) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      console.log(xmlhttp.responseText); // Correct console.log position
    }
  };
  var url = "./test.php";
  xmlhttp.open("POST", url, true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  // Construct the parameter string
  var params = "userID=" + encodeURIComponent(visitorId);

  // Send the AJAX request with the parameters
  xmlhttp.send(params);
}

the above code for send the value js to php using ajax and The XMLHttpRequest Object

public static function Authentication($user, $pass)
    {
        $username = User::login($user, $pass);
        if ($username) {
            $userid = new User($user);
            $conn = Database::getConnection();
            $ip = $_SERVER['REMOTE_ADDR'];
            $user_agent = $_SERVER['HTTP_USER_AGENT'];
            $token = md5(rand(0, 999999) . $ip . $user_agent . time());

            // Check if the 'userID' is set in the POST data
            // Check if the 'userID' parameter is set in the POST request
            error_log(print_r($_POST, true));

            if (isset($_POST['userID'])) {
                // Retrieve the value of 'userID'
                $userId = $_POST['userID'];

                // Now you can use $userId variable in your PHP script
                echo "Received userID: " . $userId;

                // Here, you can perform any additional processing with the received userID,
                // such as storing it in a database or using it for further operations.
            } else {
                // If 'userID' parameter is not set in the POST request
                echo "No userID received";
            }
        }
    }


the above code is used to get the post passing value in php.

i try to send the visitorid to php for authentication. The XMLHttpRequest Object and ajax was alert the message success. but the php have not get the value i also use the browser tool to debug. but the tool have not identify any issue work normal. but php have not get the $_POST value in the php.

[Thu Mar 14 04:57:08.573425 2024] [php:warn] [pid 1500] [client 172.30.1.154:52409] PHP Warning:  Undefined variable $fingerPrint_userId
0

There are 0 best solutions below