Skip to content

Instantly share code, notes, and snippets.

@anoxic
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save anoxic/11dd6cacfbeafbaac330 to your computer and use it in GitHub Desktop.

Select an option

Save anoxic/11dd6cacfbeafbaac330 to your computer and use it in GitHub Desktop.
<?php
if(!empty($_POST))
{
// This is the error message you will display if something goes wrong. For now this will be empty:
$error = "";
// Wrap everything inside a "try" block so that you can display
// the error message but still also render your form.
try
{
// All of your error checking will be the same
// you will just need to use `throw new Exception("error message")`
// instead of `die("error message")`
if(empty($_POST['username']))
{
throw new Exception("Please enter a username.");
}
if(empty($_POST['password']))
{
throw new Exception("Please enter a password.");
}
// You will need to include everything that should happen if all goes correctly
// within this "try" block, including the redirect back to the login page
}
catch (Exception $e)
{
// Set the error message to something you can display later:
$error = $e->getMessage();
}
}
?>
<!-- Display an error message like so -->
<?php if (!empty($error)): ?>
<div class="error">We ran into the following problem: <?php echo $error; ?></div>
<?php endif; ?>
<!-- You can refill your form inputs like so, adding a value based on the request -->
<input type="text" name="username" value="<?php echo $_POST['username']; ?>">
@anoxic
Copy link
Author

anoxic commented May 14, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment