0

我刚刚开始编写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:&nbsp
                 <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:&nbsp</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:&nbsp</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
        }
    }
}
?>
4

1 回答 1

1

您将登录和注册页面包含在标题页面中,然后您将标题页面包含在每个似乎错误的登录和注册页面中。

要实现您的目标,请按照以下步骤操作

创建一个 html 视图页面,您可以在其中显示引用登录和注册 php 脚本的登录和注册表单。

所以如果用户注册然后将他重定向回登录视图

如果他登录然后将他重定向到内容页面。

最好使用 MVC 模式,因为它可以保持布局整洁且易于调试。

希望这可以帮助 。

于 2013-08-03T15:51:21.560 回答