PHP Learn

PHP Operator ===

PHP’s === Operator enables you to compare or test variables for both equality and type. Refer below for example. [code] <?php //define variables.. $str = '9'; $int = 9; //Returns true since both variable contains the same value.. $res = ($str==$int); //Returns false since the two variables are not of the same type.. $res = ($str===$int); ?> [/code]

Create a REST API with PHP

One of the latest (sort of) crazes sweeping the net is APIs, more specifically those that leverage REST. It’s really no surprise either, as consuming REST APIs is so incredibly easy… in any language. It’s also incredibly easy to create them as you essentially use nothing more than an HTTP spec that has existed for ages. One of the few things that I give Rails credit for is its well thought-out REST support, both for providing and consuming these APIs (as its been explained by all the R...

ThinkPHP中实现gzip压缩

ThinkPHP中似乎没有对gzip的配置选项。不过实现起来还是挺简单的。以下是对 ThinkPHP 2.1 的修改方法。 首先要确定空间是否支持Zlib, 然后找ThinkPHP的控制输出函数output()。 控制输出的文件在 ThinkPHP/Lib/Think/Core 文件夹下,文件名 View.class.php。 [code] /** +---------------------------------------------------------- * 输出模板 +---------------------------------------------------------- * @access protected +---------------------------------------------------------- * @param string $content 模板内容 * @param boolean $display 是否直接显示...

PHP断点续传 HTTP学习笔记

HTTP断点续传原理是这样的: 1. 客户端需要告诉服务器端从哪里开始。 2.服务端收到请求,返回206状态。并标识续传的起始点及结束点 如下实例 1. 客户端传递请求信息给web服务器,要求从200070字节开始。。 GET /down.zip HTTP/1.1 User-Agent:NetFox RANGE: bytes = 200070- Accept:text/html,image/gif,image/jpeg,*;q=.2,*/*;q=.2 2.服务端收到这个请求以后,返回信息 206 Content-Length = 100222222 Content-Range = bytes 200070 - 100222221/100222222 Content-Type=application/octet-stream 注意:服务端状态 206; Content-Range = bytes (客户端请求续传起始点) - (下载文件大小-1)/(下载文件大小) 在PHP中,是利用$_SERVER['HTTP-RANGE...

PHP Array List of English Stop Words

Stop Words are words which do not contain important significance to be used in Search Queries. Usually these words are filtered out from search queries because they return vast amount of unnecessary information. A better definition is provided below: “Words that do not appear in the index in a particular database because they are either insignificant (i.e., articles, prepositions) or so common that the results would be higher than the system can handle (as in the case of IUCAT where terms suc...

9 Useful PHP Functions and Features You Need to Know

1. Functions with Arbitrary Number of Arguments You may already know that PHP allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments. First, here is an example with just optional arguments: [code] // function with 2 optional arguments function foo($arg1 = '', $arg2 = '') { echo "arg1: $arg1\n"; echo "arg2: $arg2\n"; } foo('hello','world'); /* prints: arg1: hello ...

ThinkPHP示例之 字段映射

要使用模型的自动创建create方法的话,ThinkPHP的表单名称就是数据表的字段名称,如果担心这样不够安全,可以定义字段映射,来隐藏实际的数据表字段名称。 下面的例子就运用了字段映射定义。请注意看表单名称和字段的对应关系。 标题: 邮箱: 内容: 验证码: 输入对应的数字 示例源码 控制器IndexAction类 [code] <?php class IndexAction extends Action{ // 首页 public function index(){ $Form = D("Form"); $list = $Form->findAll(); $this->assign('list',$list); $this->display(); } // 处理表单数据 public funct...

ThinkPHP的html:list标签简要用法及注意事项

<html:list id="checkList" name="user" style="list" checkbox="true" action="true" datasource="list" show="id:编号|8%,title:标题:edit,content:内容,create_time|toDate='Y-m-d H#i#s':添加时间,status|getStatus:状态" actionlist="forbid|resume:禁用|恢复,edit:编辑" /> ThinkPHP的html标签库中的list标签功能强大,但没有文档.只好看源码了. 大概如下: datasource和show必须要, datasource是数据源名称, 也就是assign所指向的变量, show是最复杂的, 等会再讲 pk是主键名, 默认为id;style是css样式的class, 因为整个list标签会被编译成table, 比如这里为mylist, 那么样式表中的table.mylist样式就会用在这; checkbox是否显示复选框; action是否显示操作列 s...

ThinkPHP与RBAC(基于角色的权限管理)

访问控制可以简单表述为:判断谁(Who)对什么(What/Which)进行怎样(How)的操作是否为真。对于一个系统来说,有必要建立一个良好的访问控制系统,对访问权限进行合理的分配,用于保证系统的安全性、可靠性。 传统的访问控制技术主要有:自主访问控制(DAC)、强制访问控制(MAC)和基于角色的权限访问控制(Role Based Access Control,简称RBAC)。 DAC和MAC访问控制技术均是直接对用户本身进行权限的管理,细度太小。当用户数量庞大并且用户之间关系复杂时,主体和客体关系的匹配及权限的管理就变得复杂起来。并且权限的变更将导致权限分配列表的变更,此时可能会遭遇到很大的困难,甚至于要修改系统的源代码。 而RBAC访问控制技术很好地解决了这一问题。在RBAC中,用户的权限不是在用户本身上进行管理的,用户的权限是由用户所处的角色所决定的。在权限管理中,通过角色这一桥梁将用户与权限联系起来。用户和角色、角色与权限是一个多对多的关系。 与RBAC访问控制相关的概念有: 用户(User):一个具有唯一标识符的用户,与权限相分离,只能通过所属的R...

为ThinkPHP添加web services

给ThinkPHP应用程序添加web services,首先需要启动soap模块,这里仅需要修改php.int文件即可,extension=php_soap.dll 接着编写服务类。这里单独建立一个Webserver文件夹,把所有的web服务类都放置在里面。这样做的好处不言而喻,一方面为了组织的方便,另一方面可以实现团队协作开发,相互之间不会有过多影响。如下图所示: 在Webserver文件夹下面,我们可以编写各种业务逻辑代码,这里仅是演示之用,所以非常简单。当然了,你还可以在里面实现更为复杂的逻辑。 [code] class Printers extends Base { public function printme() { return 'Hello'; } } [/code] 现在,我们就可以实现web服务端了。在IndexAction.class.php文件里面编写一段web服务端代码,启动web服务。 [code] class IndexAction extends Action{ public func...