登录表单电子邮件和密码错误

时间:2013-12-17 13:46:09

标签: php mysql database

我创建了一个登录表单但是当我尝试登录时,它说电子邮件或密码不正确但我会使用正确的电子邮件和密码。

我在我的数据库用户表中创建用户但又一次我收到此错误。当我尝试发送电子邮件和密码时,所有错误都没问题,它说您需要输入电子邮件和密码。

并且激活错误也正常。我输入了正确的密码和电子邮件地址。给我错误。

这是 users.php

<?php 
function user_exists($email) {
    $email = sanitize($email);
    return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `users` WHERE `email` = '$email'"), 0) == 1) ? true : false;


    }

function user_active($email) {
    $email = sanitize($email);
    return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `users` WHERE `email` = '$email' AND `active` = 1"), 0) == 1) ? true : false;


    }
    function user_id_from_email($email) {
          $email = sanitize($email);
          return mysql_result(mysql_query("SELECT `id` FROM `users` WHERE `email` = '$email'"), 0, 'id');   
        }
    function login($email, $password) {
          $id = user_id_from_email($email);
          $email = sanitize($email);
          $password = md5($password);

          return (mysql_result(mysql_query("SELECT COUNT (`id`) FROM `users` WHERE `email` = '$email' AND `password` = '$password'"), 0 ) == 1) ? $id : false;
        }



?>

这是 login.php

<?php 

include("core/init.php");



if(empty($_POST) === false) {
    $email = $_POST['email'];
    $password = $_POST['password'];

    if (empty($email) === true || empty($password) === true) {

        $errors[] = 'You need to enter a email and password';
        } else if (user_exists($email) === false) {
            $errors[] = 'We can\'t find that email. Have you registered ?';

            } else if (user_active($email) === false) {
                $errors[] = 'You have\'t activated your account';
                } else {
                    $login = login($email, $password);
                    if ($login === false) {
                        $errors[] = 'That email/passowrd cocmbination is incorrect';
                        }else  {

                            $_SESSION['id'] = $login;
                            header('Location: main.php');

                            exit();

                            }

                    }
                    print_r ($errors);
    }
?>

1 个答案:

答案 0 :(得分:0)

很抱歉,但是有一大堆错误的东西 - 很多可能会产生错误。

1)php中没有'sanitize'功能,你还没有告诉我们它的用途。

2)你的login.php确实阻止了users.php

3)生成密码的md5哈希远非安全(它应该是最小化的sha1哈希)

4)您永远不会检查DBMS返回的错误

5)...实际上 - 这不是真的 - 你将users.php中函数的返回值与false进行比较 - 如果查询失败,你只会得到假 - 如果它返回0行则不会

考虑....

function do_something_with_email($email, &$err) {
      $email = mysql_real_escape_string($email);

      if (!($res=mysql_query("SELECT `id` FROM `users` WHERE `email` = '$email'"))) {
          $err=mysql_error();
          return false;
      }
      if (!($data=mysql_fetch_array($res)) {
          $err=mysql_error();
          return false;
      }
      return $data[0];
    }

 switch (do_something_with_email($email, $err)) {
  case false:
    die ($err);
  case 0:
    print "No records matched";
    break;
  default:
    print "OK";
    break;
 }