使用用户名或电子邮件登录

时间:2015-06-19 10:55:46

标签: php html mysql

我正在尝试让我的登录页面允许用户使用他们的用户名或电子邮件地址登录。

到目前为止,它只允许用户使用电子邮件登录。我需要一些新鲜的眼睛,我无法找到问题所在。我已经尝试删除每次使用电子邮件登录的提及,但仍然没有运气,表中的用户名和电子邮件字段都是唯一的,尝试交替使用。有人会帮忙吗?

登录表单

<?php
ini_set('display_errors', '1');
require_once '../includes/conn.php';

if($user->is_loggedin()!=""){
    $user->redirect('./index.php');
}

if(isset($_POST['login'])){
    $username = $_POST['username_email'];
    $email = $_POST['username_email'];
    $password = $_POST['password'];

    if($user->login($usrename,$email,$password)){
        $user->redirect('./index.php');
    }else{
        $error = "Login details provided do not match out records.<br /><br />";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>EpicOwl UK | CMS Admin Panel Login</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
    <a href="index.php"><img id="logo" src="../images/logo.png" /></a>
    <div id="navigation">
        <ul>
            <a href="../index.php"><li>Home</li></a>
            <a href="../users/profile.php"><li>My Profile</li></a>
            <a href="./index.php"><li>Admin Panel</li></a>
        </ul>
    </div>
</div>
<div id="content">
<form method="post"><br /><br />
    <h2>Administrator Login</h2>
    <?php
    if(isset($error)){
    ?>
    <em><?php echo $error; ?></em>
    <?php
    }
    ?>
    <input type="text" name="username_email" placeholder="Username/Email" required /><br /><br />
    <input type="password" name="password" placeholder="Password" /><br /><br />
    <button type="submit" name="login">Login</button><br /><br /><br />
    <label>Don't have an account?  Why not register one by clicking <a href="./register.php">HERE</a></label><br /><br /><br /><br />
</form>
</div>
<div id="footer">
    <p class="copyright">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>

班级档案

<?php
ini_set('display_errors', '1');
class USER{
    private $db;

    function __construct($conn){
        $this->db = $conn;
    }

    public function register($username,$email,$password){
       try{
           $new_password = password_hash($password, PASSWORD_DEFAULT);

           $stmt = $this->db->prepare("INSERT INTO users(username,email,password)VALUES(:username, :email, :password)");

           $stmt->bindparam(":username", $username);
           $stmt->bindparam(":email", $email);
           $stmt->bindparam(":password", $new_password);            
           $stmt->execute(); 

           return $stmt; 
       }
       catch(PDOException $e){
           echo $e->getMessage();
       }    
    }

    public function login($username,$email,$password){
       try{
          $stmt = $this->db->prepare("SELECT * FROM users WHERE username=:username OR email=:email LIMIT 1");
          $stmt->execute(array(':username'=>$username, ':email'=>$email));
          $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
          if($stmt->rowCount() > 0){
             if(password_verify($password, $userRow['password'])){
                $_SESSION['session'] = $userRow['id'];
                return true;
             }else{
                return false;
             }
          }
       }
       catch(PDOException $e){
           echo $e->getMessage();
       }
   }

    public function is_loggedin(){
        if(isset($_SESSION['session'])){
            return true;
        }
    }

    public function redirect($url){
        header("Location:$url");
    }

    public function logout(){
        session_destroy();
        unset($_SESSION['session']);
        return true;
    }
}
?>

SQL表:

-- phpMyAdmin SQL Dump
-- version 4.0.7
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 19, 2015 at 11:52 AM
-- Server version: 5.5.42
-- PHP Version: 5.3.28

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `cl47-dbuser-1yz`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(25) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(60) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

注册很好,用户名,电子邮件和&amp;密码全部插入。谢谢你的期待。

3 个答案:

答案 0 :(得分:3)

问题在于这一行:

if($user->login($usrename,$email,$password)){
                ^^^^^^^^^

拼写错误,应该显示为$username,这就是为什么它只允许您使用该电子邮件登录。

根据您的登录功能:

public function login($username,$email,$password)
                      ^^^^^^^^^

旁注:

确保密码列足够长以容纳哈希值。 60有时是不够的。将其增加到255以备将来使用,建议按照password_hash()

上的手册推荐
  

因此,建议将结果存储在数据库列中,该列可以扩展到超过60个字符(255个字符将是一个不错的选择)。

同时在error_reporting(E_ALL);

之上添加ini_set('display_errors', 1);

答案 1 :(得分:1)

您只需要管理if条件,该条件会检查字段用户名/电子邮件。

喜欢:考虑$username_email&amp; $password是我们的输入字段

步骤1:我们需要在登录功能中激活选择查询:

"SELECT * FROM users WHERE username=$username_email OR email=$username_email LIMIT 1"

第2步:获取用户详细信息后,我们需要使用$password验证该用户的密码(注意:必须检查密码是否加密

希望这会对你有所帮助

答案 2 :(得分:-1)

首先,您需要将两个表设为唯一(用户名)和(电子邮件),而不是将以下内容添加到您的mysql查询中,

$username = $_POST['username'];
$password = $_POST['password'];
$query="SELECT email,username from members WHERE username='".mysql_real_escape_string($username)."' OR email='".mysql_real_escape_string($username)."' and password='".mysql_real_escape_string($password)."' LIMIT 1";

如果行数大于“0”,则开始会话。