What's new

PHP Question for bot

jay41

Forum Veteran
Joined
Nov 13, 2016
Posts
1,927
Reaction
472
Points
658
Age
24
Please fix this code, the error are Undefined index: when the otp_request_count.json is empty.

<?php
$file = 'otp_request_count.json';
$data = file_get_contents($file);
$requestData = json_decode($data, true);
$ip = $_SERVER['REMOTE_ADDR'];
$username = 'username'; // Replace with your actual username variable
if (time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
unset($requestData['requests'][$ip]);
}

if ($requestData['requests'][$ip][$username]['count'] < 3 || time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
// Allow OTP request
$requestData['requests'][$ip][$username]['count']++;
$requestData['requests'][$ip][$username]['last_request_time'] = time();
// Generate and send OTP
// ...

// Write the updated request data back to the JSON file
$data = json_encode($requestData);
file_put_contents($file, $data);
echo "Success";
} else {
// Display error message
$data = (time() - $requestData['requests'][$ip][$username]['last_request_time']);
$time = (60 - $data);
echo "Maximum OTP request limit exceeded. Please try again in ".$time." second(s).";
}
?>
 
The "Undefined index" error occurs because the code is trying to access indexes in the $requestData array that don't exist when the otp_request_count.json file is empty. To fix this issue, you can check if the necessary indexes exist before accessing them. Here's the updated code:

PHP:
<?php
$file = 'otp_request_count.json';
$data = file_get_contents($file);
$requestData = json_decode($data, true);

$ip = $_SERVER['REMOTE_ADDR'];
$username = 'username'; // Replace with your actual username variable

// Check if the necessary indexes exist before accessing them
if (isset($requestData['requests'][$ip][$username]['last_request_time']) && isset($requestData['requests'][$ip][$username]['count'])) {
    if (time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        unset($requestData['requests'][$ip]);
    }
     
    if ($requestData['requests'][$ip][$username]['count'] < 3 || time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        // Allow OTP request
        $requestData['requests'][$ip][$username]['count']++;
        $requestData['requests'][$ip][$username]['last_request_time'] = time();
        // Generate and send OTP
        // ...

        // Write the updated request data back to the JSON file
        $data = json_encode($requestData);
        file_put_contents($file, $data);
        echo "Success";
    } else {
        // Display error message
        $data  = (time() - $requestData['requests'][$ip][$username]['last_request_time']); 
        $time = (60 - $data);
        echo "Maximum OTP request limit exceeded. Please try again in ".$time." second(s).";
    }
} else {
    // Handle the case when the indexes are not found
    echo "Error: Indexes not found in the JSON file.";
}
?>

By using the isset() function, we check if the indexes 'last_request_time' and 'count' exist in the $requestData array before accessing them. If any of these indexes are not found, the code will display an error message.
 
The "Undefined index" error occurs because the code is trying to access indexes in the $requestData array that don't exist when the otp_request_count.json file is empty. To fix this issue, you can check if the necessary indexes exist before accessing them. Here's the updated code:

PHP:
<?php
$file = 'otp_request_count.json';
$data = file_get_contents($file);
$requestData = json_decode($data, true);

$ip = $_SERVER['REMOTE_ADDR'];
$username = 'username'; // Replace with your actual username variable

// Check if the necessary indexes exist before accessing them
if (isset($requestData['requests'][$ip][$username]['last_request_time']) && isset($requestData['requests'][$ip][$username]['count'])) {
    if (time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        unset($requestData['requests'][$ip]);
    }
    
    if ($requestData['requests'][$ip][$username]['count'] < 3 || time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        // Allow OTP request
        $requestData['requests'][$ip][$username]['count']++;
        $requestData['requests'][$ip][$username]['last_request_time'] = time();
        // Generate and send OTP
        // ...

        // Write the updated request data back to the JSON file
        $data = json_encode($requestData);
        file_put_contents($file, $data);
        echo "Success";
    } else {
        // Display error message
        $data  = (time() - $requestData['requests'][$ip][$username]['last_request_time']);
        $time = (60 - $data);
        echo "Maximum OTP request limit exceeded. Please try again in ".$time." second(s).";
    }
} else {
    // Handle the case when the indexes are not found
    echo "Error: Indexes not found in the JSON file.";
}
?>

By using the isset() function, we check if the indexes 'last_request_time' and 'count' exist in the $requestData array before accessing them. If any of these indexes are not found, the code will display an error message.
Please update the code when Indexes not found in the JSON file, It will create an ip, username, count, and last_request_time.
 
Please update the code when Indexes not found in the JSON file, It will create an ip, username, count, and last_request_time.
To handle the case when the indexes are not found in the JSON file, you can modify the code to create the necessary indexes with default values. Here's the updated code:

PHP:
<?php
$file = 'otp_request_count.json';
$data = file_get_contents($file);
$requestData = json_decode($data, true);

$ip = $_SERVER['REMOTE_ADDR'];
$username = 'username'; // Replace with your actual username variable

// Check if the necessary indexes exist before accessing them
if (isset($requestData['requests'][$ip][$username]['last_request_time']) && isset($requestData['requests'][$ip][$username]['count'])) {
    if (time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        unset($requestData['requests'][$ip]);
    }
    
    if ($requestData['requests'][$ip][$username]['count'] < 3 || time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        // Allow OTP request
        $requestData['requests'][$ip][$username]['count']++;
        $requestData['requests'][$ip][$username]['last_request_time'] = time();
        // Generate and send OTP
        // ...

        // Write the updated request data back to the JSON file
        $data = json_encode($requestData);
        file_put_contents($file, $data);
        echo "Success";
    } else {
        // Display error message
        $data  = (time() - $requestData['requests'][$ip][$username]['last_request_time']);
        $time = (60 - $data);
        echo "Maximum OTP request limit exceeded. Please try again in ".$time." second(s).";
    }
} else {
    // Create the necessary indexes with default values
    $requestData['requests'][$ip][$username]['count'] = 1;
    $requestData['requests'][$ip][$username]['last_request_time'] = time();
    
    // Write the updated request data back to the JSON file
    $data = json_encode($requestData);
    file_put_contents($file, $data);
    
    echo "Success";
}
?>

In the else block where the indexes are not found, we create the necessary indexes $requestData['requests'][$ip][$username]['count'] and $requestData['requests'][$ip][$username]['last_request_time'] with default values. Then, we write the updated request data back to the JSON file. Finally, we display the "Success" message to indicate that the OTP request was allowed.
 
To handle the case when the indexes are not found in the JSON file, you can modify the code to create the necessary indexes with default values. Here's the updated code:

PHP:
<?php
$file = 'otp_request_count.json';
$data = file_get_contents($file);
$requestData = json_decode($data, true);

$ip = $_SERVER['REMOTE_ADDR'];
$username = 'username'; // Replace with your actual username variable

// Check if the necessary indexes exist before accessing them
if (isset($requestData['requests'][$ip][$username]['last_request_time']) && isset($requestData['requests'][$ip][$username]['count'])) {
    if (time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        unset($requestData['requests'][$ip]);
    }
  
    if ($requestData['requests'][$ip][$username]['count'] < 3 || time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        // Allow OTP request
        $requestData['requests'][$ip][$username]['count']++;
        $requestData['requests'][$ip][$username]['last_request_time'] = time();
        // Generate and send OTP
        // ...

        // Write the updated request data back to the JSON file
        $data = json_encode($requestData);
        file_put_contents($file, $data);
        echo "Success";
    } else {
        // Display error message
        $data  = (time() - $requestData['requests'][$ip][$username]['last_request_time']);
        $time = (60 - $data);
        echo "Maximum OTP request limit exceeded. Please try again in ".$time." second(s).";
    }
} else {
    // Create the necessary indexes with default values
    $requestData['requests'][$ip][$username]['count'] = 1;
    $requestData['requests'][$ip][$username]['last_request_time'] = time();
  
    // Write the updated request data back to the JSON file
    $data = json_encode($requestData);
    file_put_contents($file, $data);
  
    echo "Success";
}
?>

In the else block where the indexes are not found, we create the necessary indexes $requestData['requests'][$ip][$username]['count'] and $requestData['requests'][$ip][$username]['last_request_time'] with default values. Then, we write the updated request data back to the JSON file. Finally, we display the "Success" message to indicate that the OTP request was allowed.
Please add a code that can remove the entry in the json file if over 1 minute old
 
Please add a code that can remove the entry in the json file if over 1 minute old
To remove entries in the JSON file that are over 1 minute old, you can iterate through the array and check the timestamp for each entry. If the entry is older than 1 minute, you can remove it using the unset() function. Here's the updated code:

PHP:
$file = 'otp_request_count.json';
$data = file_get_contents($file);
$requestData = json_decode($data, true);

$ip = $_SERVER['REMOTE_ADDR'];
$username = 'username'; // Replace with your actual username variable

// Check if the necessary indexes exist before accessing them
if (isset($requestData['requests'][$ip][$username]['last_request_time']) && isset($requestData['requests'][$ip][$username]['count'])) {
    // Remove entries older than 1 minute
    foreach ($requestData['requests'] as $requestIp => &$userData) {
        foreach ($userData as $user => &$requestData) {
            if (time() - $requestData['last_request_time'] > 60) {
                unset($requestData);
            }
        }
        // Remove empty IP entries
        if (empty($userData)) {
            unset($requestData['requests'][$requestIp]);
        }
    }

    if ($requestData['requests'][$ip][$username]['count'] < 3 || time() - $requestData['requests'][$ip][$username]['last_request_time'] > 60) {
        // Allow OTP request
        $requestData['requests'][$ip][$username]['count']++;
        $requestData['requests'][$ip][$username]['last_request_time'] = time();
        // Generate and send OTP
        // ...

        // Write the updated request data back to the JSON file
        $data = json_encode($requestData);
        file_put_contents($file, $data);
        echo "Success";
    } else {
        // Display error message
        $data  = (time() - $requestData['requests'][$ip][$username]['last_request_time']);
        $time = (60 - $data);
        echo "Maximum OTP request limit exceeded. Please try again in ".$time." second(s).";
    }
} else {
    // Create the necessary indexes with default values
    $requestData['requests'][$ip][$username]['count'] = 1;
    $requestData['requests'][$ip][$username]['last_request_time'] = time();
  
    // Write the updated request data back to the JSON file
    $data = json_encode($requestData);
    file_put_contents($file, $data);
  
    echo "Success";
}

In the code above, after checking if the necessary indexes exist, a foreach loop is used to iterate through the $requestData['requests'] array. For each entry, it checks if the last request time is older than 1 minute. If it is, the entry is removed using unset(). After removing the old entries, it also checks if there are any IP entries left that have no associated user data, and removes those as well.

After removing the old entries, the code continues with the rest of the logic to allow or deny the OTP request and update the JSON file accordingly.
 

Similar threads

Back
Top