php教程系统时区8小时差设置方法
修改php系统时区的两种方式:
1、修改php.ini文件中的设置,找到[date]下的“;date.timezone=”,将该项修改为date.timezone=asia/hong_kong (prc 中国时间),然后重新启动apache服务器。
2、在应用程序中,使用时间日期函数之前添加“date_default_timezone_set(“asia/hong_kong”)”函数
修改php.ini文件后获取本地时间:
php代码:
<?php
echo “现在是北京时间:”.date(“y-m-d h:i:s”).”<p>”;
?>
显示结果:
现在是北京时间:2010-11-30 20:50:03 (与本地时间相符)
php.ini修改后的代码:
[date]
; defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = prc
下面看看更多方法
1.在php.ini文件中修改设置。
[date]
//defines the default timezone used by the date functions
date.timezone = asia/shanghai
这里真是。。。可以设置shanghai,chongqin,hong_kong,taibei...等等,可就是没有找到beijing,不知道是什么原因。
2.在php程序中运行时设置。
<?php
if(date_default_timezone_get() != "1asia/shanghai") date_default_timezone_set("asia/shanghai");
?>
注意:用"date_default_timezone_get"获得的时间设置中有一个1,也不太清楚是杂回事呢。
3.在使用时间时自己多加8*3600秒也是行滴。
<?php
echo date("y-m-d h:i:s",time()+8*3600);
?>
注:
自php5.0开始,用php获取系统时间时,时间比当前时间少8个小时。原因是php.ini中没有设置timezone时,php是使用的utc时间,所以在中国时间要少8小时
php教程 正则匹配unicode 编码正则方法,因为unicode编码的特殊性,所以一般的中文或英文正则是不能正确取到我们想要的内容的,下面来看一款专业的unicode正则表达式吧。
看个unicode编码图。
$words = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrsruvwxyz!@#$%^&*()_+-=[],./{}|<>?'"你好啊我们";
$otherstr=preg_replace("//[x{0080}-x{00ff}]+/iu"," ",$words);
echo 'otherstr:',$otherstr;
装php教程扩展:windows 下的 memcache步骤
1.下载php的扩展dll:http://downloads.php.net/pierre/
2.将.dll放入php的扩展目录下面,一般在php目录中的ext 文件夹中。
3.修改apache目录下bin文件夹中的php.ini,加上extension=php_memcache.dll即可。
4.重启apache服务器,用phpinfo()查看,出现memcache说明配置正确.
值得注意的是:
必须注意php的编译版本,下载memcache的时候注意要使用同样的编译版本;比如php是使用vc6编译的,那么memcache也应该使用vc6编译版本
下面来看一篇在windows下载安装memcache php教程吧
windows下的memcache安装:
1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:memcached
2. 在终端(也即cmd命令界面)下输入 ‘c:memcachedmemcached.exe -d install’ 安装
3. 再输入: ‘c:memcachedmemcached.exe -d start’ 启动。note: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
4.下载php_memcache.dll,请自己查找对应的php版本的文件
5. 在c:winntphp.ini 加入一行 ‘extension=php_memcache.dll’
6.重新启动apache,然后查看一下phpinfo,如果有memcache,那么就说明安装成功!
memcached的基本设置:
-p 监听的端口
-l 连接的ip地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位mb。默认64mb
-m 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
memcache环境测试:
运行下面的php文件,如果有输出this is a test!,就表示环境搭建成功。开始领略memcache的魅力把!
< ?php
$mem = new memcache;
$mem->connect(”127.0.0.1′, 11211);
$mem->set(’key’, ‘this is a test!’, 0, 60);
$val = $mem->get(’key’);
echo $val;
?>
include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 . ,当前工作目录是 /www/ ,脚本中要 include 一个 include/a.php教程 并且在该文件中有一句 include "b.php" ,则寻找 b.php 的顺序先是 /www/ ,然后是 /www/include/ 。如果文件名以 ./ 或者 ../ 开始,则只在当前工作目录相对的 include_path 下寻找。
所以如下所示的文件结构
----a.php
----include/b.php
----include/c.php
其中a.php
<?php
include 'include/b.php';
?>
-----------------------
b.php
<?php
include 'c.php';
include 'include/c.php';
?>
--------------------------
c.php
<?php
echo 'c.php';
?>
--------------------------
都能正确运行,说明b.php中两种不同包含路径都是可行的,根据include寻找包含文件的方式都能找到c.php。
但是最好的方式还是使用绝对路径,如果使用了绝对路径,php内核就直接通过路径去载入文件而不用去include path逐个搜索文件,增加了代码执行效率
<?php
define('root_path',dirname(__file__));
include root_path.'/c.php';
?>
结论:
显然include 后面路径的格式和php的include path 对程序性能都是存在影响的,include 性能从慢到快的排序是
include 'a.php' < include './a.php' < include '/fullpath/a.php
在代码中,使用绝对路径include文件是最好的选择,因为这样php内核就直接通过路径去载入文件而不用去include path逐个搜索文件。
所以我们最好在项目的公用文件中定义一个项目根目录绝对路径的常量,然后所有的include的路径前都带上这个常量,这样项目中所有的include使用的都是绝对路径,既提高程序性能,也减少了相对路径带来的烦恼。
参考代码(来自emlog):
define('emlog_root', dirname(__file__));
include emlog_root . '/config.php';
如果你的项目中已经大量使用include 'test.php' 这样格式的相对路径且不好大量修改,那么请尽量减少php include path中的路径以提高一定的include性能。因为include path中的路径越少,php搜索文件的时间也越少。
在php教程中中文乱码解决方法
最最觉的是ajax post get 中文乱码了,
用ajax传递中文时..ff会很聪明的自动将中文字符转成十六进制.而bc的ie却非得自己处理..-____!!
最简单的方法就是在需要转义的地方使用js的内置函数 encodeuricomponent
p.s.最初是用escape做中文转义..注意它与 encodeuricomponent 的区别
php中其它引起乱码解决方法分析
1. php文件本身的编码与网页的编码应匹配
a. 如果欲使用gb2312编码,那么php要输出头:header(“content-type: text/html; charset=gb2312"),静态页面添加,所有文件的编码格式为ansi,可用记事本打开,另存为选择编码为ansi,覆盖源文件。
b. 如果欲使用utf-8编码,那么php要输出头 :header(“content-type: text/html; charset=utf-8"),静态页面添加,所有文件的编码格式为utf-8。保存为utf-8可能会有点麻烦,一般utf-8文件开头会有bom,如果使用 session就会出问题,可用editplus来保存,在editplus中,工具->参数选择->文件->utf-8签名,选择总是删除,再保存就可以去掉bom信息了。
2. php本身不是unicode的,所有substr之类的函数得改成mb_substr(需要装mbstring扩展);或者用iconv转码。
二.php与mysql教程的数据交互
php与数据库教程的编码应一致
1. 修改mysql配置文件my.ini或my.cnf,mysql最好用utf8编码
[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-storage-engine=myisam
在[mysqld]下加入:
default-collation=utf8_bin
init_connect='set names utf8'
2. 在需要做数据库操作的php程序前加mysql_query("set names '编码'");,编码和php编码一致,如果php编码是gb2312那mysql编码就是gb2312,如果是utf-8那mysql编码就是 utf8,这样插入或检索数据时就不会出现乱码了
三.php与操作系统相关
windows和linux的编码是不一样的,在windows环境下,调用php的函数时参数如果是utf-8编码会出现错误,比如 move_uploaded_file()、filesize()、readfile()等,这些函数在处理上传、下载时经常会用到,调用时可能会出现下面的错误:
warning: move_uploaded_file()[function.move-uploaded-file]:failed to open stream: invalid argument in ...
warning: move_uploaded_file()[function.move-uploaded-file]:unable to move '' to '' in ...
warning: filesize() [function.filesize]: stat failed for ... in ...
warning: readfile() [function.readfile]: failed to open stream: invalid argument in ..
在linux环境下用gb2312编码虽然不会出现这些错误,但保存后的文件名出现乱码导致无法读取文件,这时可先将参数转换成操作系统识别的编码,编码转换可用mb_convert_encoding(字符串,新编码,原编码)或iconv(原编码,新编码,字符串),这样处理后保存的文件名就不会出现乱码,也可以正常读取文件,实现中文名称文件的上传、下载。
其实还有更好的解决方法,彻底与系统脱离,也就不用考虑系统是何编码。可以生成一个只有字母和数字的序列作为文件名,而将原来带有中文的名字保存在数据库中,这样调用move_uploaded_file()就不会出现问题,下载的时候只需将文件名改为原来带有中文的名字。实现下载的代码如下
header("pragma: public");
header("expires: 0");
header("cache-component: must-revalidate, post-check=0, pre-check=0");
header("content-type: $file_type");
header("content-length: $file_size");
header("content-disposition: attachment; filename="$file_name"");
header("content-transfer-encoding: binary");
readfile($file_path);
$file_type是文件的类型,$file_name是原来的名字,$file_path是保存在服务上文件的地址