how do I show a php page just once only per user

谁说胖子不能爱 提交于 2019-12-06 14:45:50

问题


Hi I have a php page that I want show it just one time per user.

I think this just might be possible with cookies,session-timeout or session-cookies.

But I'm not sure.

Thanks for your kindness :)


回答1:


you answered your own question - by setting a cookie.

// check if they've been here, if they haven't set
// a cookie for subsequent visits
if($_COOKIE['beenhere']) { 
    setcookie("beenhere", '1');
}
else {
    // where you want them to go if they've seen this page
    header('Location: http://www.example.com/');

For more information:

  • http://php.net/setcookie
  • http://php.net/header

If you want a single user to -never- see the page again, you must set an expiration for the cookie (see linked page above) as closing the browser will eliminate the cookie as I've set it above.




回答2:


To show a page once per-user-session you can try the following

//mypage.php

if(!isset($_SESSION['mypage_view'])
{
     $_SESSION['mypage_view'] = 1;   
} else {
     //check if this is not the first time the page has been viewed
     if(isset($_SESSION['mypage_view'])) {
      //not first time redirect
      header('location: google.com');
      session_write_close();
      exit();
     }
}



回答3:


You may also use sessions

if($_SESSION['sessioned_here'] == null) {
    // just been on this page
} else {
    // visited already. get out

}


来源:https://stackoverflow.com/questions/5367357/how-do-i-show-a-php-page-just-once-only-per-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!