$apiUrl = 'https://fleetpoint.org/tool/analyse/loggedin.php'; $response =…
- User
response = file_get_contents($apiUrl);
- Assistant
Based on the code snippet provided, it appears that you are checking if the response from the API is successful and then parsing the response as JSON. If the "message" value in the JSON response is set to "USERLOGGEDIN", you are not performing any actions and the user can access the site.
If the "message" value is not set to "USERLOGGEDIN" or there is an error in fetching the API response, you are redirecting the user to the login page.
Here are a few things you can check to troubleshoot the issue:
-
Ensure that the API URL is correct and accessible. You can try opening the URL directly in a browser or use tools like cURL or Postman to send a request and verify the response.
-
Check if any error is occurring during the file_get_contents() function call. You can enable error reporting to display any potential errors by adding the following code before the file_get_contents() function call:
ini_set('display_errors', 1); error_reporting(E_ALL); -
- User
can you edit my php and use Curl for the api request
- Assistant
Certainly! Here's an updated version of your code that uses cURL to make the API request:
$apiUrl = 'https://fleetpoint.org/tool/analyse/loggedin.php'; $curl = curl_init($apiUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); curl_close($curl); if ($response !== false) { $json = json_decode($response, true); if (isset($json['message']) && $json['message'] === 'USERLOGGEDIN') { // User is logged in. Continue with site access. } else { // User is not logged in header('Location: https://fleetpoint.org/login.php'); exit(); } } else { echo "API Error"; }In this updated code, we initialize a cURL handle using
curl_init()and set theCURLOPT_RETURNTRANSFERoption to true. This ensures that the response is returned as a string rather than being directly outputted. The API response is then stored in the$responsevariable.After that, we close the cURL handle with
curl_close().The remaining logic for checking the API response and redirecting the user to the login page remains the same as in your original code.