登录后显示警报

时间:2011-07-06 21:13:20

标签: php login

我有一个网站,你需要在登录后显示一个欢迎文本块。什么是在PHP中实现这一点的最佳方式,以便它只在登录时显示,而不是在你已经登录时显示正在访问该页面?

3 个答案:

答案 0 :(得分:2)

这是一个基本的会话示例:

// on page where accepting the login POST
session_start(); // initialize session.
// store that logged_in is true. You might want to put a username here instead.
$_SESSION['logged_in'] = TRUE; 
// store that they need to see the message still.
$_SESSION[ 'show_login' ] = TRUE;

然后:

// on all other pages at the top (before whitespace even)
session_start(); // you still have to initialize the session.

// other pages where actually outputting welcome text 
// is the user logged in?
if( isset( $_SESSION[ 'show_login' ] ) && $_SESSION[ 'show_login' ] )
{
    // gotta clean up our flags.
    $_SESSION[ 'show_login' ] = FALSE;
    // Then HAPPY DAY! We can say hello.
    echo "Hey! I know this guy.";
}
else
{
    // we are actively hostile to those who are not logged in...
    // you may want to be more cordial.
    // sometimes it is so obvious that I come from NJ...
    echo "I've never seen you before in my life. He smells";    
}

答案 1 :(得分:1)

利用$_SESSION并保存到那里。

答案 2 :(得分:1)

在您的登录脚本中,向用户的会话添加一个标志,例如$_SESSION['justloggedin'] = true;。然后,假设您在所有页面上都包含一个公共文件,只需检查该标志是否存在:

if ($_SESSION['justloggedin'] === true) {
   ... display welcome message
   $_SESSION['justloggedin'] = false;
}

这样,他们登录,他们被重定向,无论他们登陆哪个页面,都会显示欢迎信息。之后,每个后续页面都不会显示该消息,因为您在显示消息时清除了“显示消息”标志。