基本上,它是一个登录表单。我希望它只显示登录表单,当用户会话存在时(即他登录时)。但我收到以下错误
Parse error: syntax error, unexpected $end in C:\xampp\htdocs\zabis\index.php on line 113
代码如下:
<?php
error_reporting(E_ALL ^ E_NOTICE);
define('INCLUDE_CHECK',true);
require 'connect.php';
require 'functions.php';
session_name('tzLogin');
// Starting the session
session_set_cookie_params(2*7*24*60*60);
// Making the cookie live for 2 weeks
session_start();
if($_SESSION['id'] && !isset($_COOKIE['tzRemember']) && !$_SESSION['rememberMe'])
{
// If you are logged in, but you don't have the tzRemember cookie (browser restart)
// and you have not checked the rememberMe checkbox:
$_SESSION = array();
session_destroy();
// Destroy the session
}
if(isset($_GET['logoff']))
{
$_SESSION = array();
session_destroy();
header("Location: demo.php");
exit;
}
if ($_POST['submit']=='login')
{
$err = array();
if(!$_POST['username'] || !$_POST['password'])
$err = 'username or password missing!';
if(!count($err))
{
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$rememberMe = (int)$_POST['rememberMe'];
$row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM members WHERE usr='{$username}' AND pass='".md5($password)."'"));
if($row['usr'])
{
$_SESSION['usr']=$row['usr'];
$_SESSION['id']=$row['id'];
$_SESSION['rememberMe']=$_POST['rememberMe'];
//setcookie('tzRemember',$_POST['rememberMe']);
if($rememberMe=="on")
setcookie('zsRememberMe',$username, time()+7200);
}
else
$err[] = "wrong username or password!";
}
if($err)
$_SESSION['msg']['login-err'] = implode('<br/>',$err);
header("Location: index.php");
exit;
}
else
die("Please enter a username and password.");
?>
<html>
<body>
<?php
if(!$_SESSION['id']):
?>
<div id="content">
<h1>Login Form</h1>
<form id="form1" name="form1" action="doLogin.php" method="post">
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" id="password" />
</p>
<p/>
<input type="checkbox" name="rememberMe"> Remember me
<p>
<input type="submit" id="login" name="login" />
</p>
</form>
<div id="message"></div>
</div>
</body>
</html>
答案 0 :(得分:3)
上次if
声明未结束。通过添加:
<?php endif; ?>
在代码的最后。
答案 1 :(得分:3)
更改
<?php
if(!$_SESSION['id']):
?>
<div id="content">
<h1>Login Form</h1>
<form id="form1" name="form1" action="doLogin.php" method="post">
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" id="password" />
</p>
<p/>
<input type="checkbox" name="rememberMe"> Remember me
<p>
<input type="submit" id="login" name="login" />
</p>
</form>
<div id="message"></div>
</div>
</body>
</html>
到
<?php
if(!$_SESSION['id']):
?>
<div id="content">
<h1>Login Form</h1>
<form id="form1" name="form1" action="doLogin.php" method="post">
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" id="password" />
</p>
<p/>
<input type="checkbox" name="rememberMe"> Remember me
<p>
<input type="submit" id="login" name="login" />
</p>
</form>
<div id="message"></div>
</div>
<?php endif; ?>
</body>
</html>
请注意<?php endif; ?>
。你忘了关闭最后的if语句