| 제목 | Ajax 전송 시 csrf 설정 및 internal 500 error 관련 질문드립니다. | ||
|---|---|---|---|
| 카테고리 | CI 2, 3 | ||
| 글쓴이 | bsc0227 | 작성시각 | 2018/03/06 17:26:07 |
|
|
|||
|
안녕하세요. Codeigniter 공부중인 한 학생입니다. 제가 Ajax로 컨트롤러에 데이터를 전송하고싶어 config.php에서 csrf 설정을 하고 view 부분의 ajax코드에서 data 부분에 <?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash(); ?>',를 추가해줬습니다. 하지만 Ajax 전송 시 500 에러가 발생하는데요, 이게 왜 발생하는건가요? config.php와 view 그리고 controller 소스를 첨부하겠습니다. <config.php> .... $config['global_xss_filtering'] = TRUE; /* |-------------------------------------------------------------------------- | Cross Site Request Forgery |-------------------------------------------------------------------------- | Enables a CSRF cookie token to be set. When set to TRUE, token will be | checked on a submitted form. If you are accepting user data, it is strongly | recommended CSRF protection be enabled. | | 'csrf_token_name' = The token name | 'csrf_cookie_name' = The cookie name | 'csrf_expire' = The number in seconds the token should expire. | 'csrf_regenerate' = Regenerate token on every submission | 'csrf_exclude_uris' = Array of URIs which ignore CSRF checks */ $config['csrf_protection'] = TRUE; $config['csrf_token_name'] = 'csrf_token_ci'; $config['csrf_cookie_name'] = 'csrf_cookie_name'; $config['csrf_expire'] = 7200; $config['csrf_regenerate'] = TRUE; $config['csrf_exclude_uris'] = array(); ....
<view>의 ajax 부분 ....
$('#comment_submit').click(function(){
$.ajax({
url: '/index.php/board_comment/ajax_comment_add',
type: 'POST',
data: {
<?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash(); ?>',
comment_contents: encodeURIComponent($('#input01').val()),
board_id: '<?php echo $this->uri->segment(3); ?>'
},
dataType: 'html',
complete: function(xhr, textStatus){
if(textStatus == 'success'){
if(xhr.responseText == 1000){
alert('댓글 내용을 입력하세요.');
}else if(xhr.responseText == 2000){
alert('댓글을 다시 업력해주세요.');
}else if(xhr.responseText == 9000){
alert('댓글을 입력하시려면 로그인이 필요합니다.');
}else{
alert($('#comment_area').html());
$('#comment_area').html(xhr.responseText);
$('#input01').val('');
}
}
}
})
});
.....
<controller 부분>
public function ajax_comment_add(){
if(@$this->session->userdata('logged_in') == TRUE){
// 로그인 되어있을 시
$table = 'board_comment';
$board_id = $this->input->post('board_id',TRUE);
$comment_contents = $this->input->post('comment_contents',TRUE);
if($comment_contents != ''){
//먼저 board_id로 달린 댓글이 있는지 체크
//있다면 가장 최신의 댓글의 comment_id를 가져옴
$arrays = array(
'table' => $table,
'board_id' => $board_id
);
$latest_comment_id_result = $this->board_model->get_latest_comment_id($arrays);
if($latest_comment_id_result){
$latest_comment_id = $latest_comment_id_result + 1;
}else{
$latest_comment_id = 1;
}
$write_data = array(
'table' => $table,
'board_id' => $board_id,
'comment_id' => $latest_comment_id, //board_id + comment_id 카운트
'user_id' => $this->session->userdata('user_id'),
'comment_contents' => $comment_contents,
;
);
$result = $this->board_model->insert_comment($write_data);
if ($result) {
$sql = "SELECT * FROM ". $table ." WHERE board_id = '". $board_id . "' ORDER BY comment_id DESC";
$query = $this -> db -> query($sql);
?>
<table cellspacing="0" cellpadding="0" class="table table-striped">
<tbody>
<?php
foreach ($query -> result() as $lt) {
?>
<tr>
<th scope="row">
<?php echo $lt -> user_id;?>
</th>
<td><?php echo $lt -> contents;?></td>
<td>
<time datetime="<?php echo mdate("%Y-%M-%j", human_to_unix($lt->reg_date));?>">
<?php echo $lt -> reg_date;?>
</time>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
// 글 실패시
echo "2000";
}
} else {
// 글 내용이 없을 경우
echo "1000";
}
} else {
// 로그인 필요 에러
echo "9000";
}
}
}
}
.....
<오류 사진> https://s3.ap-northeast-2.amazonaws.com/ohrora.com/%EC%A0%9C%EB%AA%A9+%EC%97%86%EC%9D%8C.png
조언 부탁드립니다. 감사합니다. |
|||
| 태그 | ajax,csrf | ||
| 다음글 | 코드이그나이터 db연결 부분에서 500 에러가 발생합니... (5) | ||
| 이전글 | 멀티도메인 관련 소스 입니다. 맞게 사용하고 있는지.. (2) | ||
|
변종원(웅파)
/
2018/03/06 17:33:00 /
추천
1
|
|
bsc0227
/
2018/03/06 17:36:05 /
추천
0
안녕하세요 웅파님. 조언 감사드립니다. 제가 많이 초보라서그런데 혹시 ci log를 어디에서 볼수 있는지 여쭤봐도 될까요..? |
|
변종원(웅파)
/
2018/03/06 17:44:21 /
추천
0
config.php 에서 log로 검색해서 0->4로 바꾸고 application/logs 디렉토리 확인하시면 됩니다. 매뉴얼 아래부분 읽어보세요. http://www.ciboard.co.kr/user_guide/kr/general/errors.html |
|
bsc0227
/
2018/03/06 17:52:12 /
추천
0
감사합니다 웅파님! 해결했습니다 ㅎㅎ 근데 추가적으로 더 여쭤봐도될까요? Ajax 전송 후 다시 F5를 눌러 브라우저 리프레시를 한 후 다시 시도했을때에는 또 500에러가 발생합니다. 이건 csrf 코드가 아직 안바뀌어서 그런건가요? |
|
변종원(웅파)
/
2018/03/06 22:07:06 /
추천
1
데이터가 없어서겠죠. 리프레시 한다고 원래 데이터가 가는게 아닐테니까요.
|
이런 상황에서 500 에러는 대부분 db관련 에러입니다.
ci log 확인해보시고 모델의 쿼리를 출력해서 콘솔에서 실행해보세요.