Welcome Guest, Not a member yet? Register   Sign In
How to Sanitize data in FilterInterface CI4
#1

Hi,

Can somebody tell me how to use FilterInterface in modifying request before using it in the Controller? I want to sanitize or to remove unwanted characters in the data before using it in my Controller.

Here is the sample of my code: 

Filter:

PHP Code:
namespace App\Filters;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;

class 
SanitizeFields implements FilterInterface
{

    protected $session;

    public function before(RequestInterface $request$arguments null)
    {
        $request->getVar('number_only'FILTER_SANITIZE_NUMBER_INT);
    }

    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
        // Do something here
    }



Controller: 
PHP Code:
//controller
public function getData()
{
    echo $this->request->getVar('number_only');


Is it possible to sanitize the value of number_only variable from "123abc456" to "123456"? 

Thank you!
Reply
#2

PHP Code:
// Check to see if the variable is an integer
if (filter_var($this->request->getVar('number_only'), FILTER_VALIDATE_INT) === false)
{
    // ERROR!  
}
else
{
    echo $this->request->getVar('number_only');

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB