Tsea Tsea - 2 months ago 6
PHP Question

How do I only display data if query string is in the URL codeigniter

Im trying to only display data if the url has a query string.
In example
I want the data displayed at http://example.com/users only to me accessible by the correct query string

http://example.com/users?access-token=b393c6

This is my controller:

public function users() {
$users = $this->cd_model->selectAllusers();
$users_json = json_encode($users, true);
echo $users_json;
$this->load_view('/users');

}


and this is my model

public function selectAllusers() {
$sql = 'SELECT * FROM users';
$query = $this->db->query($sql);
return $query->result();
}

Answer

In controller just check either there is any query string parameters or not it could be through GET method or POST. If only Get required then use $this->input->get(null); instead of $this->input->get_post(null);

public function users() {
        $queryString = $this->input->get_post(null);
        if($queryString && !empty($queryString['access_token'])){
         $users = $this->cd_model->selectAllusers();
         $users_json = json_encode($users, true);
         echo $users_json;
        }
        $this->load_view('/users');

}
Comments