用户登录和注册php页面不能一起工作但是单独工作。

时间:2013-08-03 15:30:55

标签: php html forms login

我刚刚开始编写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>";
}
?>

的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
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

您要包含登录信息&amp;注册页面进入标题页然后你在每个登录和注册页面中包含标题页似乎是错误的。

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

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

因此,如果用户签名,则将他重定向回登录视图

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

最好使用MVC模式,因为它可以保持布局清洁,并且调试很容易。

希望这有帮助。