Ali Zia Ali Zia - 1 month ago 20
PHP Question

CodeIgniter giving 500 Internal Error just on 1 page

I uploaded my site on live. All website is working perfectly but 1 page is showing me

500 Internal Server Error
.

Can you tell me why?

This is my controller.



defined('BASEPATH') OR exit('No direct script access allowed');

class Game extends CI_Controller {

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

if (!$this->session->userdata('is_logged_in')) {
redirect('admin/login');
}

$this->load->model('game_model', 'game');
}

public function index() {
$this->load->helper('url');

$data['categories'] = $this->game->get_categories();

$this->load->view('admin/includes/header');
$this->load->view('admin/game_view', $data);
$this->load->view('admin/includes/footer');
}

public function ajax_list() {
$list = $this->game->get_datatables();

$data = array();
$no = $_POST['start'];
foreach ($list as $game) {
$no++;
$row = array();
$row[] = $game->title;
$row[] = $this->game->get_category_name_by_id($game->category_id);
$row[] = $game->rating;
if ($game->status == '0') {
$row[] = '<span class="label label-danger">Inactive</span>';
} else if ($game->status == '1') {
$row[] = '<span class="label label-success">Active</span>';
}
//add html for action
$row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_game(' . "'" . $game->id . "'" . ')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_game(' . "'" . $game->id . "'" . ')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';

$data[] = $row;
}

$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->game->count_all(),
"recordsFiltered" => $this->game->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}

public function ajax_edit($id) {
$data = $this->game->get_by_id($id);
echo json_encode($data);
}

public function ajax_add() {
$this->_validate();

$config['upload_path'] = './assets/game_images/';
$config['allowed_types'] = 'gif|png|jpg|jpeg';

$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$file = $this->upload->data();
$file_name = $file['file_name'];

if ($file_name == '') {
$data['error_string'][] = 'Please upload an image.';
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
} else {
$data['inputerror'][] = 'image';
$data['error_string'][] = strip_tags($this->upload->display_errors());
$data['status'] = FALSE;
echo json_encode($data);
exit();
}

$data = array(
'title' => $this->input->post('title'),
'iframe' => $this->input->post('iframe'),
'status' => $this->input->post('status'),
'category_id' => $this->input->post('category_id'),
'rating' => $this->input->post('rating'),
'image' => $file_name
);
$insert = $this->game->save($data);
echo json_encode(array("status" => TRUE));
}

public function ajax_update() {
$this->_validate();

if ($_FILES['image']['name']!=''){

$config['upload_path'] = './assets/game_images/';
$config['allowed_types'] = 'gif|png|jpg|jpeg';

$this->load->library('upload', $config);

if ( ! $this->upload->do_upload('image')){
$data['inputerror'][] = 'image';
$data['error_string'][] = strip_tags($this->upload->display_errors());
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
else {
$this->game->delete_image_by_id($this->input->post('id'));
$file = $this->upload->data();
$file_name = $file['file_name'];
}
} else {
$file_name='';
}

if ($file_name==''){
$data = array(
'title' => $this->input->post('title'),
'iframe' => $this->input->post('iframe'),
'status' => $this->input->post('status'),
'rating' => $this->input->post('rating'),
'category_id' => $this->input->post('category_id')
);
} else {
$data = array(
'title' => $this->input->post('title'),
'iframe' => $this->input->post('iframe'),
'status' => $this->input->post('status'),
'category_id' => $this->input->post('category_id'),
'rating' => $this->input->post('rating'),
'image' => $file_name
);
}

$this->game->update(array('id' => $this->input->post('id')), $data);
echo json_encode(array("status" => TRUE));
}

public function ajax_delete($id) {
$this->game->delete_by_id($id);
echo json_encode(array("status" => TRUE));
}

private function _validate() {
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;

if ($this->input->post('title') == '') {
$data['inputerror'][] = 'title';
$data['error_string'][] = 'Game Title is required';
$data['status'] = FALSE;
}

if ($this->input->post('iframe') == '') {
$data['inputerror'][] = 'iframe';
$data['error_string'][] = 'Game Iframe is required';
$data['status'] = FALSE;
}

if ($this->input->post('status') == '') {
$data['inputerror'][] = 'status';
$data['error_string'][] = 'Status is required';
$data['status'] = FALSE;
}

if ($this->input->post('rating') == '') {
$data['inputerror'][] = 'rating';
$data['error_string'][] = 'Rating is required';
$data['status'] = FALSE;
}

if ($this->input->post('category_id') == '') {
$data['inputerror'][] = 'category_id';
$data['error_string'][] = 'Please select category';
$data['status'] = FALSE;
}

if ($data['status'] === FALSE) {
echo json_encode($data);
exit();
}
}

}

Answer

You are using short array tag in your code for production.

<?php echo form_upload(['name' => 'image', 'id' => 'image']); ?>

You just need to use like simply:

<?php echo form_upload(array('name' => 'image', 'id' => 'image')); ?>

Your PHP version is not allowed you to use php short tag array.


Update:

Adding comment of @Simba: You need to upgrade your php version urgently -- short array format has been around since PHP 5.4. If your php doesn't support it, then it means you're on a very old and unsupported version. You need to upgrade to avoid security issues. Being able to use modern php features like short array format will be a bonus, but the security worries of running an old version should be more important.

Comments