php 输出链接判断有效期60秒 过期跳转其他页面
以下为 PHP 代码示例:
<?php
$url = 'http://www.example.com'; // 要验证的链接
$expire_time = 60; // 过期时间(秒)
// 发送 HTTP 请求并获取响应状态码
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 判断链接是否有效
if ($http_code === 200) {
// 链接有效,输出链接
echo '<a href="' . $url . '">点击跳转</a>';
} else {
// 链接无效,判断是否过期
$last_modified_time = filemtime($url); // 获取文件最后修改时间
$current_time = time(); // 获取当前时间
if (($current_time - $last_modified_time) > $expire_time) {
// 链接过期,跳转到其他页面
header('Location: http://www.otherpage.com');
exit;
} else {
// 链接未过期,重新验证链接有效性
// ...
}
}
?>
上述代码使用 curl 发送 HTTP 请求并获取响应状态码,判断链接是否有效。如果链接有效,直接输出链接;否则判断链接是否过期,如果过期则跳转到其他页面,否则重新验证链接的有效性。在判断链接是否过期时,使用 filemtime() 函数获取文件最后修改时间,与当前时间比较判断是否过期
原文地址: https://www.cveoy.top/t/topic/fDUb 著作权归作者所有。请勿转载和采集!