简体   繁体   中英

count page hits and redirect?

I have this code which i'm trying to use to count the number of hits a page has had before redirecting the user to another page.

The idea is that non-logged in users can only visit profile.php 6 times before being redirected to a signup page, but it is also doing this for logged in users and i want the logged in users to be able to access profile.php as many times as they want.

Can someone please show me where i am going wrong.

so an example is if session is null then limit page access to 6 times, but if session = logged in then allow unlimited access.

<?
!session_id() ? session_start() : null;

if(!isset($_SESSION['logged_in']) && empty($_SESSION['logged_in'])){
    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"] = 1;
    }

    $_SESSION["page_access_count"]++;

    if($_SESSION["page_access_count"] > 6){
        header("Location: limit.php");
        exit();
    }
}

?>

The problem lies here:

if(!isset($_SESSION['logged_in']) && empty($_SESSION['logged_in']))

$_SESSION['logged_in'] can never be not set AND empty . You need to use the OR operator here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM