1

我为我的网站创建了一个登录表单,但目前它不起作用,我被定向到 invalid.php,这表明我的身份验证失败。

 <?php
     session_start();
     include("scripts/dbconnect.php");
     $numrows=0;
     $password=$_POST['password'];
     $email=$_POST['email'];
     $query="select fname,lname,email from mayan_users where (password='$password' && email='$email')";
     $link = mysql_query($query);
     if (!$link) {
      die('login error');
     }
     $numrows=mysql_num_rows($link);
     if ($numrows>0){  // authentication is successfull
      $row = mysql_fetch_array($link, MYSQL_ASSOC);
      $_SESSION['user']['fname']=$row['fname'];
      $_SESSION['user']['lname']=$row['lname'];
      $_SESSION['user']['email']=$row['email'];
      header("location:../index2.php");
     } else {
      header("location:../invalid.php");  // authentication was unsuccessfull
     }
    ?>

这是我的登录表格

<div id="Loginform" style="background-color:fuchsia; width:100%">

<span id="logspan">
     <input type="email" name="email" id="email" placeholder="Email" required />
     <input type="password" name="password" id="password" placeholder="Password" required/>
     <input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
    </span>

</div>
4

4 回答 4

4

如果您不通过其他方法提交登录名,则可能需要将其包装在表单标签中。

<form method="post" action="">
     <span id="logspan">
         <input type="email" name="email" id="email" placeholder="Email" required />
         <input type="password" name="password" id="password" placeholder="Password" required/>
         <input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
     </span>
</form>

如果您的 PHP 在另一个文档中,则将操作设置为表单处理器

action="form-processor.php"

(谢谢njk!)

于 2013-01-11T15:13:48.570 回答
1

您没有表单标签。像这样添加它们

<form method="POST" action="">
<!-- INSERT INPUTS HERE -->
</form>
于 2013-01-11T15:13:12.593 回答
0

试试这个:

<form id="Loginform" style="background-color:fuchsia; width:100%">

<span id="logspan">
     <input type="email" name="email" id="email" placeholder="Email" required />
     <input type="password" name="password" id="password" placeholder="Password" required/>
     <input type="button" name="submit" style="cursor:pointer" id="submit" value="Log In" onclick="logMeIn()" />
    </span>

</form>
于 2013-01-11T15:14:37.957 回答
0

首先,如果您的 PHP 版本支持,我建议您使用 PDO。
然后我想你的javascript函数以正确的方式发送参数。
最后,您可以避免检查行数。简单地

if($row = mysql_fetch_array($link))
{
  ...
}
else
{
  header("location: ../invalid.php");
  exit; //It's a good practice
}
于 2013-01-11T15:29:25.697 回答