diff options
author | h5p9sl <21267024+h5p9sl@users.noreply.github.com> | 2021-05-13 22:07:11 -0600 |
---|---|---|
committer | h5p9sl <21267024+h5p9sl@users.noreply.github.com> | 2021-05-14 02:37:13 -0600 |
commit | fe41f57df59c7f2a11d80eaaebf08d3a3a51a6d5 (patch) | |
tree | 8917741bbe991d6ddf4f23953309288975f56eb2 | |
parent | 4145fa13230d25d9978c003a8cccd1b7c2e11aaf (diff) |
Add error handling
-rw-r--r-- | create_thread.php | 4 | ||||
-rw-r--r-- | includes/Session.php | 2 | ||||
-rw-r--r-- | includes/error.php | 25 | ||||
-rw-r--r-- | register.php | 10 | ||||
-rw-r--r-- | signin.php | 10 | ||||
-rw-r--r-- | signout.php | 2 | ||||
-rw-r--r-- | viewthread.php | 19 |
7 files changed, 52 insertions, 20 deletions
diff --git a/create_thread.php b/create_thread.php index 6fb7df9..a203b39 100644 --- a/create_thread.php +++ b/create_thread.php @@ -10,6 +10,7 @@ <h2>Create a new thread</h2> <?php include_once 'includes/Session.php'; +include_once 'includes/error.php'; if (!Session::get()->is_signed_in()) { trigger_error('You must be <a href="signin.php">signed in</a> to create a thread.'); exit(); @@ -44,6 +45,7 @@ if (!Session::get()->is_signed_in()) { <?php include_once 'includes/functions_post.php'; include_once 'includes/functions_thread.php'; +include_once 'includes/error.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING); @@ -51,7 +53,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $thread_cat = filter_input(INPUT_POST, 'thread_cat', FILTER_SANITIZE_NUMBER_INT); if (empty($thread_subject) or !$thread_subject) { - echo 'Thread subject cannot be empty'; + trigger_error('Thread subject cannot be empty'); } else { $thread_id = create_thread($thread_subject, $thread_cat); create_post($post_content, $thread_id, $thread_cat); diff --git a/includes/Session.php b/includes/Session.php index 7951d70..ceaa765 100644 --- a/includes/Session.php +++ b/includes/Session.php @@ -54,4 +54,4 @@ class Session return $result; } -}
\ No newline at end of file +} diff --git a/includes/error.php b/includes/error.php new file mode 100644 index 0000000..5e33212 --- /dev/null +++ b/includes/error.php @@ -0,0 +1,25 @@ +<?php +function user_notice($message) { + echo '<p class="error">'. $message .'</p>'; +} + +function handle_error($errno, $errstr, $errfile, $errline) { + if (!(error_reporting() & $errno)) { + // This error code is not included in error_reporting, so let it fall + // through to the standard PHP error handler + return false; + } + + switch ($errno) { + // See https://www.php.net/manual/en/errorfunc.constants.php + case E_USER_NOTICE: + user_notice($errstr); + break; + default: + return false; + } + return true; +} + +$old_error_handler = set_error_handler('handle_error'); +?> diff --git a/register.php b/register.php index 02fbe58..4c42610 100644 --- a/register.php +++ b/register.php @@ -21,6 +21,7 @@ <?php include_once './includes/functions_user.php'; +include_once './includes/error.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $errors = array(); @@ -61,15 +62,16 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } if (!empty($errors)) { - echo 'Please check the following problems: <ul>'; + $errstr = 'Please check the following problems: <ul>'; foreach ($errors as $err) { - echo '<li>' . $err . '</li>'; + $errstr .= '<li>' . $err . '</li>'; } - echo '</ul>'; + $errstr .= '</ul>'; + trigger_error($errstr); } else { $pass_hash = password_hash($user_pass, PASSWORD_DEFAULT); register_user($user_name, $pass_hash); - echo 'Account successfully registered! You can now <a href="signin.php">sign in</a>'; + echo '<p class="success">Account successfully registered! You can now <a href="signin.php">sign in</a></p>'; } } ?> @@ -17,6 +17,7 @@ </form> <?php +include_once 'includes/error.php'; function validate($data) { @@ -42,17 +43,18 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } if (!empty($errors)) { - echo 'Please check the following problems: <ul>'; + $errstr = 'Please check the following problems: <ul>'; foreach ($errors as $err) { - echo '<li>' . $err . '</li>'; + $errstr .= '<li>' . $err . '</li>'; } - echo '</ul>'; + $errstr .= '</ul>'; + trigger_error($errstr); } else { $user = new User(); $result = $user->get_by_name($user_name); if (!$result) { - echo 'There is no user with that name. Did you mean to <a href="register.php">create a new account?</a>'; + trigger_error('There is no user with that name. Did you mean to <a href="register.php">create a new account?</a>'); } else { if (!password_verify($user_pass, $user->password)) { echo 'Password does not match!'; diff --git a/signout.php b/signout.php index 035877b..bbaa47a 100644 --- a/signout.php +++ b/signout.php @@ -13,4 +13,4 @@ include_once './includes/templates/header.php'; echo '<p class="success">You have now been signed out</p>'; ?> </body> -</html>
\ No newline at end of file +</html> diff --git a/viewthread.php b/viewthread.php index e8eda06..fa1c81b 100644 --- a/viewthread.php +++ b/viewthread.php @@ -68,29 +68,30 @@ foreach ($posts as $post) { ?> <hr> <h2>Reply to this thread</h2> -<form method="post"> - <textarea name="post_content" rows="10" cols="50"></textarea> - <br> - <input type="submit" name="submit"> -</form> -</body> -</html> <?php include_once 'includes/functions_post.php'; +include_once 'includes/error.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!Session::get()->is_signed_in()) { - echo 'You must be <a href="signin.php">signed in</a> to reply to this thread.'; + trigger_error('You must be <a href="signin.php">signed in</a> to reply to this thread.', E_USER_NOTICE); return; } $post_content = filter_input(INPUT_POST, 'post_content', FILTER_SANITIZE_STRING); if (empty($post_content) or !$post_content) { - echo 'Thread subject cannot be empty'; + trigger_error('Reply cannot be empty'); } else { create_post($post_content, $current->id, $current->category->id); header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $current->id); } } ?> +<form method="post"> + <textarea name="post_content" rows="10" cols="50"></textarea> + <br> + <input type="submit" name="submit"> +</form> +</body> +</html> |