我想问一下如何通过电子邮件或用户名只使用一个功能来登录用户。我试过这段代码
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();
}
}
答案 0 :(得分: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);
答案 1 :(得分: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'");
答案 2 :(得分: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");
}
?>
希望它会对你有所帮助。当然你必须做出改变,但这对我来说非常有用。