我想问我如何只使用一个功能通过电子邮件或用户名登录用户。我试过这段代码
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();
}
}