我刚刚开始编写php代码。我想在同一页面上进行用户登录和注册表单。这两个代码都可以单独工作,但是在将它们放在一起时我遇到了问题。有人可以向我解释我哪里出错了吗?
我遇到的两个问题是:
1)当我尝试登录时,它会提示错误未定义的变量电子邮件(但不是在我注册时)。
2) 在他们注册或登录后,它不会将我重定向到 index.php。
头文件。
<?php
session_start();
include 'login.php';
include 'signup.php';
if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
$loggedin = TRUE;
}
else {
$loggedin = FALSE;
}
if ($loggedin)
{
//shows a bunch of code
}
else
{
echo "<div class='header'>" .
"<div class='logo'>" .
"<a href='login.php' " . "style='color: #FFF; text-decoration: none;'>$appname</a>" .
"</div> ";
echo "<div id='login'>
<fieldset>
<legend style='color:white;'>Login</legend>
<form method='post' action='login.php'>$error".
"<div class='row'>
Username:
<span class='form'>
<input type='text' maxlength='16' name='user' value='$user' />
</span>
</div> ".
"<div class='row'>
Password: 
<span class='form'>
<input type='password' maxlength='16' name='pass' value='$pass' />
</span>
</div>".
"<div class='row'>
<span class='form'>
<input type='submit' value='Login' />
</span>
</div>
</form>
</fieldset>
</div>
</div>";
echo "<div class='container'>
<form method='post' action='signup.php'>$error".
"<div class='row_signup'>
<span class='form_signup'>
<h3>Sign Up Today!</h3>
</span>
</div> ".
"<div class='row_signup'>
<span class='label_signup'>Username:</span>
<span class='form_signup'>
<input type='text' maxlength='16' name='user' value='$user' onBlur='checkUser(this)'/><br /><span id='info'></span>
</span>
</div> ".
"<div class='row_signup'>
<span class='label_signup'>Password: </span>
<span class='form_signup'>
<input type='password' maxlength='16' name='pass' value='$pass' />
</span>
</div>".
"<div class='row_signup'>
<span class='label_signup'>Email: </span>
<span class='form_signup'>
<input type='email' name='email' value='$email' />
</span>
</div>".
"<div class='row_signup'>
<span class='form_signup'>
<input type='submit' value='Sign Up' />
</span>
</div> ".
"</form>
</div>";
}
?>
登录.php
<?php
include_once 'header.php';
$error = $user = $pass = "";
if (isset($_POST['user']))
{
$user = sanitizeString($_POST['user']);
$pass = sanitizeString($_POST['pass']);
if ($user == "" || $pass == "")
{
$error = "Not all fields were entered<br />";
}
else
{
$query = "SELECT user,pass FROM members
WHERE user='$user' AND pass='$pass'";
if (mysql_num_rows(queryMysql($query)) == 0)
{
$error = "<span class='error'>Username/Password
invalid</span><br /><br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
header("Location: index.php"); // redirect
}
}
}
?>
注册.php
<?php // Example 21-5: signup.php
include_once 'header.php';
$error = $user = $pass = $email = "";
if (isset($_SESSION['user'])) destroySession();
if (isset($_POST['user']))
{
$user = sanitizeString($_POST['user']);
$pass = sanitizeString($_POST['pass']);
$email = sanitizeString($_POST['email']);
if ($user == "" || $pass == "" || $email == "")
$error = "Not all fields were entered<br /><br />";
else
{
if (mysql_num_rows(queryMysql("SELECT * FROM members
WHERE user='$user'")))
$error = "That username already exists<br /><br />";
else
{
queryMysql("INSERT INTO members VALUES('$user', '$pass', '$email')");
$_SESSION['user'] = $user;
header("Location: index.php"); // redirects
}
}
}
?>