csvファイルのダウンロード

2010/08/31 – 6:15 PM

VIA:CakePHP1.2 を使って簡単に Csv ダウンロードを実装する方法
VIA:CSV Helper (PHP 5)
続きを読む »

PHP5.3でエラー「Deprecated: Assigning the return ・・・」

2010/08/27 – 4:59 PM

VIA:Deprecated: Assigning the return value of new by reference is deprecated in

同様のエラーが出たのでメモ

Windowsの開発環境
Apache2.2
PHP5.3
の環境化で、Cakephpを試していたら下記のエラーがでました。

Deprecated: Assigning the return value of new by reference is deprecated in

下記のURLで調査したところ、PHP5.3の環境だとでるようです。
http://cakephp.jp/modules/newbb/viewtopic.php?topic_id=1993&forum=3
下記のファイルにif文を追加しました。
「cake/libs/configure.php」

if (isset($config['debug'])) {
 if ($_this->debug) {
  error_reporting(E_ALL);
  この三行追加
  if (error_reporting() > 6143) {
   error_reporting(E_ALL & ~E_DEPRECATED);
  }
  ここまで
  if (function_exists('ini_set')) {
   ini_set('display_errors', 1);
  }

App外に共通で使用するComponentやHelperをPluginとして置く

2010/08/27 – 2:00 PM

VIA:第4回CakePHP勉強会@TokyoのLTで発表してきました!

具体的には、
/path/to/plugins/tools/controllers/components/hoge.php
var $components = array(‘Tools.Hoge’);
/path/to/plugins/tools/views/helpers/hoge.php
var $helpers = array(‘Tools.Hoge’);
/path/to/plugins/tools/models/behaviors/hoge.php
var $actsAs = array(‘Tools.Hoge’);
となります。

共通で使用できるものは一緒にした方が複数appから利用しやすいしメンテしやすい。

CakephpでSecurimage(captcha)を使う

2010/08/26 – 2:01 PM

携帯で、CAPTCHAを設定してみた。
「そこそこ見やすく、数字のみ」を基準にSecurimageKCAPTCHAを比較してみた。どちらもCakephpで使用した実績はあるらしい(Googleで検索すると出てくるので)
KCAPTCHAは標準でGoggleのCaptchaのようなグニャグニャ数字がでるらしいが、GoogleのChaptchaは個人的には見づらいと思う。
設定しだいでは見やすくなるのかもしれないが、ちょっと調べる時間がとれない。
と、いうことで、Securimageを使用。
続きを読む »

session idの長さ

2010/08/26 – 1:58 PM

VIA:CakePHP に use_trans_sid 風の処理を組み込む
VIA:PHPのSession IDの長さを変更する

if( preg_match('/^[0-9a-f]{32}$/', $sid) ){
    session_id($sid);
    $this->_needs_session_renew=true; //for Session Fixation
}

データベース接続情報を取得する

2010/08/26 – 1:58 PM

VIA:DB接続情報をDSN形式で取得
VIA:getDSN

/**
 * getDSN
 *  - Gets a DSN for the other apps to connect to
 *    the database independent from CakePHP
 */
function getDSN()
{
    $this->db =& ConnectionManager::getDataSource('default');
    $c = $this->db->config;
    return "{$c['driver']}://{$c['login']}:{$c['password']}@{$c['host']}/{$c['database']}";

} //end getDSN()

携帯キャリアによってrenderの処理を変える

2010/08/26 – 1:57 PM

VIA:携帯によって表示を変える

function render( $action = null, $layout = null, $file = null )
{
        if( !empty( $_SERVER["HTTP_USER_AGENT"] ) )
        {
                $ua     = $_SERVER["HTTP_USER_AGENT"];
                if( strpos( $ua, "DoCoMo" ) !== FALSE ){
                        $action = "i_" . ( $action ? $action : $this->action );
                        $layout = "i_" . $this->layout;
                }else if( strpos( $ua, "UP.Browser" ) !== FALSE ){
                        $action = "a_" . ( $action ? $action : $this->action );
                        $layout = "a_" . $this->layout;
                }else if(
                        strpos( $ua, "SoftBank" ) !== FALSE ||
                        strpos( $ua, "Vodafone" ) !== FALSE ||
                        strpos( $ua, "J-PHONE" ) !== FALSE ){
                        $action = "j_" . ( $action ? $action : $this->action );
                        $layout = "j_" . $this->layout;
                }
        }
        return parent::render( $action, $layout, $file );
}

1.2RCのLIKE,BETWEEN,INの書き方

2010/08/25 – 11:15 AM

VIA:1.2RCのLIKE,BETWEEN,INの書き方まとめ

$conditions = array("Post.title LIKE" => "%post%");
$conditions = array("Post.date BETWEEN ? AND ?" => array("2008-1-1", "2009-1-1"));
$conditions = array("Post.title LIKE ?" => array("%post%"));
$conditions = array("Post.id" => array(1,2,3,4,5));

VIA:3.7.3.7 複雑な find の条件

HTTP Response Headersの文字コード

2010/08/25 – 11:14 AM

VIA:PHPで出力する際にHTTP Response Headers で違う文字コードが指定される

HTTP Response Headers Content-Type が 目的の文字コードと違う場合は
出力文字列上でいくら

<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">

と書いてもダメ。

php.iniのdefault_charset が = “EUC-JP” などになっているので
デフォルトの = “” にするか目的の文字コードへ変える。
(例えば = “SJIS” など)

或いは

header("Content-type: text/html; charset=Shift_JIS");

をページ毎に出力する。

または.htaccessファイルに

php_value default_charset SJIS(←目的の文字コード)

php_value default_charset none

と書くべし。

コメントスパム

2010/08/24 – 7:25 PM

VIA:ppBlog コメントスパム対策に四苦八苦

言語セットが日本語以外をはじく。

///// コメントスパム対策 //////
if(!ADMIN){
//httpの数が多いと弾く
if(substr_count(strtolower($c_com), 'http') >= 4){
  print "「http」が4つ以上ある文章は書き込めません";
  exit;
}
//携帯はチェックしない
if(!preg_match('/docomo|up\.browser|j\-phone|softbank|vodafone|pdxgw|astel|l\-mode/i',$_SERVER['HTTP_USER_AGENT'])){
  //記事のURL以外からは拒否
  if(!stristr($_SERVER['HTTP_REFERER'], ROOT_PATH.'index.php?UID='.$UID)){
    print "どこからコメントしてるの?";
    exit;
  }
  // ブラウザの言語設定で、日本語が含まれていないと拒否
  // 入力内容に日本語が入っていても、弾く
  if(!stristr($_SERVER["HTTP_ACCEPT_LANGUAGE"], "ja")){
    print "鎖国してます";
    exit;
  }
}}