简体   繁体   English

使用PDO的简单登录功能

[英]simple login function using PDO

im still in the early learning stages, banging my head against walls looking for clues. 我仍处于早期学习阶段,将头撞在墙上寻找线索。 Iv been reading the manual to no avail. iv一直没有阅读手册。

im building a user log in system based on the phpAcadamy tutorial 'Register & Login'. 即时通讯基于phpAcadamy教程“注册和登录”构建用户登录系统。 They use mysql_connect in the tutorial, but I am using a PDO connection to mysql. 他们在本教程中使用mysql_connect,但是我正在使用与mysql的PDO连接。

1) the function user_id_from_username should return the user_id entry for the posted $username. 1)函数user_id_from_username应该返回发布的$ username的user_id条目。 mine does not, im confused about how to simply return the entry, and i just need a little bit of guidance and explanation. 我没有,我对如何简单地返回条目感到困惑,我只需要一点指导和解释。

2) the login function works, BUT i need it to return $user_id if TRUE, so that i can set the session. 2)登录功能的工作,但我需要它返回$ user_id如果为TRUE,以便我可以设置会话。

here is my code: 这是我的代码:

    <?php 
    function user_id_from_username(PDO $db, $username) {
    $stmt = $db->prepare('SELECT `user_id` FROM `users` WHERE `username` = 1');
    $stmt->bindParam(1, $username);
    $stmt->execute();
    return ($stmt->fetchColumn());
    }   //??? I NEED THIS FUNCTION TO RETURN THE `user_id` ENTRY FOR $username

    function login(PDO $db, $username, $password) {
    $user_id = user_id_from_username($db, $username);
    $password = md5($password);
    $stmt = $db->prepare('SELECT COUNT(`user_id`) FROM `users` WHERE `username` = ? AND `password` = ?');
    $stmt->bindParam(1, $username);
    $stmt->bindParam(2, $password);
    $stmt->execute();
    return (bool) $stmt->fetchColumn();
    }   //??? I NEED THIS FUNCTION TO RETURN $user_id IF TRUE (to set session)

    //---------------------login.php-----------------------

    if (empty($_POST) === false) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    if (empty($username) === true || empty($password) === true) {
    $errors[] = 'You need to enter a username and password.';
    } else if (user_exists($db, $username) === false) {
    $errors[] = 'We can\'t find that username. Have you registered?';
    } else if (user_active($db, $username) === false) {
    $errors[] = 'You haven\'t activated your account!';
    } else {
    $login = login($db, $username, $password);
    if ($login === false) {
    $errors[] = 'That username/password combination is incorrect.';
    } else {
    die($login);


    }
    }
    print_r($errors);
    }

So, according to this login script, after a successful login (good username and password, and active account) it should output the $user_id integer: "die($login)". 因此,根据此登录脚本,在成功登录(良好的用户名和密码以及有效的帐户)之后,它应输出$ user_id整数:“ die($ login)”。

(I didnt include the other two functions: user_exists and user_active, because i have them working properly) (我没有包括其他两个功能:user_exists和user_active,因为我让它们正常工作)

It prints the error array correctly, it logs in ok, except for this next step. 它将正确打印错误数组,并且登录正常(下一步除外)。

Thanks in advance for helping me out! 在此先感谢您对我的帮助!

You have to put ? 你要放? here 这里

$stmt = $db->prepare('SELECT `user_id` FROM `users` WHERE `username` = 1');

should be 应该

$stmt = $db->prepare('SELECT `user_id` FROM `users` WHERE `username` = ?');

Edit 编辑

Use this function 使用这个功能

function login(PDO $db, $username, $password) {
$user_id = user_id_from_username($db, $username);
$password = md5($password);
$stmt = $db->prepare('SELECT COUNT(`user_id`) FROM `users` WHERE `username` = ? AND `password` = ?');
$stmt->bindParam(1, $username);
$stmt->bindParam(2, $password);
$stmt->execute();
if($stmt->fetchColumn() > 0) {
    return $user_id;
} else {
    return false;
}
}

In prepared statements ? 在准备好的陈述中? is used. 用来。 The ? ? is replaced with the variable given in bind_param . bind_param给定的变量替换。 In your code ? 在您的代码中? will be replaced with $username 将被替换为$username

$stmt = $db->prepare('SELECT `user_id` FROM `users` WHERE `username` = ?');
$stmt->bindParam(1, $username);

Using user_id_from_username() in login function makes absolutely no sense. 在登录功能中使用user_id_from_username() 绝对没有意义。
As well as getting count instead of actual id 以及获得计数而不是实际ID

You could do it in one step: 你可以这样做在一个步骤:

function login(PDO $db, $username, $password) {
    $password = md5($password); // you should add salt and change hash function
    $sql = 'SELECT user_id FROM `users` WHERE `username` = ? AND `password` = ?';
    $stmt = $db->prepare($sql);
    $stmt->execute(array($username, $password));
    return $stmt->fetchColumn();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM