Jump to content

simple login function using PDO


Go to solution Solved by junkomatic,

Recommended Posts

Hi, im still in the early learning stages, banging my head against walls looking for clues. Iv been reading the manual to no avail.

 

im building a user log in system based on the phpAcadamy tutorial 'Register & Login'. They use mysql_connect in the tutorial, but I am using a PDO connection to mysql.

 

1) the function user_id_from_username should return the `user_id` entry for the posted $username. 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.

 

here is my code:

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)". 

 

It prints the error array correctly, it logs in ok, except for this next step.

Thanks in advance!

Edited by junkomatic
Link to comment
https://forums.phpfreaks.com/topic/277567-simple-login-function-using-pdo/
Share on other sites

  • Solution

ok i fixed it:

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;
}
}

just needed to add the if/else at the end of this login function duh!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.