我的代码有问题吗?它可以工作,但它不会重定向到我的索引.php它总是以表单所在的登录名.php结束。
<?php
include 'core/ini.php';
if (empty($_POST) === false) {
$username = $_POST ['username'];
$password = $_POST ['password'];
if (empty ($username) === true || empty ($password) === true ) {
$errors[] = 'You need to enter a username and password!';
} else if (user_exists($username) === false) {
$errors[] = 'We can''t find that username. Have you registered?';
} else if (user_active($username) === false) {
$errors[] = 'You haven''t activated your account! ';
} else {
$login = login($username, $password) ;
if ($login === false) {
$errors[] = 'That username/password combination is incorrect ';
} else {
$_SESSION['user_id'] = $login;
header('Location :index.php');
exit();
}
}
print_r($errors);
}
?>
thanks!
EDIT *
this is my login.php
<?php
include 'core/ini.php';
if (empty($_POST) === false) {
$username = $_POST ['username'];
$password = $_POST ['password'];
if (empty ($username) === true || empty ($password) === true ) {
$errors[] = 'You need to enter a username and password!';
} else if (user_exists($username) === false) {
$errors[] = 'We can''t find that username. Have you registered?';
} else if (user_active($username) === false) {
$errors[] = 'You haven''t activated your account! ';
} else {
$login = login($username, $password) ;
if ($login === false) {
$errors[] = 'That username/password combination is incorrect ';
} else {
$_SESSION['user_id'] = $login;
header('Location :index.php');
exit();
}
}
print_r($errors);
}
?>
这就是过程的进行。我不知道我应该把我的开始会话放在哪里,但我不知道为什么它可以在没有错误的情况下工作。
将header('Location :index.php');
更改为header('Location: index.php');
那个空间可能是原因。
我猜您错过了页面顶部的session_start();
,因为您正在存储会话。启动session_start();
。
另外,您的login()
函数是否返回 TRUE?回显某些内容以检查函数是否按预期返回 TRUE。
您需要在页面顶部使用session_start
,我认为您应该在标题定位后删除exit
。