Shah Rushabh Shah Rushabh - 25 days ago 10
PHP Question

How can I allow controller access only for form submission?

I have one controller in CodeIgniter. I didn't want to allow anyone to access controller. this controller can be accessed only for the form submission. How can I do it? below is given controller code.

public function editad($uniqueid = ''){
$this->load->model('Ads');
$this->load->model('Postingad');
$data =
if($uniqueid == ''){
show_404();
}
$checkbox = $this->Ads->products($uniqueid);
$product = json_decode($this->input->post('products'));
$checked = $this->input->post('product_category');
for($i = 0; $i < count($checkbox); $i++){
$checkboxes[] = $checkbox[$i]['Product'];
}
for($i = 0; $i < count($checkbox); $i++){
if(!in_array($checkbox[$i]['Product'], $product)){
if($checkbox[$i]['Image'] != 'http://wallpapercraze.com/images/wallpapers/nowallpaper-585747.jpeg'){
unlink('./uploads/'.$checkbox[$i]['Image']);
}
$this->Ads->deletecheckbox($uniqueid,$checkbox[$i]['Product']);
}
}
$files = array(
'electronics'=>'efile',
'kitchen'=>'kfile',
'decoratives / Interior'=>'dfile',
'Home Decor'=>'hdfile',
'Furnitures'=>'ffile',
'Toys'=>'tfile',
'Vehicles'=>'vfile',
'Other'=>'ofile'
);
$config['upload_path'] = './uploads/'.$uniqueid;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library("upload", $config);
for($i = 0; $i < count($checked); $i++){
$filenamefield = $files[$checked[$i]];
if($_FILES[$filenamefield]['size'] > 0){
if($this->upload->do_upload($filenamefield)){
$upload_data = $this->upload->data();
$filename = $upload_data['file_name'];
$checkboxinfo = array(
'UniqueID'=>$uniqueid,
'Product'=>$checked[$i],
'Image'=>$uniqueid.'/'.$filename
);
$this->Postingad->checkbox($checkboxinfo);
}
}
else{
$checkboxinfo = array(
'UniqueID'=>$uniqueid,
'Product'=>$checked[$i],
'Image'=>'http://wallpapercraze.com/images/wallpapers/nowallpaper-585747.jpeg'
);
$this->Postingad->checkbox($checkboxinfo);
}
}
echo '<pre>';
print_r($upload_data);
echo '</pre>';
}

Answer

Start your function name with a _.

In some cases you may want certain functions hidden from public access. To make a function private, simply add an underscore as the name prefix and it will not be served via a URL request.

Read more about this in this similair question: CodeIgniter - Private functions

public function editad($uniqueid = ''){ should be public function _editad($uniqueid = ''){

Comments