Jump to content

[SOLVED] Login script help


wrathican

Recommended Posts

hey guys im making a login script that does the following in this order:

1 checks to see if the username exsists

2 if the username exists, get the password from the db

3 compare the db pass and entered pass

4 if they match check to see if the account is activated

a if the account isnt activated it returns to some page with an error

b if the account is active it logs the person in using sessions

 

login script:

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$password = sha1($password);

$query = "SELECT user_username, user_password, user_level, user_active FROM ls12_users WHERE user_username='".$username."'";
$result = query($query);
$rows = numRows($result);

if($rows == 1) {
	$array = fetchArray($result);
	$username = $array['user_username'];
	$dbpassword = $array['user_password'];
	$level = $array['user_level'];
	$active = $array['user_active'];

	if ($password == $dbpassword) {
		//check if active
		if ($active == 1) {
			//the account is active. set sessions
			$_SESSION['username'] = $username;
			$_SESSION['level'] = $level;

			header('location: ../index.php');

		}else{
			//account is not active
			$_SESSION['errorstate'] = 1;
			$errorarray[] = "Sorry, but your account has not been activated yet. Please check your email for the activation email and follow the instructions there.";
			$_SESSION['errormessage'] = $errorarray;

			header('location: ../login.php');
		}
	}else{

		$_SESSION['errorstate'] = 1;
		$errorarray[] = "Sorry, but the password you entered was incorrect. Please try again.";
		$_SESSION['errormessage'] = $errorarray;

	header('location: ../login.php');

	}

}else{
	$_SESSION['errorstate'] = 1;
	$errorarray[] = "Sorry, but the username you entered does not exsist. Please try again.";
	$_SESSION['errormessage'] = $errorarray;

	header('location: ../login.php');
}
?>

 

functions.inc:

<?php

//queries the db
function query($query) {
mysql_query($query);
}

//gets an array based on the query
function fetchArray($result) {
mysql_fetch_array($result);
}

//get the number of rows from the query
function numRows($result) {
mysql_num_rows($result);
}

?>

 

i get this error "Sorry, but the username you entered does not exsist. Please try again." whihch means that the username i entered doesnt exsist in the db. however the username DOES actually exsist, any ideas why i am getting this error?

Link to comment
https://forums.phpfreaks.com/topic/102006-solved-login-script-help/
Share on other sites

their is three lines that you may need to change,

 

change your SQL to this:

$query = "SELECT `user_username`, `user_password`, `user_level`, `user_active` FROM l`s12_users` WHERE `user_username` = '".$username."'";

 

and change $row to:

$rows = mysql_num_rows($result);

 

and finally, change $array to:

$array = mysql_fetch_array($result);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.