尝试在未登录的情况下创建页面重定向。我将尝试展示我的意思。PHP - 无法在“if”语句中更改会话变量
在index.php:
<?php
// initialization of login system and generation code
$oSimpleLoginSystem = new SimpleLoginSystem();
echo $oSimpleLoginSystem->getLoginBox();
session_start();
$_SESSION["loggedin"] = False;
// class SimpleLoginSystem
class SimpleLoginSystem {
// variables
var $aExistedMembers; // Existed members array
// constructor
function SimpleLoginSystem() {
$this->aExistedMembers = array(
'test' => MD5('test'), //Sample: MD5('qwerty')
);
}
function getLoginBox() {
ob_start();
require_once('login_form.html');
$sLoginForm = ob_get_clean();
$sLogoutForm = '<a href="'.$_SERVER['PHP_SELF'].'?logout=1">logout</a>';
if ((int)$_REQUEST['logout'] == 1) {
if (isset($_COOKIE['member_name']) && isset($_COOKIE['member_pass']))
$this->simple_logout();
}
if ($_REQUEST['username'] && $_REQUEST['password']) {
if ($this->check_login($_REQUEST['username'], MD5($_REQUEST['password'])))
{
$_SESSION["loggedin"] = True;
$this->simple_login($_REQUEST['username'], $_REQUEST['password']);
header('Location: /site.html');
}
else {
return 'Username or Password is incorrect' . $sLoginForm;
}
} else {
if ($_COOKIE['member_name'] && $_COOKIE['member_pass']) {
if ($this->check_login($_COOKIE['member_name'], $_COOKIE['member_pass'])) {
header('Location: /site.html');
}
}
return $sLoginForm;
}
}
在site.php:
<?php
session_start();
if ($_SESSION["loggedin"] == 0)
{
header('Location: http://test-weeabear.c9users.io/');
}
else
{
return "It worked!"
}
?>
为什么当我打开site.html,它重定向我回index.html的,甚至登录后?
注:我改变了一些东西,比如链接和文件名。为了隐私。
写在session_start()之前,如果site.php条件。 –
仍然将我重定向回。不知道什么是错的。 –
是你的header语句在实际代码中包含index.php的url吗?尝试打印$ _SESSION并退出,并检查您收到的内容。 –