PHP filter_input() function not working with CI controller

520 views Asked by At

PHPs filter_input() function is not working within CI controller. However it works inside a view. The controller code is below.

class member extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $obj =& get_instance();
        $obj->load->helper('url');
        $this->load->library('session');

        $this->load->library('form_validation');

        $this->load->library('pagination');     

    }

    public function editMember() {
        $mem_id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_STRING);
        echo $mem_id;
    }
}

The url used to call the editMember function is like the one below.

https://mypage.com/member/editMember?id=mem001

The echo statement displays nothing.

When I try to get the input parameter using $_GET['id'], it works and echo statement shows mem001 as in the example link above. If I use CodeIgniter's input class as below, it works.

$mem_id = $this->input->get('id');
echo $mem_id;

Could you please help on what is going wrong and how to fix this? Thanks.

Here is var_dump($_SERVER['QUERY_STRING']) result.

string(9) "id=mem001"
1

There are 1 answers

2
Ravi Makwana On
public function editMember() {
if (isset($_GET["id"])) { 
if (!filter_input(INPUT_GET,'id', FILTER_SANITIZE_SPECIAL_CHARS) { 
    $mem_id = filter_input(INPUT_GET,'id', FILTER_SANITIZE_SPECIAL_CHARS);
    echo $mem_id;
} else { 
    $mem_id = filter_input(INPUT_GET,'id', FILTER_SANITIZE_SPECIAL_CHARS);
    echo $mem_id; 
} 
} 
}