<p>完整代码:</p>
<p>style.css:</p>
<p>.progress {
border: 1px solid #ccc;
height: 20px;
margin-top: 10px;
}</p>
<p>.progress-bar {
background-color: #007bff;
color: #fff;
font-size: 14px;
height: 100%;
text-align: center;
}</p>
<p>index.php:</p>
<?php
function get_remaining_count($limit) {
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

    if (isset($_SESSION['count'])) {
        $_SESSION['count'] = intval($_SESSION['count']) + 1;
    } else {
        $_SESSION['count'] = 1;
    }

    $count = $limit - intval($_SESSION['count']);

    return $count;
}

function redirect($url) {
    header("Location: $url");
    exit;
}

$count = get_remaining_count(50);

if ($count <= 0) {
    redirect("https://example.com/vip");
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>限制访问次数</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>欢迎访问本页面</h1>
    <p>您还可以访问 <?= $count ?> 次</p>
<pre><code>&lt;div class=&quot;progress&quot;&gt;
    &lt;div class=&quot;progress-bar&quot; style=&quot;width: calc(&lt;?= $count / 50 * 100 ?&gt;%)&quot;&gt;
        &lt;?= $count ?&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;?php if ($count &lt;= 0): ?&gt;
    &lt;a href=&quot;https://example.com/vip&quot;&gt;开通 VIP 继续使用&lt;/a&gt;
&lt;?php endif; ?&gt;
</code></pre>
</body>
</html>

原文地址: https://www.cveoy.top/t/topic/1Rc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录