我正在尝试为我的网站制作注册和登录系统,当我点击登录时,我在登录文件中收到解析错误syntax error unexpected 'else'(T_ELSE)
可能是什么问题?
下面是包含代码的文件:
<?php
//include config
require_once('includes/config.php');
//check if already logged in move to home page
if($user->is_logged_in()){
header('Location: index.php');
}
}
else
{
echo "<a href=register.php>Register</a> <a href=login.php>Login</a>";
}
//process login form if submitted
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($user->login($username,$password)){
$_SESSION['username'] = $username;
header('Location: memberpage.php');
exit;
} else {
$error[] = 'Wrong username or password or your account has not been activated.';
}
}//end if submit
//define page title
$title = 'Login';
答案 0 :(得分:3)
在}
之后,两个花括号被关闭header('Location: index.php');
。把它做成一个并尝试。
答案 1 :(得分:3)
在其他地方删除}
:
if($user->is_logged_in()){
header('Location: index.php');
} else {
echo "<a href=register.php>Register</a> <a href=login.php>Login</a>";
}
答案 2 :(得分:0)
你有一个额外的大括号。
更改
//check if already logged in move to home page
if($user->is_logged_in()){ header('Location: index.php');
}
}
要
//check if already logged in move to home page
if($user->is_logged_in()){ header('Location: index.php');
}
答案 3 :(得分:0)
在你的第一个else声明之前,你有一个双重括号;拿出一个:
if($user->is_logged_in()){ header('Location: index.php');
}
//} <- this is the extra one that shouldnt be there
else
{
答案 4 :(得分:0)
这是正确的代码,u两个大括号已关闭}
//check if already logged in move to home page
if($user->is_logged_in()){ header('Location: index.php');
}
else
{
echo "<a href=register.php>Register</a> <a href=login.php>Login</a>";
}**strong text**
//process login form if submitted
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($user->login($username,$password)){
$_SESSION['username'] = $username;
header('Location: memberpage.php');
exit;
} else {
$error[] = 'Wrong username or password or your account has not been activated.';
}
}//end if submit
//define page title
$title = 'Login';
答案 5 :(得分:0)
很好地格式化你的代码,你的代码中有两个大括号。
//include config
require_once('includes/config.php');
//check if already logged in move to home page
if($user->is_logged_in()){
header('Location: index.php');
} else {
echo "<a href=register.php>Register</a> <a href=login.php>Login</a>";
}
//process login form if submitted
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($user->login($username,$password)){
$_SESSION['username'] = $username;
header('Location: memberpage.php');
exit;
} else {
$error[] = 'Wrong username or password or your account has not been activated.';
}
}//end if submit
//define page title
$title = 'Login';
答案 6 :(得分:0)
你在if循环中添加了额外的}。请替换以下代码。
//include config
require_once('includes/config.php');
//check if already logged in move to home page
if($user->is_logged_in()){
header('Location: index.php');
}
else
{
echo "<a href=register.php>Register</a> <a href=login.php>Login</a>";
}
答案 7 :(得分:0)
这里有一个额外的结束括号:
if($user->is_logged_in()){ header('Location: index.php');
}
}
....
这应该是:
if($user->is_logged_in())
{
header('Location: index.php');
}
else
{
echo "<a href=register.php>Register</a> <a href=login.php>Login</a>";
}
旁注:
我建议您始终使用正确的格式,这有助于您找出这些问题。