Jump to content

Recommended Posts

I found a little login code snipplet online and was playing around with it a bit.. I'm adding an error detection array to it.. The error detection works perfectly. The problem I'm having is with matching the username and passwords from the database in the error if statements.. Here is the code:

 

<?php
// we must never forget to start the session
session_start();

include 'include/config.inc.php';

if (isset($_POST['txt_user_name']) && isset($_POST['txt_user_password'])) {
include 'include/database.inc.php';

// post form variables
$txt_user_name = $_POST['txt_user_name'];
$txt_user_password = $_POST['txt_user_password'];

// check if the user id and password combination exist in database
$query  = "SELECT user_name ";
$query .= "FROM dw_users ";
$query .= "WHERE user_name = '$txt_user_name' ";
$query .= "AND user_password = PASSWORD('$txt_user_password') ";		
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) { 
	$user_name = $row['user_name'];
	$user_password = $row['user_password'];
}
if (mysql_num_rows($result) == 1) {
	// the user id and password match,
	// set the session
	$_SESSION['db_is_logged_in'] = true;

	// after login we move to the main page
	header('Location: '.SCRIPT_URL.'accounts.php');
	exit();
} else {
	//array & variable declaration
	$errorarray = array();	//used to store error messages

	//echo $user_name; // doesnt even echo the username from the database while loop above
	//$user_name = "admin"; // works if I just set it manually
	//$user_password = "admin";

	//validate your input 
	if ($_POST['txt_user_name'] != "" && $_POST['txt_user_name'] != $user_name) { // using post for same page and request for page to page.. Just testing
		$_SESSION['errorstate'] = 1;
		$errorarray[] = "ERROR: Invalid username!";
	} elseif ($_POST['txt_user_name'] == "") { 
		$_SESSION['errorstate'] = 1;
		$errorarray[] = "ERROR: The username field is empty!";
	} else {
		// display nothing if correct
	}
	if ($_POST['txt_user_password'] != "" && $_POST['txt_user_password'] != $user_password) {
		$_SESSION['errorstate'] = 1;
		$errorarray[] = "ERROR: Invalid password!";
	} elseif ($_POST['txt_user_password'] == "") {
		$_SESSION['errorstate'] = 1;
		$errorarray[] = "ERROR: The password field is empty!";
	} else {
		// display nothing if correct
	}

	//check for errors
	if ($_SESSION['errorstate'] == 1) { // if error
		$_SESSION['errormessage'] = $errorarray; //store the errorarray in a session
	}   
}
// Close databse connection
mysql_close();
}
?>

 

The query pulls out the info just fine, but I can't figure out why it won't let me use $user_name, and $user_password in the if statements..

 

Link to comment
https://forums.phpfreaks.com/topic/119877-solved-how-do-i-remedy-this/
Share on other sites

This section doesnt read $user_name and $user_password.. Maybe because it's inside the if statement? Not sure, but I thought if I set $user_name = "admin"; at the top of the script, I would then be able to use it throughout..

 

<?php
//echo $user_name; // doesnt even echo the username from the database while loop above
	//$user_name = "admin"; // works if I just set it manually
	//$user_password = "admin";

	//validate your input 
	if ($_POST['txt_user_name'] != "" && $_POST['txt_user_name'] != $user_name) {
?>

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.