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

PHP & 技术 21 May 2007 04:26 pm

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 & 技术 14 May 2007 02:19 pm

php使用strpos注意

  1. mystring = 'abc';
  2. $findme   = 'a';
  3.  
  4.  
  5. if (strpos($mystring, $findme)){
  6.     echo '找到';
  7. }else{
  8.     echo '找不到';
  9. }

由于’a'是在第一个字符所以返回是’0′,’0′等于’false’所以显示’找不到’;

应使用以下方式

  1. mystring = 'abc';
  2. $findme   = 'a';
  3. $pos = strpos($mystring, $findme);
  4.  
  5. // Note our use of ===.  Simply == would not work as expected
  6. // because the position of 'a' was the 0th (first) character.
  7. if ($pos === false) {
  8.     echo "The string '$findme' was not found in the string '$mystring'";
  9. } else {
  10.     echo "The string '$findme' was found in the string '$mystring'";
  11.     echo " and exists at position $pos";
  12. }

PHP & 技术 25 Apr 2007 11:36 am

wamp windows 下gd模块失效

用wamp自带的工具配置失效,因为他显示的php.ini和实际使用的是两个文件。
应当使用wamp\php\下的php.ini

PHP & 技术 09 Apr 2007 03:44 pm

用pear::pager产生html翻页

网站需要使用全静态页面进行翻页。
只需修改pager类的fileName和append两个option就可以实现
将list.php?page=2改为list_2.html

  1. fileName [string]
  2.  
  3. name of the page, with a "%d" if append == TRUE.
  4.  
  5. fixFileName [boolean]
  6.  
  7. If set to FALSE, the fileName option is not overridden. Use at your own risk.
  8.  
  9. append [boolean]
  10.  
  11. 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

以下为函数

  1. /**
  2. * 获得html分页对象
  3. *
  4. * @param    int     $limit      每页显示数目
  5. * @param    int     $cnt        总数
  6. * @param    int     $filename   文件名格式
  7. * @return   object
  8. */
  9. function get_html_pager($limit, $cnt,$filename='list_%d.html') {
  10.     include_once('Pager/Pager.php');
  11.     $options = array(
  12.         'perPage'       => $limit,
  13.         'delta'         => 10,
  14.         'url'           => '',
  15.         'clearIfVoid'   => true,
  16.         'urlVar'        => 'Page',
  17.         'mode'          => 'Jumping',
  18.         'totalItems'    => $cnt,
  19.         'prevImg'       => '上一页',
  20.         'nextImg'       => '下一页',
  21.         'firstPageText' => '首页',
  22.         'lastPageText'  => '末页',
  23.         'linkClass'     => '',
  24.         'curPageLinkClassName'  => 'strong',
  25.         'excludeVars'   => array('Flush'),
  26.         'path'          => './',
  27.         'fileName'      => $filename,
  28.         'append'        => false,
  29.         //'spacesAfterSeparator'                => 1,
  30.     );
  31.     $pager = Pager::factory($options);
  32.     return $pager;
  33. }

PHP & 技术 15 Dec 2006 02:51 pm

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的版本要一致。

  1. <?php
  2. $memcache_obj = new Memcache;
  3.  
  4. /* connect to memcached server */
  5. $memcache_obj->connect('localhost', 11211);
  6.  
  7. /*
  8. set value of item with key 'var_key', using on-the-fly compression
  9. expire time is 50 seconds
  10. */
  11. $memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
  12.  
  13. echo $memcache_obj->get('var_key');
  14. ?>

显示”some really big variable”就是成功了

PHP & 技术 18 Nov 2006 01:34 pm

php header 404

当使用cgi模式时使用

  1. header("Status: 404 Not Found");

mod_php使用

  1. header("HTTP/1.1 404 Not Found");

其中Not Found无意义,仅供显示。

万全的方法

  1. function header_status($status)
  2. {
  3.  
  4.    // 'cgi', 'cgi-fcgi'
  5.    if (substr(php_sapi_name(), 0, 3) == 'cgi')
  6.        header('Status: '.$status, TRUE);
  7.    else
  8.        header($_SERVER['SERVER_PROTOCOL'].' '.$status);
  9. }
  10. header_status('404 Not Found');

PHP & 技术 27 Sep 2006 01:24 pm

关于cookie中的数据读出时带斜杠

当magic_quotes_gpc=on。
所有从GET/POST/Cookie来的变量的单引号(’)、双引号(”)、反斜杠backslash()以及空字元NUL
(the null byte)都会被加上反斜杠

在文本框中输入一个单引号,显示会变成三个斜杠一个单引号

当你接收$_POSTt数据时自动加一个斜杠,然后取$_COOKIE又会加一次斜杠所以变成三个斜杠一个单引号.

你需要在存和取时根据magic_quotes_gpc各去一次斜杠就可以显示原文本内容。

Next Page »