php 当有人登录时显示注销按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15640635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a logout button show up when someone is logged in?
提问by Tyler
Is there a way to make a logout button appear on all pages of my website when a user is logged in and not there if someone isn't logged in? I'm assuming some sort of php would do the trick, but I'm not sure. I'm sort of new to all of this so if you could give a detailed explanation it would be greatly appreciated!
有没有办法在用户登录时在我网站的所有页面上显示注销按钮,如果有人未登录则不显示?我假设某种 php 可以解决问题,但我不确定。我对这一切都很陌生,所以如果你能给出详细的解释,将不胜感激!
Thank you in advance!
先感谢您!
采纳答案by Natwar Singh
you can do it by starting new session and setup a session variable.
您可以通过启动新会话并设置会话变量来实现。
at login file do it like
在登录文件中这样做
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
$username = trim(htmlentities(mysql_real_escape_string($_POST['username'])));
$password = trim(htmlentities(mysql_real_escape_string($_POST['password'])));
if (!empty($username) && !empty($password)) {
$_SESSION['username'] = $username;
echo "<br/> welcome ", $username;
echo "<br/><a href='logout.php'>Logout</a>";
} else {
echo "Please enter correct username or password";
}
} else {
echo "Go to login page</br>";
echo "<a href='login.php'>Login</a>";
}
and every page check for that session variable
和每个页面检查该会话变量
<?php
session_start();
if(isset($_SESSION['username'])){
echo "you logged in as </br>", $_SESSION['username'];
echo "<br/><a href='logout.php'>logout</a>";
}else{
//your page stuff
}
and to logout, destroy the session
并注销,销毁会话
<?php
session_start();
if (isset($_SESSION['username'])) {
session_destroy();
echo "<br> you are logged out successufuly!";
}
echo "<br/><a href='login.php'>login</a>";
?>
//don't forget to read more about session and cookies.
//不要忘记阅读有关会话和cookie的更多信息。
回答by Charaf JRA
Create $_SESSION['user'] and fill it when user is loged in.
create a php page that you will include in all your pages ,in this page check if a session variable $_SESSION['user'] is set or not,and show the link according to this condition
if(isset($_SESSION["user"])){ /*your logout button*/ } else { /*your login button*/ }
创建 $_SESSION['user'] 并在用户登录时填写。
创建一个将包含在所有页面中的 php 页面,在此页面中检查是否设置了会话变量 $_SESSION['user'],并根据此条件显示链接
if(isset($_SESSION["user"])){ /*your logout button*/ } else { /*your login button*/ }
回答by spullen
Have you looked into session management? http://www.php.net/manual/en/book.session.php
回答by SteeveDroz
This is an example, there are other ways:
这是一个例子,还有其他方法:
When the user logs in, create a $_SESSION['user']
variable.
当用户登录时,创建一个$_SESSION['user']
变量。
When you want to show the logout button, check if the session variable is set or not with isset($_SESSION['user'])
.
当您想显示注销按钮时,请检查会话变量是否设置为isset($_SESSION['user'])
。
回答by Hackerman
Like this:
像这样:
if (isset($_SESSION['uid']))
{
if (!empty($_SESSION['uid']))
{
echo "<input type='button' value='Logout' onclick='logout_function();' />";
}
}
Saludos xD
萨鲁多斯 xD
回答by inzenir
Something like this should do the trick.
像这样的事情应该可以解决问题。
if(!empty($_SESSION["user"]))
{
/*
* logout button
*/
}
else
{
/*
* login code
*/
}