How to add visited pages urls into a session array?

前端 未结 3 742
北荒
北荒 2021-01-23 07:05

Everytime the user visit a page, the page url will be stored into an array session. I want to have 10 elements in the array only. So that 10 elements will save 10 latest visited

3条回答
  •  渐次进展
    2021-01-23 07:54

    You are always overwriting the array with a new one here:

    $urlarray=array();       // new empty array
    $urlarray[] = $currentpageurl;    
    $_SESSION['pageurl']=$urlarray;
    

    Instead use:

    session_start();
    // like @Kwpolska said, you probably miss that, so $_SESSION didnt work
    
    is_array($_SESSION["pageurl"]) or $_SESSION["pageurl"] = array();
    // fix for your current problem
    
    $_SESSION['pageurl'][] = $currentpageurl;
    // This appends it right onto an array.
    
    $_SESSION["pageurl"] = array_slice($_SESSION["pageurl"], -10);
    // to cut it down to the last 10 elements
    

提交回复
热议问题