duanmangxie7131 2012-12-24 19:33
浏览 32
已采纳

如果页面访问次数超过5次而忽略某些页面,则将访问者重定向到不同的URL。

Ok i'm using a page count to count the number of time profile.php is accessed by users. If they are not logged in they can only view 5 users profiles.

Each profile has an extension of profile.php e.g. profile.php?id=1 or id=2 etc.

The page count works fine, basically if a user tries to click on more than 5 profiles it will redirect them to the limit.php page which tells the user they have reached their limit an to login in to view more.

I have set it to exclude a few profiles as listed below.

basically what the problem is, if the user clicks on the excluded profiles such as 99999 or 99998 it wont redirect the user to the limit.php page. This is fine because i want it to exclude these profiles, however, if the user clicks on another profile such as 1 or 8 etc in between and the user tried to revisit 99999 or 99998 then it wont let them and redirects them?

Can anyone show me how i can edit the script to still allow users to access profiles 99999 or 99998 even after or during them visiting other profiles without it redirecting them?

Hope this is clear. Thanks

 <?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']++;
    }


        ?>
  • 写回答

1条回答 默认 最新

  • duanjupiao4730 2012-12-24 19:42
    关注

    I wrapped the limit verification logic in a function called verify_profile_visit_limit. Now we can get the "id" of the page using $_GET (as it's a query string) and skip the limit check if it was in the $free_profile array by exiting the verify_profile_visit_limit function using return function.

    !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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state