PHP类变量问题
我有下面的类来处理我的用户登录/注销(我只在这里包括相关的内容)。我想将登录并访问login.php的用户重定向到那里的帐户页面。我用……来做这件事PHP类变量问题,php,Php,我有下面的类来处理我的用户登录/注销(我只在这里包括相关的内容)。我想将登录并访问login.php的用户重定向到那里的帐户页面。我用……来做这件事 $User = new User(); if ($User->loggedin = 'true') header('Location:MyAccountNEW.php'); 问题是,如果我将其切换为true或false,它将重定向到myaccountnew.php。。(虽然当条件为(2>3)时不会出现这种情况。当我回显$User l
$User = new User();
if ($User->loggedin = 'true') header('Location:MyAccountNEW.php');
问题是,如果我将其切换为true或false,它将重定向到myaccountnew.php。。(虽然当条件为(2>3)时不会出现这种情况。当我回显$User loggedin时,也不会出现任何问题。我有点不知所措
上课时间到了
Class User {
public $loggedin = false;
public $username = "";
public $ShopperID = "";
function __construct() {
$this->CheckLogin();
}
function CheckLogin() {
if (!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
$this->loggedin = true;
$this->username = $_SESSION['Username'];
}
else {
$this->loggedin = false;
}
}
下面是logout.php的样子
<?php include ("base.php");
include("userclass.php");
$User = new User();
$User->loggedin = false;
更换
if ($User->loggedin = 'true')
与
因为
if ($User->loggedin = 'true')
是一个赋值,将始终返回true
可能只是您的一个类型=]您使用的是一个相等(=)而不是两个(=)
此外,我强烈建议添加以下内容:
if ($User->loggedIn == 'true') {
header('location: somewhereelse.php');
die(); // <-- important!!
}
这是因为:
true == "true"
true == "foo"
true == "false"
除了空字符串或字符串“0”
之外的任何字符串值都被认为是真的。对我来说太快了:)3个答案同时键入我的回答。天哪,我不敢相信我错过了一个额外的等号lol。谢谢;)关于添加die()以防止继续处理页面,这一点非常好
if ($User->loggedIn == true)
// or even shorter:
if ($User->loggedIn)
true == "true"
true == "foo"
true == "false"