SESSION 无法检查登录 [英] SESSION is not working to check login
问题描述
你能告诉我这有什么问题吗?我非常确定这段代码是完全正确的,但我不知道为什么它在 Dreamweaver 和 netbean 等 Web 应用程序中的第 2 页看起来是错误的.
could you tell me what's wrong with this? I'm very sure this code is totally correct, but I've no idea why it looks like wrong at page 2 in web application like dreamweaver and netbean.
1. session_start();
2. if (isset($_SESSION['username'] == 1) && isset ($_SESSION['username']) == 'admin') {
3. echo "<p> HELLO </p>";
4. }
而且我必须让你知道这段代码真的像这样工作.
And I have to let you know that this code is really working something like this.
1. session_start();
2. if (isset ($_SESSION['username']) == 'admin') {
3. echo "<p> HELLO </p>";
4. }
你能指出上面遗漏了什么吗?
Can you point it out what's missing above?
推荐答案
isset() 仅在设置了变量时返回 true
或 false
.因此,您实际上是在尝试比较 (true == 'admin')
或 (false == 'admin')
,这完全没有意义.
isset() only returns true
or false
if the variable was set. Therefore, you are actually trying to compare (true == 'admin')
or (false == 'admin')
, which makes no sense at all.
您首先需要确保设置了变量,然后将其值与 admin
进行比较.
What you need is first to make sure that the variable is set AND then compare its value to admin
.
您需要将逻辑更改为:
session_start();
if (isset($_SESSION['username']) && ($_SESSION['username'] === 'admin')) {
echo "<p> HELLO </p>";
}
其他的也一样.
这篇关于SESSION 无法检查登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!