Files
narzedzia_www/test_www.php
2025-09-26 17:09:08 +02:00

112 lines
3.8 KiB
PHP

<?php
header('Content-Type: text/html; charset=utf-8');
$responseHeaders = '';
$responseCode = 0;
$url = '';
$timeout = 5;
$userAgent = '';
$customUserAgent = '';
$standardUserAgents = [
'Chrome' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
'Firefox' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:117.0) Gecko/20100101 Firefox/117.0',
'Safari' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15',
'Edge' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.0.0',
'curl' => 'curl/7.88.1'
];
if (isset($_GET['url']) && filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
$url = $_GET['url'];
if (isset($_GET['timeout'])) {
$t = intval($_GET['timeout']);
if ($t > 0 && $t <= 30) {
$timeout = $t;
}
}
if (isset($_GET['customUserAgent']) && trim($_GET['customUserAgent']) !== '') {
$customUserAgent = trim($_GET['customUserAgent']);
$userAgent = $customUserAgent;
} elseif (isset($_GET['userAgent']) && array_key_exists($_GET['userAgent'], $standardUserAgents)) {
$userAgent = $standardUserAgents[$_GET['userAgent']];
} else {
$userAgent = "PHP script"; // fallback
}
$opts = [
"http" => [
"method" => "GET",
"timeout" => $timeout,
"header" => "User-Agent: " . $userAgent . "\r\n"
]
];
$context = stream_context_create($opts);
error_clear_last();
$content = @file_get_contents($url, false, $context);
if ($content !== false) {
foreach ($http_response_header as $hdr) {
if (preg_match('|^HTTP/\d\.\d\s+(\d+)|', $hdr, $matches)) {
$responseCode = intval($matches[1]);
break;
}
}
if ($responseCode === 200) {
$responseHeaders = implode("<br>", $http_response_header);
} else {
$responseHeaders = "Otrzymano kod odpowiedzi HTTP: $responseCode";
}
} else {
$error = error_get_last();
$responseHeaders = "Błąd podczas pobierania strony: " . ($error ? htmlspecialchars($error['message']) : 'Nieznany błąd');
}
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Prosty request HTTP w PHP</title>
</head>
<body>
<h2>Sprawdź stronę HTTP</h2>
<form method="get" action="">
<label for="url">Adres URL:</label>
<input type="text" id="url" name="url" size="50"
value="<?php echo htmlspecialchars($url); ?>" required>
<br><br>
<label for="timeout">Timeout (1-30 sekund):</label>
<input type="number" id="timeout" name="timeout" min="1" max="30" value="<?php echo $timeout; ?>" required>
<br><br>
<label for="userAgent">User-Agent (wybierz z listy):</label>
<select id="userAgent" name="userAgent">
<?php foreach ($standardUserAgents as $key => $ua): ?>
<option value="<?php echo $key; ?>" <?php echo (isset($_GET['userAgent']) && $_GET['userAgent'] === $key) ? 'selected' : ''; ?>>
<?php echo $key; ?>
</option>
<?php endforeach; ?>
</select>
<br><br>
<label for="customUserAgent">Lub wpisz własny User-Agent:</label>
<input type="text" id="customUserAgent" name="customUserAgent" size="50"
value="<?php echo htmlspecialchars(isset($_GET['customUserAgent']) ? $_GET['customUserAgent'] : ''); ?>">
<br><br>
<input type="submit" value="Sprawdź">
</form>
<?php if ($responseHeaders): ?>
<h3>Odpowiedź serwera:</h3>
<div><?php echo $responseHeaders; ?></div>
<?php endif; ?>
</body>
</html>