1

好的,我正在使用页数来计算用户访问 profile.php 的时间。如果他们没有登录,他们只能查看 5 个用户的个人资料。

每个配置文件都有一个 profile.php 的扩展名,例如 profile.php?id=1 或 id=2 等。

页数工作正常,基本上,如果用户尝试点击超过 5 个配置文件,它会将他们重定向到 limit.php 页面,该页面告诉用户他们已达到限制并登录以查看更多信息。

我已将其设置为排除下面列出的一些配置文件。

基本上问题出在哪里,如果用户单击排除的配置文件,例如 99999 或 99998,它不会将用户重定向到 limit.php 页面。这很好,因为我希望它排除这些配置文件,但是,如果用户单击另一个配置文件(例如 1 或 8 等)并且用户尝试重新访问 99999 或 99998,那么它不会让它们重定向它们吗?

谁能告诉我如何编辑脚本以仍然允许用户访问配置文件 99999 或 99998,即使在他们访问其他配置文件之后或期间他们访问其他配置文件而不重定向它们?

希望这很清楚。谢谢

 <?php 

    !session_id() ? session_start() : null;
    if(!isset($_SESSION['page_access_count'])){
        $_SESSION['page_access_count'] = 1;
    }elseif($_SESSION['page_access_count'] >= 6){
        // redirect to signup page
        header('Location: limit.php');
        exit;
    }

      $free_profiles = array(99999,99998,99997,99996,99995,99994,99993); // array of profile IDs to exclude

    if (! in_array($_GET['id'], $free_profiles)) {
      $_SESSION['page_access_count']++;
    }


        ?>
4

1 回答 1

1

我将限制验证逻辑包装在一个名为verify_profile_visit_limit. 现在我们可以使用(因为它是一个查询字符串)获取页面的“id”,并通过使用 function退出函数来$_GET跳过限制检查它是否在数组中 。$free_profileverify_profile_visit_limitreturn

!session_id() ? session_start() : null;

verify_profile_visit_limit();

function verify_profile_visit_limit(){
    $free_profiles = array(99999,99998,99997,99996,99995,99994,99993);

    if(in_array($_GET["id"], $free_profiles)) return;

    if(! isset($_SESSION["page_access_count"])){
        $_SESSION["page_access_count"] = 0;
    }

    $_SESSION["page_access_count"]++;

    if($_SESSION["page_access_count"] > 5){
        header("Location: limit.php");
        exit();
    }
}
于 2012-12-24T19:42:47.037 回答