
ブログ引っ越しました。→ http://taramonera.net/2.5/
2011-11-10
CodeIgniter2.0.3 pg_escape_stringが変?
プログラミング, CodeIgniter | |
pg_escape_stringが変?
CodeIgniter2.0.3で文字(1文字)をpg_escape_stringすると、お尻に半角スペースが付いてくる。。
2文字以上だと問題ない。
わたしの開発環境だけなのだろうか。。。
postgre_driver.phpでpg_escape_stringは使われているので
とりあえずそのあとにtrimして対処。
2011-11-09
CodeIgniter2.0.3 postgresql使用時にクライアントエンコーディング(client_encoding)を設定する
プログラミング, CodeIgniter | |
postgresql使用時にクライアントエンコーディング(client_encoding)を設定する
CodeIgniterの文字コードがUTF-8で、postgresqlではEUC-JPなどを使用している場合、
CodeIgniterのpostgre_driverではclient_encodingを設定してくれてない。
system/database/drivers/postgre/postgre_driver.php
<?php /** * Set client character set * * @access public * @param string * @param string * @return resource */ function db_set_charset($charset, $collation) { // @todo - add support if needed return TRUE; }
todoのまま。。
ということで、下記のように変更。
<?php /** * Set client character set * * @access public * @param string * @param string * @return resource */ function db_set_charset($charset, $collation) { // @todo - add support if needed pg_set_client_encoding($this->conn_id , $this->char_set); return TRUE; }
2011-09-27
CodeIgniter2.0.3 postgresqlのエスケープ処理(like検索)にバグ
プログラミング, CodeIgniter | |
CodeIgniter2.0.3 postgresqlのエスケープ処理(like検索)にバグ
現象
Active Recordの$this->db->like();を使用したときに、
「%(パーセント)」「_(アンダースコア)」が2重にエスケープされる。
エスケープ文字が「!」の場合、
test_test
↓こうなる
test!!_test
ソースを確認
system/database/drivers/postgre/postgre_driver.php
の299行目
<?php $str = str_replace(array('%', '_', $this->_like_escape_chr), array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), $str); ?>
str_replaceは引数が配列の場合、左から順に処理していくので
この順番だとエスケープ文字($this->_like_escape_chr)が2個付く。
エスケープ文字を最初に置換するように修正。
<?php $str = str_replace(array($this->_like_escape_chr, '%', '_'), array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), $str); ?>
2011-07-20
CodeIgniter Shift-JISでPOSTされた値をUTF-8で受け取る
プログラミング, CodeIgniter | |
Shift-JISでPOSTされた値をUTF-8で受け取る
CodeIgniter内は、UTF-8
HTMLページは、Shift-JIS
という環境で、「accept-charset="utf-8"」を使わずにShift-JISでPOSTされた値をUTF-8で受け取る方法。
POSTが文字化け (Codeigniter-users) - CodeIgniter日本語化 - SourceForge.JP
によると、POSTされた値を
CI_Input内、CI_Utf8のclean_stringが呼ばれる前にUTF-8に変換しておく必要があるとのことなので、
CodeIgniterのフックを使って実装した。
フックの有効化
application/config/config.php
$config['enable_hooks'] = TRUE;//フック機能を有効に設定。
フックの定義
フックは application/config/hooks.php でファイルで定義する。
$hook['pre_system'] = array( 'class' => '', 'function' => 'convert_encoding', 'filename' => 'Convert_encoding.php', 'filepath' => 'hooks', );
これで、pre_system(システムの実行の最初)にapplication/hooks/Convert_encoding.phpのconvert_encodingメソッドが実行される。
文字コード変換を実装
application/hooks/Convert_encoding.php
<?php function convert_encoding() { if(isset($_POST)){ $_POST = _mbConvertEncodingEx($_POST, "UTF-8", "sjis-win"); } } /** * mb_convert_encoding()の拡張 * * @param mixed $target arrayかstring * @param string $toEncoding エンコード先 * @param string $fromEncoding エンコード元(default:null) * @return mixed arrayが来たらarrayを、stringが来たらstringを */ function _mbConvertEncodingEx($target, $toEncoding, $fromEncoding = null){ if (is_array($target)) { foreach ($target as $key => $val) { if (is_null($fromEncoding)) { $fromEncoding = mb_detect_encoding($val); } $target[$key] = _mbConvertEncodingEx($val, $toEncoding, $fromEncoding); } } else { if (is_null($fromEncoding)) { $fromEncoding = mb_detect_encoding($target); } $target = mb_convert_encoding($target, $toEncoding, $fromEncoding); } return $target; } ?>
$_POSTされたデータに配列も含まれる場合があるため、
配列をmb_convert_encodingするプログラム - せとっちの備忘録(技術系)
を利用させていただきました。
参考
2011-05-12
CodeIgniter 1.7.3 から 2.0.2へ移行してみた
サーバ, CodeIgniter | |
CodeIgniter 1.7.3 から 2.0.2へ移行してみた
1.7.3でとあるサービスを開発していたが、保守終了となるみたいなので、
開発初期段階だしサラッと2.0.2へ移行してみた。
『Webサイト制作者のためのPHP入門講座』の CodeIgniter アプリを CodeIgniter 2.0.0 に移行する
を参考に作業を行う。
ファイルの移動・置換
- systemディレクトリ内のapplicationディレクトリをsystemと同じ階層に移動
- systemディレクトリ内の残りのファイルとディレクトリを2.0.2のものに置き換える
ドキュメントルート直下にあるindex.phpを編集
ドキュメントルート直下にあるindex.phpには、
system_path(systemディレクトリへのパス)
application_folder(applicationディレクトリへのパス)
が設定されているので変更があれば変更する。
コントローラ、モデルが継承しているクラス名を変更
コントローラは CI_Controller を、モデルは CI_Model を継承するように変更されため
application/controllers
application/models
内のソースで該当の箇所があれば変更する。
class Hogehoge extends Controller ↓変更 class Hogehoge extends CI_Controller
class Hogehoge extends Model ↓変更 class Hogehoge extends CI_Model
親クラスのコンストラクタの呼び出し部分も変更する
parent::Controller(); ↓変更 parent::__construct();
parent::Model(); ↓変更 parent::__construct();
ページにアクセスして動作確認
私の環境では下記のエラーが出た
An Error Was Encountered In order to use the Session class you are required to set an encryption key in your config file.
CodeIgniter 2.0からは、設定ファイルの暗号化キーが設定されていないとこのエラーが出るため、
application/config/config.phpに暗号化キーを設定する
$config['sess_encrypt_cookie'] = TRUE; $config['encryption_key'] = "";//32文字のランダムな文字列を設定してください。
個人的対応
CodeIgniterでindex.phpを消すためのmod_rewrite設定 @さくらのレンタルサーバ その2
の設定をCodeIgniter 2.0.2に対応するため、
system/application/libraries/MY_Config.php
を
system/application/core/MY_Config.php
へ移動