Add test_www.php
This commit is contained in:
76
test_www.php
Normal file
76
test_www.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
|
||||
$responseHeaders = '';
|
||||
$responseCode = 0;
|
||||
$url = '';
|
||||
$timeout = 5; // domyślny timeout
|
||||
|
||||
if (isset($_GET['url']) && filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
|
||||
$url = $_GET['url'];
|
||||
|
||||
// Pobierz i sprawdź timeout z formularza
|
||||
if (isset($_GET['timeout'])) {
|
||||
$t = intval($_GET['timeout']);
|
||||
if ($t > 0 && $t <= 30) { // ograniczenie timeout do 30s
|
||||
$timeout = $t;
|
||||
}
|
||||
}
|
||||
|
||||
$opts = [
|
||||
"http" => [
|
||||
"method" => "GET",
|
||||
"timeout" => $timeout,
|
||||
"header" => "User-Agent: PHP script\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>
|
||||
<input type="submit" value="Sprawdź">
|
||||
</form>
|
||||
|
||||
<?php if ($responseHeaders): ?>
|
||||
<h3>Odpowiedź serwera:</h3>
|
||||
<div><?php echo $responseHeaders; ?></div>
|
||||
<?php endif; ?>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user