Link to home
Start Free TrialLog in
Avatar of Wanda Marston
Wanda MarstonFlag for Canada

asked on

My login form is not validating email or password.

My login form is letting me login no matter what I type into the fields. I have a database set up and I am able to "register". However, my login form is not telling me that I do not have a valid email address and it is letting me put anything into the password field.

I have included three separate pieces of code

Below is the code for the top part of my Index page.
<?php

// This file is the home page. 

// Require the configuration before any PHP code as the configuration controls error reporting:
require ('./includes/config.inc.php');
// The config file also starts the session.

// To test the sidebars:
// $_SESSION['user_id'] = 1;
// $_SESSION['user_admin'] = true;

// Require the database connection:
require (MYSQL);

// If it's a POST request, handle the login attempt:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	include ('./includes/login.inc.php');
}

Open in new window


The Login Form that is on the Index page;

<?php // Show the user info or the login form:

 if (isset($_SESSION['user_id'])) {

	// Show basic user options:
	// Includes references to some bonus material discussed in Chapter 5!
	echo '<div class="title">
				<h4>Manage Your Account</h4>
			</div>
			<ul>
			<li><a href="renew.php" title="Renew Your Account">Renew Account</a></li>
			<li><a href="change_password.php" title="Change Your Password">Change Password</a></li>
			<li><a href="favorites.php" title="View Your Favorite Pages">Favorites</a></li>
			<li><a href="history.php" title="View Your History">History</a></li>
			<li><a href="recommendations.php" title="View Your Recommendations">Recommendations</a></li>
			<li><a href="Logout.php" title="Logout">Logout</a></li>
			</ul>
			';
			
	// Show admin options, if appropriate:
	if (isset($_SESSION['user_admin'])) {
		echo '<div class="title">
					<h4>Administration</h4>
				</div>
				<ul>
				<li><a href="add_page.php" title="Add a Page">Add Page</a></li>
				<li><a href="add_pdf.php" title="Add a PDF">Add PDF</a></li>
				<li><a href="#" title="Blah">Blah</a></li>
				</ul>
				';		
	}
					
} else { // Show the login form:
	
	require ('includes/login_form.inc.php');
	
}

?>

Open in new window


The Login Code

<?php 

// This is the login page for the site.

// Array for recording errors:
$login_errors = array();

// Validate the email address:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$e = mysqli_real_escape_string ($connect, $_POST['email']);
} else {
	$login_errors['email'] = 'Please enter a valid email address!';
}

// Validate the password:
if (!empty($_POST['pass'])) {
	$p = mysqli_real_escape_string ($connect, $_POST['pass']);
} else {
	$login_errors['pass'] = 'Please enter your password!';
}
	
if (empty($login_errors)) { // OK to proceed!

	// Query the database:
	$q = "SELECT id, username, type, IF(date_expires >= NOW(), true, false) FROM users WHERE (email='$e' AND pass='"  .  get_password_hash($p) .  "')";		
	$r = mysqli_query ($connect, $q);
	
	if (mysqli_num_rows($r) == 1) { // A match was made.
		
		// Get the data:
		$row = mysqli_fetch_array ($r, MYSQLI_NUM); 
		
		// If the user is an administrator, create a new session ID to be safe:
		// This code is created at the end of Chapter 4:
		if ($row[2] == 'admin') {
			session_regenerate_id(true);
			$_SESSION['user_admin'] = true;
		}
		
		// Store the data in a session:
		$_SESSION['user_id'] = $row[0];
		$_SESSION['username'] = $row[1];
		
		// Only indicate if the user's account is not expired:
		if ($row[3] == 1) $_SESSION['user_not_expired'] = true;
			
	} else { // No match was made.
		$login_errors['login'] = 'The email address and password do not match those on file.';
	}
	
} // End of $login_errors IF.

// Omit the closing PHP tag to avoid 'headers already sent' errors!

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I use PHP although I am far from an expert. I have never seen this before though:

require ('./includes/config.inc.php');

Are you sure it is not:

require ('includes/config.inc.php');

or

require ('../includes/config.inc.php');
Avatar of Wanda Marston

ASKER

Thank you for both posts.

I have two test sites set up both going into the same data base. On the one site the Login sections works and on the other it doesn't and the pages are set up differently on both pages.

They both have this code ('./includes/config.inc.php'): and I have changed this around thinking that this may be a factor.

I have quickly looked through the above article and there is no doubt there are items there I don't understand. I will definitely reread.

However, I am wondering about this particular piece of code and if I have this in the wrong spot.

// If it's a POST request, handle the login attempt:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	include ('./includes/login.inc.php');
}

Open in new window


Thank you,

Wchirnisde
I think I might put error_reporting(E_ALL) in the script.  And I would change the include() to require_once().  That way it should throw an error if anything is missing.
rbudj the "./" indicates current directory.  
ok thank you shdwmage
Hello:

I received this error –

Parse error: syntax error, unexpected T_IF in  xxxxxxxx /Index.php on line 20

This is line 20 – if ($_SERVER['REQUEST_METHOD'] == 'POST') {

of the piece of code that I indicated may be in the wrong spot.

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	require_once ('./includes/login.inc.php');
}

Open in new window


Thank you,

Wchirnside
The word "unexpected" in a parse error message means that something upstream in the script is missing.  Maybe a semi-colon that ends a statement or something like that.
Okay, found out where the missing semicolon should be. Added the error script as mentioned and then changed the include to require_once. The page loaded and everything worked as before, meaning that I could put all xxx's or whatever in the email and password fields and I could hit the login button and go to the next page.

Wchirnside
Hello,

Have now figured this out. I did have that piece of code in the wrong spot considering that my Login button was going to a different page.

Your suggestions were helpful as I might not have reached that conclusion so quickly had I not got some outside input.

Thank you,

WChirnside
I seem to be learning PHP in spite of myself.

Thank you for your help.
;-)

This is a great book: http://www.sitepoint.com/books/phpmysql4/

Thanks for the points, ~Ray
Hello,

Believe it or not started out with that book over a year ago but needed how to integrate it with Dreamweaver or at least I thought I did so I got sidetracked somewhat. Have several other books and also like the ones by Larry Ullman.

WChirnside