Complete Proxy Checker with PHP Backend

Complete Proxy Checker with PHP Backend

1. HTML Form (Updated to Submit to PHP)

Replace the <form> in the template to point to a PHP script (check_proxy.php).

html
<form id="proxyCheckerForm" method="POST" action="check_proxy.php"> <label for="proxyAddress">Proxy Address</label> <input type="text" id="proxyAddress" name="proxyAddress" placeholder="e.g., 192.168.1.1" required> <label for="proxyPort">Proxy Port</label> <input type="number" id="proxyPort" name="proxyPort" placeholder="e.g., 8080" required> <button type="submit">Check Proxy</button> </form>

2. PHP Script (check_proxy.php)

This script receives the proxy details, validates them, and checks the proxy by attempting a connection.

php
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $proxyAddress = filter_input(INPUT_POST, 'proxyAddress', FILTER_VALIDATE_IP); $proxyPort = filter_input(INPUT_POST, 'proxyPort', FILTER_VALIDATE_INT, [ 'options' => ['min_range' => 1, 'max_range' => 65535] ]); if (!$proxyAddress || !$proxyPort) { echo json_encode([ 'success' => false, 'message' => 'Invalid proxy address or port.' ]); exit; } // Function to check proxy function checkProxy($address, $port) { $timeout = 5; // Timeout in seconds $connection = @fsockopen($address, $port, $errno, $errstr, $timeout); if ($connection) { fclose($connection); return true; // Proxy is working } return false; // Proxy failed } // Check the proxy $isProxyValid = checkProxy($proxyAddress, $proxyPort); // Return the result if ($isProxyValid) { echo json_encode([ 'success' => true, 'message' => "Proxy $proxyAddress:$proxyPort is valid." ]); } else { echo json_encode([ 'success' => false, 'message' => "Proxy $proxyAddress:$proxyPort is invalid." ]); } } else { echo json_encode([ 'success' => false, 'message' => 'Invalid request method.' ]); } ?>

3. AJAX Integration (Optional)

If you prefer not to reload the page, you can use AJAX to handle the form submission and display the response.

javascript
const form = document.getElementById("proxyCheckerForm"); const resultDiv = document.getElementById("result"); form.addEventListener("submit", async function (event) { event.preventDefault(); const formData = new FormData(form); const response = await fetch("check_proxy.php", { method: "POST", body: formData, }); const result = await response.json(); resultDiv.style.display = "block"; resultDiv.textContent = result.message; resultDiv.style.color = result.success ? "green" : "red"; });

How It Works:

  1. HTML Form: User inputs proxy details (IP and port).
  2. Backend (PHP):
    • Validates the inputs (IP and port).
    • Attempts to connect to the proxy using fsockopen().
    • Returns a JSON response with the result.
  3. Frontend (Optional AJAX): Displays the result dynamically without reloading.

This implementation allows for real-time or page-reload proxy validation. Let me know if you'd like more customization!

Post a Comment

أحدث أقدم