标签 Codeigniter 下的文章

让codeigniter的oci8 db driver支持insert_id()

php_mysql模块有一个函数:mysql_insert_id

功能是

mysql_insert_id() 返回给定的 link_identifier 中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号。如果没有指定 link_identifier ,则使用上一个打开的连接。

说白了就是返回上一次insert的id号,但是oracle没有auto_increment属性,不能让字段自增,一般业内的解决方法是用sequence+trigger方案。今天需要取得oracle insert之后的id值,直接用上了codeigniter的$this->db->insert_id(),结果codeigniter报错提示:This feature is not available for the database you are using。我表示鸭梨很大,到底有好大?鸭梨山大:

/**
* Insert ID
*
* @access public
* @return integer
*/
function insert_id()
{
// not supported in oracle
return $this->display_error('db_unsupported_function');
}

既然都display_error了还返回的是interger。- -#
没办法codeigniter支持的数据库是主流的mysql,oracle是被抛弃的小孩。我们只能自己将其抚养成人了。
insert_id代码修改为:

function insert_id($tbl='')
{
$sql = "select " . $tbl . "_seq.currval as id from dual";
$statement = @ociparse($this->conn_id, $sql);
$row=@ociexecute($statement);
@ocifetchinto($statement, $row, OCI_ASSOC);
return $row['ID'];
}

加了一个$tbl参数,因为必须要传递$tbl,才能知道要查询的sequence,而且sequence要有一定的命名规范,比如:tablename_seq。

#167 codeigniter oci8 database driver do not support setting charset and session_mode while ocilogon() to oracle db

system/database/drivers/oci8/oci8_driver.php line 80 db_connect() function did not support set the charset and session mode while connecting to oracle db.

function db_connect()
    {
        return @ocilogon($this->username, $this->password, $this->hostname);
    }
function db_pconnect()
    {
        return @ocilogon($this->username, $this->password, $this->hostname);
    }

here is the prototype of oci_connect( ocilogon improved version )

resource oci_connect ( string $username , string $password [, string $db [, string $charset [, int $session_mode ]]] )

codeigniter最新的2.0.1太蛋疼鸟。system/database/drivers/oci8/oci8_driver.php里面的db_connect()和db_pconnect()函数尽然不支持连接的时候设定字符集,因为函数的原形ocilogon(被oci_connect代替)支持设定charset和session_mode。还有codeigniter为啥要使用@ocilogon而非oci_connect呢?php官方说5.0.0以前的版本才使用ocilogon,codeigniter为了使用ocilogon不报warning还前面加上了"@"符号。但是codeigniter声称是支持php 5.1.6以及以后版本的php框架。有点搞不懂codeigniter的想法了。 没有版本,这个问题,只有通过hard coding修改oci8_driver.php文件的db_connect()和db_pconnect()函数为:

function db_connect()
    {
        return @ociplogon($this->username, $this->password, $this->hostname, $this->char_set);
    }
function db_pconnect()
    {
        return @ociplogon($this->username, $this->password, $this->hostname, $this->char_set);
    }

已经将这个问题提交到

bitbucket

codeigniter配置oracle数据库连接

Connection Parameters

Not all of the parameters in application/config/database.php are used as one might expect.  Namely, $db[‘default’][‘database’] isn’t used at all.  The value used for $db[‘default’][‘hostname’] depends on whether the Oracle client’s tnsnames.ora file exists and contains information about the database to be used.  If the file exists and is configured for the intended database, this parameter should be set to the symbolic name.  Otherwise, it should be set to a single string of the connection parameters that tnsmames.ora would normally contain.

An example of connection parameters for the latter case:

$db['default']['hostname'] = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=orcl)))';
$db['default']['username'] = 'dbuser';
$db['default']['password'] = 'dbpassword';
$db['default']['database'] = ''; // not used by this Oracle driver
$db['default']['dbdriver'] = 'oci8';

In this case, the appropriate values need to be set for the HOST, PORT, and SID keywords in the hostname parameter.

CodeIgniter字符集编码问题

codeigniter里面的php文件都是ansi编码,其实我们在添加controller,model,view等php文件的时候,php文件的编码成了一个问题。其实这个关系不大。 如果真个项目采用utf-8,那么就将php文件的编码设置为utf-8 without bom,然后html模板文件meta写成:

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

mysql数据库统一字符集utf-8,校队:utf8_general_ci 就可以了,系统可以在“标准规范模式”正常浏览不出现乱码。 可能在后期处理上传的ms office文档的时候有转码的需要。

CodeIgniter用Xdebug调试的问题:The URI you submitted has disallowed characters.

The URI you submitted has disallowed characters.

最近玩codeigniter,使用netbeans写代码,调试器使用xdebug(貌似netbeans不支持zend debugger?之前用过的zend studio支持zend debugger),写了一个demo,然后ctrl+F5调试的时候,进过(F8)了几步之后出现如图上的问题:"The URI you submitted has disallowed characters.",因为codeigniter对$_GET、$_POST等外来参数有一个“白名单”的机制来加强了安全性,如果你的参数没有加入“白名单”,那肯定是有问题了。 而xdebug调试的时候自动加上了参数 XDEBUG_SESSION_START,所以肯定报错。

http://ci-study/index.php?XDEBUG_SESSION_START=tun-xdebug An Error Was Encountered The URI you submitted has disallowed characters.

在CI 2.0的/application/config/config.php Line 112有以下内容:

/*
|————————————————————————–
| Allowed URL Characters
|————————————————————————–
|
| This lets you specify with a regular expression which characters are permitted
| within your URLs. When someone tries to submit a URL with disallowed
| characters they will get a warning message.
|
| As a security measure you are STRONGLY encouraged to restrict URLs to
| as few characters as possible. By default only these are allowed: a-z 0-9~%.:_-
|
| Leave blank to allow all characters — but only if you are insane.
|
| DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!!
|
*/
$config['permitted_uri_chars'] =’a-z 0-9~%.:_\-‘;

CI采用了正则表达式的方式来匹配白名单,尽量少的匹配字符串意味着更高的安全性,默认是白名单正则表达式为a-z 0-9~%.:_-

解决方法1

放弃netbeans,使用eclipse+PDT、zend studio等可以使用zend debugger的PHP IDE。(为了一个小问题换IDE有点蛋疼的说)

解决方法2:

$config['permitted_uri_chars'] =’a-z 0-9~%.:_\-‘;

修改为:

$config['permitted_uri_chars'] =’a-z A-Z 0-9~%.:_\-‘;

实际production环境中最好还原到默认的permitted_uri_chars,提高安全性。