Welcome Guest, Not a member yet? Register   Sign In
Filter to custom error pages
#1

I'm using a simple honeypot filter, and I can't seem to display the error page correctly.  Can anyone see something wrong with what I'm doing here? Instead of the expected "Not allowed" message, I'm seeing a default message like: 
Quote:This page isn’t working
If the problem continues, contact the site owner.
HTTP ERROR 405


Here's what I'm doing....
#1
app/Filters/MyhoneypotFilter.php:
PHP Code:
<?php

namespace App\Filters;

use 
CodeIgniter\Honeypot\Exceptions\HoneypotException;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;
use 
Config\Services;

class 
MyhoneypotFilter implements FilterInterface
{
    /**
    * Checks if Honeypot field is empty, if not then the
    * requester is a bot
    *
    * @param array|null $arguments
    *
    * @throws HoneypotException
    */
    public function before(RequestInterface $request$arguments null)
    {
        if (Services::honeypot()->hasContent($request)) {
            log_message('warning'"Honeypot caught: " $request->getIPAddress());
            return Services::response()->setStatusCode(405);

        }
    }

    /**
    * Attach a honeypot to the current response.
    *
    * @param array|null $arguments
    */
    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
        Services::honeypot()->attachHoneypot($response);
    }


#2
I won't include everything in my htaccess, but if it matters, I'll add it.
mysite/public_html/.htaccess:
Code:
ErrorDocument 405 /error.php


#3
mysite/public_html/error.php:
PHP Code:
<?php
    $code 
$_SERVER['REDIRECT_STATUS'];
    $codes = array(
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Not Allowed',
        500 => 'Internal Server Error'
    );
    $source_url 'http'.((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 's' '').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (array_key_exists($code$codes) && is_numeric($code)) {
        die("Error $code{$codes[$code]}");
    } else {
        die('Unknown error');
    
Reply
#2

(This post was last modified: 01-10-2022, 12:48 AM by iRedds.)

You are expecting a message, but you are not sending it.
Therefore, you get the default message.
PHP Code:
return Services::response()->setStatusCode(405)->setBody('Not allowed'); 
Reply
#3

Thanks,
That gets me the message string, but it doesn't push out to my error.php file. If I'm using the CI framework, can I not use an external file to grab the REDIRECT_STATUS key/value?
Reply
#4

Never mind on this.... I think I'm making things harder than they need to be, and it's not worth the effort. I'm just going to throw ConfigException.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB