0

我想问我如何只使用一个功能通过电子邮件或用户名登录用户。我试过这段代码

    function loginemail($username,$password)
{
    $timestamp = time();
    $time = date("d/m/y h:i:s a", $timestamp);
    $sql = mysql_query("SELECT * FROM users WHERE username ='$username'");
    $sql = mysql_query("SELECT * FROM users WHERE email ='$username'");
    $row = mysql_fetch_assoc($sql);
    $maintenance = mysql_query("SELECT * FROM settings");
    $row2 = mysql_fetch_assoc($maintenance);
    $main = $row2['maintenance'];

if($main == 1)
{
    if($row['password'] == $password && $row['username'] == $username)
    {
        $query = mysql_query("INSERT INTO log (id,username,time,action) VALUES 

(NULL,'$username','$time','Login')");
        if(!$query)
        {
            echo "FAILED";
        }
        else
        {
        $_SESSION['username'] = $row['username'];
        header('Location: /dash');
        }
    }
    elseif($row['password'] == $password && $row['email'] == $username && $row['rank'] == 5)
    {
        $user = $row['username'];
        $_SESSION['username'] = $row['username'];
        $_SESSION['email'] = $row['email'];
        header('Location: /user/chooser');
    }
    else
    {
        header("Location: /index/error");
    }
}
else
{
    if($row['password'] == $password && $row['username'] == $username)
    {
        $query = mysql_query("INSERT INTO log (id,username,time,action) VALUES 

(NULL,'$username','$time','Login')");
        if(!$query)
        {
            echo "FAILED";
        }
        else
        {
        $_SESSION['username'] = $row['username'];
        header('Location: /dash');
        }
    }
    elseif($row['password'] == $password && $row['email'] == $username)
    {
        $user = $row['username'];
        $_SESSION['username'] = $row['username'];
        $_SESSION['email'] = $row['email'];
        header('Location: /user/chooser');
    }
    else
    {
        header("Location: /index/error");
    }
}
}

但它不适用于用户名部分。现在我已将代码更改为使用 php oop。所以这是我当前的代码。

class User {

    public function check($uname,$passwd)
    {
            $p=md5($passwd);
            $run=mysql_query("SELECT * FROM users WHERE username='$uname'");
            $user_data = mysql_fetch_array($run);
            $row=mysql_num_rows($run);
            if ($row == 1)
            {
                $_SESSION['login'] = true;                                                  $_SESSION['username'] = $user_data['username'];
                header('Location: /dash');
            }
            else
            {
                return FALSE;
            }
    }

    public function logout()
    {
        $_SESSION['login'] = FALSE;
            session_destroy();
    }
}
4

3 回答 3

3

改变这个

$sql = mysql_query("SELECT * FROM users WHERE username ='$username'");
$sql = mysql_query("SELECT * FROM users WHERE email ='$username'");
$row = mysql_fetch_assoc($sql);

$sql = mysql_query("SELECT * FROM users WHERE username ='$username' or email = '$username'");
$row = mysql_fetch_assoc($sql);
于 2013-04-19T07:17:55.677 回答
3

尝试这个:

$user = mysql_query("SELECT * FROM users WHERE (username = '$username' OR email = '$email') AND password = '$password'");

编辑

由于输入的登录名是用户名或电子邮件,

$user = mysql_query("SELECT * FROM users WHERE (username = '$login_name' OR email = '$login_name') AND password = '$password'");
于 2013-04-19T07:23:21.787 回答
0
<form action="login.php" method="post">

    Username* <br/><input type="text" name="login_username" /><br/>
    Password* <br/><input type="password" name="login_pass" /><br/>

    <br/>
    <input type="submit" value="submit" />

</form>

我做了这样的登录。和后端:

<?php
    $username = $_POST['login_username'];
    $pass = $_POST['login_pass'];

    define(MYSQL_HOST,"localhost");
    define(MYSQL_USER,"youruser");
    define(MYSQL_PASS, "yourpassword");

    include "classes/MySQL.class.php";


    $db = new MySQL();
    $db->Connect("localhost","youruser","yourpassword");
    $db->selectDb("testproject");

    if(!isset($_SESSION)) { session_start(); }

    $result = "SELECT * FROM `registration` WHERE `username` = '".$username."' and `pass` ='".md5($pass)."';";
    $db->Query($result) or trigger_error($db->Error());

    $row = $db->getRow();


    if(!empty($row))
    {
        $_SESSION['error_login']="You are a validated user.";
        header("Location: thx3.php");
    }
    else
    {
        $_SESSION['error_login']="Sorry, your credentials are not valid, Please try again.";
        header("Location: home.php");
    }

?>

希望它会帮助你。当然你必须做出改变,但这对我来说非常有效。

于 2013-04-19T08:07:21.190 回答