Category ArchivePHP
LINUX & PHP & 技术 23 Oct 2007 11:24 am
apache关闭某目录的PHP解析
可以在
php_flag engine off
可以关闭对某个目录的php解析,起到保护的作用。
关闭后php文件将会变成可下载的纯文本文件,所以请小心使用。
PHP & linux 维护优化 & 技术 26 Sep 2007 02:02 pm
php session 多级目录
这是一个提高大量会话性能的好主意。
; 注意0: “N;[MODE;]/path”两边的双引号不能省略。
; 注意1: [MODE;]并不会改写进程的umask。
; 注意2: php不会自动创建这些文件夹结构。请使用ext/session目录下的mod_files.sh脚本创建。
; 注意3: 如果该文件夹可以被不安全的用户访问(比如默认的”/tmp”),那么将会带来安全漏洞。
; 注意4: 当N>0时自动垃圾回收将会失效,具体参见下面有关垃圾搜集的部分。
修改 php.ini的 session.save_path 选项,大致如下:
session.save_path = “1;/tmp/session”
php5无session.hash_bits_per_character时值为6
范围为 : 0-9, a-z, A-Z, “-”, “,”
建目录命令可参考php/ext/session/mod_files.sh
session回收命令
/usr/bin/find /tmp/session -name ’sess_*’ -type f -mmin +120 | /usr/bin/xargs /bin/rm -f
设一crontab
*/1 * * * * /bin/sh /opt/session_gc.sh
/sbin/service crond reload
================
最后注意crontab是没有环境变量的,命令需带路径
sh文件最好在linux上编辑,不要在win下写好再传,最后一个换行符导致 invalid option –
Try `/bin/rm –help’ for more information.浪费了我好多时间
LINUX & PHP & 技术 09 Aug 2007 09:40 am
带参数的php cron job
/usr/bin/curl ‘http://xxx.com/process.php?a=1&b=2&c=3′ > /dev/null
PHP5时区问题
使用php5后发现
echo date(”Y-n-j H:i:s”);
和本地相差了8小时
自PHP 5 >= 5.1.0RC1起
新增了两个函数
date_default_timezone_get()
date_default_timezone_set()
如果未设时区,则默认为UTC
解决方法1
$timezone_identifier = “Asia/Hong_Kong”;//北京时间是”PRC”
date_default_timezone_set($timezone_identifier);
解决方法2
设置php.ini
[Date]
; Defines the default timezone used by the date functions
date.timezone = “Asia/Hong_Kong”
重新启动apache
php使用strpos注意
- mystring = 'abc';
- $findme = 'a';
- if (strpos($mystring, $findme)){
- echo '找到';
- }else{
- echo '找不到';
- }
由于’a'是在第一个字符所以返回是’0′,’0′等于’false’所以显示’找不到’;
应使用以下方式
- mystring = 'abc';
- $findme = 'a';
- $pos = strpos($mystring, $findme);
- // Note our use of ===. Simply == would not work as expected
- // because the position of 'a' was the 0th (first) character.
- if ($pos === false) {
- echo "The string '$findme' was not found in the string '$mystring'";
- } else {
- echo "The string '$findme' was found in the string '$mystring'";
- echo " and exists at position $pos";
- }
wamp windows 下gd模块失效
用wamp自带的工具配置失效,因为他显示的php.ini和实际使用的是两个文件。
应当使用wamp\php\下的php.ini
用pear::pager产生html翻页
网站需要使用全静态页面进行翻页。
只需修改pager类的fileName和append两个option就可以实现
将list.php?page=2改为list_2.html
- fileName [string]
- name of the page, with a "%d" if append == TRUE.
- fixFileName [boolean]
- If set to FALSE, the fileName option is not overridden. Use at your own risk.
- append [boolean]
- If TRUE pageID is appended as GET value to the URL. If FALSE it is embedded in the URL according to fileName specs.
pear::pager文档
http://pear.php.net/manual/en/package.html.pager.factory.php
以下为函数
- /**
- * 获得html分页对象
- *
- * @param int $limit 每页显示数目
- * @param int $cnt 总数
- * @param int $filename 文件名格式
- * @return object
- */
- function get_html_pager($limit, $cnt,$filename='list_%d.html') {
- include_once('Pager/Pager.php');
- $options = array(
- 'perPage' => $limit,
- 'delta' => 10,
- 'url' => '',
- 'clearIfVoid' => true,
- 'urlVar' => 'Page',
- 'mode' => 'Jumping',
- 'totalItems' => $cnt,
- 'prevImg' => '上一页',
- 'nextImg' => '下一页',
- 'firstPageText' => '首页',
- 'lastPageText' => '末页',
- 'linkClass' => '',
- 'curPageLinkClassName' => 'strong',
- 'excludeVars' => array('Flush'),
- 'path' => './',
- 'fileName' => $filename,
- 'append' => false,
- //'spacesAfterSeparator' => 1,
- );
- $pager = Pager::factory($options);
- return $pager;
- }
windows下安装memcached
memcached官网
http://www.danga.com/memcached/
memcached windows官网
http://jehiah.cz/projects/memcached-win32/
memcached 1.2.0 for Win32为最新版,需libevent 1.2
Unzip the binaries in your desired directory (eg. c:\memcached)
Install the service using the command: ‘c:\memcached\memcached.exe -d install’ from either the command line
Start the server from the Microsoft Management Console or by running the following command: ‘c:\memcached\memcached.exe -d start’
Use the server, by default listening to port 11211
Use ‘memcached.exe -h’ for extra help and command line server
以后memcached将作为windows的一个服务每次开机时自动启动。
在php.ini 去掉 ‘extension=php_memcache.dll’前的注释
下载pecl的memcache模块包到ext目录
NOTE: php和pecl的版本要一致。
- <?php
- $memcache_obj = new Memcache;
- /* connect to memcached server */
- $memcache_obj->connect('localhost', 11211);
- /*
- set value of item with key 'var_key', using on-the-fly compression
- expire time is 50 seconds
- */
- $memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
- echo $memcache_obj->get('var_key');
- ?>
显示”some really big variable”就是成功了
php header 404
当使用cgi模式时使用
- header("Status: 404 Not Found");
mod_php使用
- header("HTTP/1.1 404 Not Found");
其中Not Found无意义,仅供显示。
万全的方法
- function header_status($status)
- {
- // 'cgi', 'cgi-fcgi'
- if (substr(php_sapi_name(), 0, 3) == 'cgi')
- header('Status: '.$status, TRUE);
- else
- header($_SERVER['SERVER_PROTOCOL'].' '.$status);
- }
- header_status('404 Not Found');
关于cookie中的数据读出时带斜杠
当magic_quotes_gpc=on。
所有从GET/POST/Cookie来的变量的单引号(’)、双引号(”)、反斜杠backslash()以及空字元NUL
(the null byte)都会被加上反斜杠
在文本框中输入一个单引号,显示会变成三个斜杠一个单引号
当你接收$_POSTt数据时自动加一个斜杠,然后取$_COOKIE又会加一次斜杠所以变成三个斜杠一个单引号.
你需要在存和取时根据magic_quotes_gpc各去一次斜杠就可以显示原文本内容。