Error Handling

0 Vraj Patel · December 30, 2014
The following is code from index.php

<!DOCTYPE html>
<html lang='en'>
<?php
include 'core/init.php';
include 'includes/head.php';
?>
<body>
<?php include 'includes/overall/header.php';?> 
<form action="login.php" method="post">
  <h1>Secure Web Server Login</h1>
  <div class="inset">
  <p>
    <label for="email">Username</label>
    <input type="text" name="username" id="username">
  </p>
  <p>
    <label for="password">Password</label>
    <input type="password" name="password" id="password">
  </p>
  <p>
    <input type="checkbox" name="remember" id="remember">
    <label for="remember">Cache Crudentials</label>
  </p>
  </div>
  <p class="p-container">
    <span>Password Reset</span>
<span><a href="register.php">      Register</a></span>
    <input type="submit" name="go" id="go" value="Log In">
  </p>
</form>
<?php include 'includes/overall/footer.php'; ?>
</body>
</html>

This code is from login.php

<?php
include 'core/init.php';
if(empty($_POST) === false)
{
$username = $_POST['username'];
$password = $_POST['password'];

if(empty($username) === true || empty($password) === true)
{
$errors[] = 'You must enter a username and password in order to proceed!';
} else if(user_exists($username) === false)
{
$errors[] = 'That username does not exist within the database. Please register or type a valid username in order to proceed!';
} else if(user_active($username) === false)
{
$errors[] = 'Account is not activated! Please activate your account and try again.';
} else{
$login = login($username, $password);
if($login === false)
{
$errors[] = 'The username and password combination you entered is incorrect!';
}
else
{
$_SESSION['user_id'] = $login;
header('Location: home.php');
exit();
}
}
print_r($errors);
}
?>

Hey guys, I need help modifying and correcting this code in such a way where is displays any error in the error array on the login page and not on a different page. I have been following the PHP login and register tutorials and have gotten to the point where he explains this however, when I do what he did, it does not work for me. I think it is because I used a different template than he did in terms of design and organization of the website but the code is the same. The same code has worked thus far but not now. Please explain simply because I am very much more a Java and C++ guy than I am a web design/ web programming guy. 

Post a Reply

Replies

Oldest  Newest  Rating
+1 Chris Sisco · December 30, 2014
I'm just going to assume by "login page" you are talking about the index.php that shows your login form and not login.php.

If there are errors logging in you can set a $_SESSION variable in login.php and assign the $error array to it then set the header location to redirect back to index.php. You can then have index.php check if that $_SESSION variable is set and if it is display the errors somewhere on the page.

Below is how I would do it. I can't promise there are not any errors though. I didn't test it. :)

login.php


<?php
include 'core/init.php';
if(empty($_POST) === false)
{
$username = $_POST['username'];
$password = $_POST['password'];

if(empty($username) === true || empty($password) === true)
{
$errors[] = 'You must enter a username and password in order to proceed!';
} else if(user_exists($username) === false)
{
$errors[] = 'That username does not exist within the database. Please register or type a valid username in order to proceed!';
} else if(user_active($username) === false)
{
$errors[] = 'Account is not activated! Please activate your account and try again.';
} else{
$login = login($username, $password);
if($login === false)
{
$errors[] = 'The username and password combination you entered is incorrect!';
}
else
{
$_SESSION['user_id'] = $login;
header('Location: home.php');
exit();
}
}

$_SESSION['errors'] = $errors;
header('Location: index.php');
exit();

}
?>


index.php


<!DOCTYPE html>
<html lang='en'>
<?php
include 'core/init.php';
include 'includes/head.php';
?>
<body>
<?php include 'includes/overall/header.php';?>

<?php
if(isset($_SESSION['errors']))
{
$errors = $_SESSION['errors'];
foreach($errors as $e)
{
echo '<div class="errorStyle">'.$e.'</div>';
}
}
?>

<form action="login.php" method="post">
<h1>Secure Web Server Login</h1>
<div class="inset">
<p>
<label for="email">Username</label>
<input type="text" name="username" id="username">
</p>
<p>
<label for="password">Password</label>
<input type="password" name="password" id="password">
</p>
<p>
<input type="checkbox" name="remember" id="remember">
<label for="remember">Cache Crudentials</label>
</p>
</div>
<p class="p-container">
<span>Password Reset</span>
<span><a href="register.php">      Register</a></span>
<input type="submit" name="go" id="go" value="Log In">
</p>
</form>
<?php include 'includes/overall/footer.php'; ?>
</body>
</html>
0 Ron Butcher · December 31, 2014
I would have the form submit to itself, and have you login file as an include in the index file. 

Here's how the logic would work:  When you first load the page, it goes through the login.php file.  Your first statement is to see if POST is empty, it will check that.  Since the form has not been submitted, it will skip the rest of the programming in login.php and move onto the form.

When the form is submitted, it will go through the login.php file since the POST is not empty.  If there is an error, it will display the error right before the form, if there are no errors, you have a header command that sends the user to another page.

index.php

<!DOCTYPE html>
<html lang='en'>
<?php
include 'core/init.php';
include 'includes/head.php';
?>
<body>
<?php include 'includes/overall/header.php';?> 
<?php include 'login.php'; //  Add This Line Here ?>   
<form action="index.php" method="post">
  <h1>Secure Web Server Login</h1>
  <div class="inset">
  <p>
    <label for="email">Username</label>
    <input type="text" name="username" id="username">
  </p>
  <p>
    <label for="password">Password</label>
    <input type="password" name="password" id="password">
  </p>
  <p>
    <input type="checkbox" name="remember" id="remember">
    <label for="remember">Cache Crudentials</label>
  </p>
  </div>
  <p class="p-container">
    <span>Password Reset</span>
<span><a href="register.php">      Register</a></span>
    <input type="submit" name="go" id="go" value="Log In">
  </p>
</form>
<?php include 'includes/overall/footer.php'; ?>
</body>
</html>

login.php

<?php
include 'core/init.php';

$errors = []; // Initialize the errors array to make sure there are no problems when no error exists

if(empty($_POST) === false)
{
$username = $_POST['username'];
$password = $_POST['password'];

if(empty($username) === true || empty($password) === true)
{
$errors[] = 'You must enter a username and password in order to proceed!';
} else if(user_exists($username) === false)
{
$errors[] = 'That username does not exist within the database. Please register or type a valid username in order to proceed!';
} else if(user_active($username) === false)
{
$errors[] = 'Account is not activated! Please activate your account and try again.';
} else{
$login = login($username, $password);
if($login === false)
{
$errors[] = 'The username and password combination you entered is incorrect!';
}
else
{
$_SESSION['user_id'] = $login;
header('Location: home.php');
exit();
}
}
print_r($errors);
}
?>

I only changed a couple lines of code in these examples.
0 Vraj Patel · December 31, 2014
Thank You so much for your responses :). Chris Sisco I tried your code first and it worked successfully thanks alot :). Ron Butcher I am sorry that I did not get a chance to try yours but if I did I am pretty sure it would work :D
  • 1

PHP

107,187 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators