Codeigniter security check if uri is number, because of pagination - php

One thing in the security of codeigniter is checking if the uri is a number or something that we expect!
Here is the code:
$this->load->helper('form');
$this->load->library('pagination');
$config['permitted_uri_chars'] = '0-9';
$config['base_url'] = '/member/index';
$config['per_page'] = 5;
$config['num_links'] = 10;
$query_not_voted_rows = "SELECT p_id FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').")";
$config['total_rows'] = $this->db->query($query_not_voted_rows)->num_rows();
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
if($this->uri->segment(3) == '')
{
$segment_url = 0;
}else{
$segment_url = $this->uri->segment(3);
}
$query_not_voted = "SELECT * FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').") LIMIT ".$segment_url.", ".$config['per_page'];
But if now someone enters in uri segment: "kdkdkdd", then the sql breaks, because $segment_url is kdkdkdd and not the number!
So the question is, how to escape this?

This is my simple solution:
if($this->uri->segment(3) == '')
{
$segment_url = 0;
}else{
if(!is_numeric($this->uri->segment(3))){
redirect('404');
}else{
$segment_url = $this->uri->segment(3);
}
}
But if somebody have any better idea or tutorial on this please...

nice job =)
answer 1 +
<?
$cadena = "sdfio9874673o4h784";
for ($i=0; $i<strlen($cadena); $i++) {
if (is_numeric($cadena[$i])) {$cadena2.=$cadena[$i];}
}
echo $cadena2;
?>
= nice code

Related

pagination is not working in codeigniter

I am working in CodeIgniter. I want to create pagination for dynamic record. So I have write code like below:
$config = array();
$config["base_url"] = base_url() . "employer/search_user";
$config["per_page"] = 3;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$query = "select sn.*,u.first_name,u.last_name,u.email,u.phone,group_concat(DISTINCT s.skill) as sk,group_concat(DISTINCT c.name) as ct from
users as u
left join snapshot as sn on sn.user_id = u.user_id
left join industry as ind on ind.ind_id = sn.industry
left join city c ON(FIND_IN_SET(c.city_id, sn.current_location) > 0)
left join skill s ON(FIND_IN_SET(s.skill_id, sn.skill) > 0)
where ".$where." u.group_id = 3 group by u.user_id limit ".$config["per_page"];
$data['users'] = $this->admin_model->query($query);
$row = count($data['users']);
$config["total_rows"] = $row;
$config['num_links'] = $row;
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data['main_content']="employer/search_user";
$this->load->view('employer/template', $data);
Here, I got 3 records in page but pagination link is not display. I have include pagination library in constructor. I did not get anuthing in $data["links"] this variable.
So where I have to correct my code? What is wrong in my code?
You need to use LIMIT and OFFSET in your query. For example
$config['per_page'] = 3;
$config['uri_segment'] = 4; // This is the current page
$offset = 0;
if ($this->uri->segment($config['uri_segment']) != "") {
$offset = $this->uri->segment($config['uri_segment']);
}
$limit = $config['per_page'];
$sql = "SELECT `field_name` FROM `table_name` WHERE `table_name`.field_name = ?";
if($limit != "") {
$sql .= "LIMIT $limit ";
}
if($offset != "") {
$sql .= "OFFSET $offset";
}
$result = $this->db->query($sql, array($field_data));
Hope this helps
For $config["total_rows"] = $row;
You get only 3. Because You are setting limit in the query.
Kindly write another query for total rows without limit.I think this will solve Your problem.
Below is my pagination code
$config = array();
$config["base_url"] = base_url() . "crm/crm/contactmgmt/modid/" . $modid;
if($this->input->post('Search')=='Search'){
$data['pn']=$data1['pn']=$this->input->post('sel_prod');
//echo "hello";
}
$config["total_rows"] = $this->Crm_model->record_count_unsubinst();
$config["per_page"] = 6;
$this->load->config('pagination');
$config["uri_segment"] = 6;
$config['enable_query_strings']='true';
$data['activeTab'] = "View";
$this->pagination->initialize($config);
$page = ($this->uri->segment(6)) ? $this->uri->segment(6) : 0;
$data['query'] = $this->Crm_model->listUnsubscribedInstn($config["per_page"], $page);
$data["link"] = $this->pagination->create_links();
$data["links"]=$data["link"];
$data["cnt"]=$config["total_rows"];
$data["sno"]=$this->uri->segment(6);

Codeigniter pagination not worrking returns 404

Hi I'd like some help please. I 'm having two controllers: movies.php and actors.php where I list all actors and all movies in them.
For example this is the method for listing all my movies
public function index() {
// count all movies
$total = $this->movie_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['movies'] = $this->movie_model->get();
// load the view
$this->view('movies/index');
}
and for listing all actors
public function index() {
// count all actors
$total = $this->actor_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['actors'] = $this->actor_model->get();
// load the view
$this->view('actors/index');
}
I have set my routes like this
$route['default_controller'] = "movies";
$route['404_override'] = '';
$route['movies'] = 'movies/index';
$route['actors'] = 'actors/index';
And the urls are like this
h**p: //localhost/www/task/public/ // for movies (default controller)
h**p: //localhost/www/task/public/actors // for actors controller
The problem is when I try to click a panination link to get the next records in each controller I get a 404 error. I have tried to change my config settings in pagination but no luck.
Any help would be appreciated.
I have link here very help full for pagination and sortable table links this may help.
http://forum.codeigniter.com/thread-1198.html
Change $config['base_url'] = site_url($this->uri->segment(1) . '/'); to,
$config['base_url'] = base_url().'movies/index';
and
$config['base_url'] = base_url().'actors/index';
And $config['uri_segment'] = 3;

How to write Segment in pagination library code igniter

Mysql Segment ?? not understood properly
in pagination...
Here is my code:
$data['query']= $this->db->select('auto_id,title,image,thumb')
->get('gallery',$config['per_page']=12,$this->uri->segment(3));
where per_page=12
try to use like this->
function records($sort_by = 'rpd_id', $sort_order = 'asc', $offset = 0) {
$limit = 10;
$data['results'] = $this->rcbs_Model->get_all_data_from_request($limit, $offset, $sort_by, $sort_order);
$config = array();
$config['base_url'] = base_url("rcbs/records/$sort_by/$sort_order");
$config['total_rows'] = $this->rcbs_Model->count_records();
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$this->load->view('booking_data', $data);
}
here $sort_by = 'rpd_id' (rpd_id)is the default table column by which
data will be sorted.
"get_all_data_from_request" is the function by which you will get all
data from your table using select query.
base_url("rcbs/records/$sort_by/$sort_order"); rcbs -Controller name
'records'->function name itselft
$this->rcbs_Model->count_records() count all the records from the
table.

CodeIgniter pagination without tables

What i have right now is like 100 divs styled to be 4 in a row, want to add pagination to it, and i succeeded with this code:
public function func() {
$this->load->library('pagination');
$this->load->database('default');
$this->load->model('s_model');
$data['all_rows_s'] = $this->s_model->count_all_s();
$data['$total_s'] = $config['total_s'] = $this->s_model->count_all_ss();
$config['base_url'] = 'http://example.com/display/s/';
$config['total_rows'] = $data['all_rows_s'];
$config['per_page'] = 12;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="page_row">';
$config['full_tag_close'] = '</div>';
$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
$this->pagination->initialize($config);
$this->template->set_theme(Settings_model::$db_config['active_theme']);
$this->template->set_layout('main');
$this->template->title($this->lang->line('s'));
$this->process_partial('header', '/header');
$this->process_partial('footer', '/footer');
$this->process_template_build('s_view', $data);
}
But, this is working because WITHOUT table because of this line:
$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
but i need to make more filters into it, like order by id and where date > NOW(), how is this possible to do ? what ca i replace this get and the code to still work, and again the view is WITHOUT table, is divs within a foreach.
Thanks!
Try this:
Controller Function
function func() {
$this->load->library('pagination');
$this->load->database('default');
$this->load->model('s_model');
$data['all_rows_s'] = $this->s_model->count_all_s();
$data['$total_s'] = $config['total_s'] = $this->s_model->count_all_ss();
/*Pagination*/
$config['base_url'] = 'http://example.com/display/s/';
$config['total_rows'] = $data['all_rows_s'];
$config['per_page'] = 12;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="page_row">';
$config['full_tag_close'] = '</div>';
/*Pagination*/
//$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
$data['row'] = $this->s_model->get_records($config['per_page'], $this->uri->segment(3));
$this->pagination->initialize($config);
$this->template->set_theme(Settings_model::$db_config['active_theme']);
$this->template->set_layout('main');
$this->template->title($this->lang->line('s'));
$this->process_partial('header', '/header');
$this->process_partial('footer', '/footer');
$this->process_template_build('s_view', $data);
}
Model Function
function get_records($limit, $offset = 0){
return $this->db->where('date > NOW()')->order_by('id', 'asc')->get('s_data', $limit, $offset)->result();
}

Codeigniter Pagination

There is a bit of problem in my code which i am not able to solve.
I m using CI 1.7.2.
I have implemented the CI Pagination into the system correctly.
The results are displayed fine but the links in the pagination are not rendering correctly.
For eg. If i click on the page 2 then the results are displayed as per the 2nd Page but the current link at pagination numbers remains 1 which should change to 2.
Here is the code that has been implemented
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
$result = $this->bmodel->getAllBanners($perPage,$uri_segment);
$data['result'] = $result;
thanks in advance.
J
Heyy,
I also faced the same problem. In the end, solution turned out to be very simple. :)
by default CI assumes that uri segment used for pagination is (3). Which in your case, for you (i am assuming shamelessly) is incorrect.
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
$config['uri_segment'] = 3; /* segment of your uri which contains the page number */
$this->pagination->initialize($config);
Hope this solves your problem
ok... try this...
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
/* Comment out this part
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
*/
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
/*Change the following line*/
$result = $this->bmodel->getAllBanners($perPage,$this->uri->segment(5));
$data['result'] = $result;
$this->load->library('pagination');
$config['base_url']="http://localhost/CodeIgniter/pagination";
$config['per_page']=2;
$config['total_rows']= $this->db->get('record')->num_rows();
$this->pagination->initialize($config);
$data['query']= $this->db->get('record',$config['per_page'], $this->uri->segment(3));
$this->load->view('pagination',$data);
$config['uri_segment'] = num; /* where num is the uri segment where you have page number */
Try this, it might help.
class Admin_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_product($search, $page, $perpage) {
$page = $page - 1;
$page < 0 ? $page = 0 : $page = $page;
$from = $page * $perpage;
$query = $this->db
->select('*')
->from('tbl_product')
->limit($perpage, $from)
->get();
return $query->result();
}
}

Categories