PHP CI 로그 레벨별 파일 분리 하는 방법

Progmming/PHP|2023. 1. 19. 11:23
반응형

libraries/Log.php

// 아마도 상단에 이렇게 선언이 되어있을텐데..
protected $_levels = array('ERROR' => '1', 'DEBUG' => '2',  'INFO' => '3', 'ALL' => '4');

// $level 을 추가하면 기존 log연월일.php 가 ERROR-연월일.php로 바뀌게 됨. 에러 레벨별로 로그 분리
$filepath = $this->_log_path.$level.'-'.date('Y-m-d').'.php';

 

 

 

https://cikorea.net/bbs/view/lecture?idx=7113 

 

[게임서버] 로그파일 분리하기

CI 기본은 log-년-월-일.php와 같이 단일파일로 로그가 저장이 됩니다. 단일파일로 로그가 합쳐져 있을경우 분석시간이 많이 걸리기 때문에 분리를 하게 됩니다. (예 : 결제로그를 별도로 분리해서

cikorea.net

 

댓글()

PHP 보안코딩 - Prepared Statements MYSQL 샘플 소스

Progmming/PHP|2019. 2. 13. 15:35
반응형

Prepared Statements MYSQL 샘플 소스코드

기본적으로 보안을 위해 사용한다고만 알고있었으나 


w3schools 자료를 보면 Prepared Statements 는 3가지 장점을 갖습니다.


  1. 준비된 명령문은 쿼리 준비가 한 번만 수행되므로 구문 분석 시간을 줄입니다 (명령문이 여러 번 실행 되더라도)

  2. 바인딩 된 매개 변수는 전체 쿼리가 아닌 매번 매개 변수 만 보내야하므로 서버 대역폭을 최소화합니다.

  3. Prepared statements는 나중에 다른 프로토콜을 사용하여 전송되는 매개 변수 값이 올바르게 이스케이프 될 필요가 없으므로 SQL injection에 매우 유용합니다. 원래 명령문 템플리트가 외부 입력에서 파생되지 않으면 SQL 삽입이 발생할 수 없습니다.
$dbconn = new mysqli("디비 주소", "아이디", "비번", "디비명");

if ($dbconn->connect_errno) {
    echo "Failed to connect to MySQL: (" . $dbconn->connect_errno . ") " . $dbconn->connect_error;
}

if (!$dbconn->query("DROP TABLE IF EXISTS test") ||
    !$dbconn->query("CREATE TABLE test(id INT, label CHAR(1))") ||
    !$dbconn->query("INSERT INTO test(id, label) VALUES (1, 'a')")) {
    echo "Table creation failed: (" . $dbconn->errno . ") " . $dbconn->error;
}


$stmt = $dbconn->prepare("select a,b,c from tbl where a like CONCAT('%',?,'%') ");
$id = "abc";

$stmt->bind_param('s', $id);
$stmt->execute();

$stmt->bind_result($a, $b, $c);


//$stmt->fetch();  

while ($stmt->fetch()) {
	echo $a ."//".$b."//".$c;
}


$stmt->close();
$dbconn->close();



참고 사이트 : 

https://www.w3schools.com

http://php.net

https://stackoverflow.com

https://ko.wikipedia.org

댓글()

PHP + Google Translate API 연동하기

Progmming/PHP|2018. 5. 4. 13:19
반응형

PHP + Google Translate API 연동하기



Google translate document site

https://cloud.google.com/translate/docs/?hl=ko



Google 에서 제공되는 샘플 소스

https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate


 

하지만 그냥 최소한의 코딩으로 처리를 하고자 했기 때문에..


php 함수를하나 만듬.


function translate($content) { $handle = curl_init(); curl_setopt($handle, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2'); curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); $data = array('key' => "API Key", 'q' => $content, 'source' => "ko", 'target' => "en"); curl_setopt($handle, CURLOPT_POSTFIELDS, preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', http_build_query($data))); curl_setopt($handle,CURLOPT_HTTPHEADER,array('X-HTTP-Method-Override: GET')); $response = curl_exec($handle); $responseDecoded = json_decode($response, true); $responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); curl_close($handle); return $responseDecoded['data']['translations'][0]['translatedText']; }


사용을 위해 신용카드 등록이 필요하지만 처음 등록 할 경우 300달러가 제공되니 참고해주세요.

댓글()