PHPのコーディングを始めたばかりです。ユーザーログインとサインアップフォームを同じページに作りたいです。これらのコードは両方とも個別に機能しますが、それらを組み合わせるときに問題が発生します。誰かが私がどこで間違っているのか説明できますか?
私が抱えている2つの問題は次のとおりです。
1) ログインしようとすると、エラー undefined variable email が表示されます (サインアップ時ではありません)。
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>";
}
?>
login.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
}
}
}
?>