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

2019-04-15 01:59发布

问题:

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

}