php date()时间函数报错,时区问题解决 UTC PRC CST

 更新时间:2016年11月25日 17:38  




echo date('Y-m-d');




"PHP Warning: date() []: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in"

实际上,从 PHP 5.1.0 ,当对使用date()等函数时,如果timezone设置不正确,在每一次调用时间函数时,都会产生E_NOTICE 或者 E_WARNING 信息。而又在php5.1.0中,date.timezone这个选项,默认情况下是关闭的,无论用什么php命令都是格林威治标准时间,但是PHP5.3中好像如果没有设置也会强行抛出了这个错误的,解决此问题,只要本地化一下,就行了。


一、在页头使用date_default_timezone_set()设置 date_default_timezone_set('PRC'); //东八时区 echo date('Y-m-d H:i:s');

二、在页头使用 ini_set('date.timezone','Asia/Shanghai');

三、修改php.ini。打开php5.ini查找date.timezone 去掉前面的分号修改成为:date.timezone =PRC

本文章来给各位同学介绍Fatal error: session_start(): Failed to initialize storage module: files问题的解决办法。

原创解决: Fatal error: session_start(): Failed to initialize storage module: files问题

Fatal error: session_start(): Failed to initialize storage module: files (path: ) in /data/www/phpmyadmin/libraries/ on line 83


 代码如下 复制代码

session.save_path = "/tmp"







 代码如下 复制代码
   $r = session_start();  var_dump($r);


Warning: session_start(): SAFE MODE Restriction in effect. The script whose uid is 501 is not allowed to access /tmp owned by uid 0 in /data/www/test.php on line 3 Fatal error: session_start(): Failed to initialize storage module: files (path: ) in /data/www/test.php on line 3

意思是 php5一个安全模式的bug,默认session的save_path是系统的临时目录,这样会要校验权限。而这个脚本不能通过/tmp拥有者uid为0来执行uid是501也是www用户组的权限




 代码如下 复制代码

 ; Handler used to store/retrieve data.
session.save_handler = files; Argument passed to save_handler.  In the case of files, this is the path
 ; where data files are stored. Note: Windows users have to change this
 ; variable in order to use PHP's session functions.
 ; The path can be defined as:
 ;     session.save_path = "N;/path"
 ; where N is an integer.  Instead of storing all the session files in
 ; /path, what this will do is use subdirectories N-levels deep, and
 ; store the session data in those directories.  This is useful if you
 ; or your OS have problems with lots of files in one directory, and is
 ; a more efficient layout for servers that handle lots of sessions.
 ; NOTE 1: PHP will not create this directory structure automatically.
 ;         You can use the script in the ext/session dir for that purpose.
 ; NOTE 2: See the section on garbage collection below if you choose to
 ;         use subdirectories for session storage
 ; The file storage module creates files using mode 600 by default.
 ; You can change that by using
 ;     session.save_path = "N;MODE;/path"
 ; where MODE is the octal representation of the mode. Note that this
 ; does not overwrite the process's umask.
 session.save_path = "/tmp"
; Whether to use cookies.
 session.use_cookies = 1
 ;session.cookie_secure =
; This option forces PHP to fetch and use a cookie for storing and maintaining
 ; the session id. We encourage this operation as it's very helpful in combatting
 ; session hijacking when not specifying and managing your own session id. It is
 ; not the end all be all of session hijacking defense, but it's a good start.
 session.use_only_cookies = 1
; Name of the session (used as cookie name).
; Initialize session on request startup.
 session.auto_start = 0
; Lifetime in seconds of cookie or, if 0, until browser is restarted.
 session.cookie_lifetime = 0
; The path for which the cookie is valid.
 session.cookie_path = /
; The domain for which the cookie is valid.
 session.cookie_domain =
; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.
 session.cookie_httponly =
; Handler used to serialize data.  php is the standard serializer of PHP.
 session.serialize_handler = php
; Defines the probability that the 'garbage collection' process is started
 ; on every session initialization. The probability is calculated by using
 ; gc_probability/gc_divisor. Where session.gc_probability is the numerator
 ; and gc_divisor is the denominator in the equation. Setting this value to 1
 ; when the session.gc_divisor value is 100 will give you approximately a 1% chance
 ; the gc will run on any give request.
 ; Default Value: 1
 ; Development Value: 1
 ; Production Value: 1
 session.gc_probability = 1
; Defines the probability that the 'garbage collection' process is started on every
 ; session initialization. The probability is calculated by using the following equation:
 ; gc_probability/gc_divisor. Where session.gc_probability is the numerator and
 ; session.gc_divisor is the denominator in the equation. Setting this value to 1
 ; when the session.gc_divisor value is 100 will give you approximately a 1% chance
 ; the gc will run on any give request. Increasing this value to 1000 will give you
 ; a 0.1% chance the gc will run on any give request. For high volume production servers,
 ; this is a more efficient approach.
 ; Default Value: 100
 ; Development Value: 1000
 ; Production Value: 1000
 session.gc_divisor = 1000
; After this number of seconds, stored data will be seen as 'garbage' and
 ; cleaned up by the garbage collection process.
 session.gc_maxlifetime = 1440
; NOTE: If you are using the subdirectory option for storing session files
 ;       (see session.save_path above), then garbage collection does *not*
 ;       happen automatically.  You will need to do your own garbage
 ;       collection through a shell script, cron entry, or some other method.
 ;       For example, the following script would is the equivalent of
 ;       setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
 ;          find /path/to/sessions -cmin +24 | xargs rm
; PHP 4.2 and less have an undocumented feature/bug that allows you to
 ; to initialize a session variable in the global scope, even when register_globals
 ; is disabled.  PHP 4.3 and later will warn you, if this feature is used.
 ; You can disable the feature and the warning separately. At this time,
 ; the warning is only displayed, if bug_compat_42 is enabled. This feature
 ; introduces some serious security problems if not handled correctly. It's
 ; recommended that you do not use this feature on production servers. But you
 ; should enable this on development servers and enable the warning as well. If you
 ; do not enable the feature on development servers, you won't be warned when it's
 ; used and debugging errors caused by this can be difficult to track down.
 ; Default Value: On
 ; Development Value: On
 ; Production Value: Off
 session.bug_compat_42 = Off
; This setting controls whether or not you are warned by PHP when initializing a
 ; session value into the global space. session.bug_compat_42 must be enabled before
 ; these warnings can be issued by PHP. See the directive above for more information.
 ; Default Value: On
 ; Development Value: On
 ; Production Value: Off
 session.bug_compat_warn = Off
; Check HTTP Referer to invalidate externally stored URLs containing ids.
 ; HTTP_REFERER has to contain this substring for the session to be
 ; considered as valid.
 session.referer_check =
; How many bytes to read from the file.
 session.entropy_length = 0
; Specified here to create the session id.
 ; On systems that don't have /dev/urandom /dev/arandom can be used
 ; On windows, setting the entropy_length setting will activate the
 ; Windows random source (using the CryptoAPI)
 ;session.entropy_file = /dev/urandom
; Set to {nocache,private,public,} to determine HTTP caching aspects
 ; or leave this empty to avoid sending anti-caching headers.
 session.cache_limiter = nocache
; Document expires after n minutes.
 session.cache_expire = 180
; trans sid support is disabled by default.
 ; Use of trans sid may risk your users security.
 ; Use this option with caution.
 ; - User may send URL contains active session ID
 ;   to other person via. email/irc/etc.
 ; - URL that contains active session ID may be stored
 ;   in publically accessible computer.
 ; - User may access your site with the same session ID
 ;   always using URL stored in browser's history or bookmarks.
 session.use_trans_sid = 0
; Select a hash function for use in generating session ids.
 ; Possible Values
 ;   0  (MD5 128 bits)
 ;   1  (SHA-1 160 bits)
 ; This option may also be set to the name of any hash function supported by
 ; the hash extension. A list of available hashes is returned by the hash_algos()
 ; function.
 session.hash_function = 0
; Define how many bits are stored in each character when converting
 ; the binary hash data to something readable.
 ; Possible values:
 ;   4  (4 bits: 0-9, a-f)
 ;   5  (5 bits: 0-9, a-v)
 ;   6  (6 bits: 0-9, a-z, A-Z, "-", ",")
 ; Default Value: 4
 ; Development Value: 5
 ; Production Value: 5
 session.hash_bits_per_character = 5
; The URL rewriter will look for URLs in a defined set of HTML tags.
 ; form/fieldset are special; if you include them here, the rewriter will
 ; add a hidden <input> field with the info which is otherwise appended
 ; to URLs.  If you want XHTML conformity, remove the form entry.
 ; Note that all valid entries require a "=", even if no value follows.
 ; Default Value: "a=href,area=href,frame=src,form=,fieldset="
 ; Development Value: "a=href,area=href,frame=src,input=src,form=fakeentry"
 ; Production Value: "a=href,area=href,frame=src,input=src,form=fakeentry"
 url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"


 代码如下 复制代码

$sessSavePath = "/data/sessions/";
 // Session保存路径
 if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath); }
 if(!empty($cfg_domain_cookie)) session_set_cookie_params(0,'/',$cfg_domain_cookie);

上面这个代码是在session_start() 初始化之前来判断是否存在session会话的文件夹。

 代码如下 复制代码

if (! isset($_COOKIE[$session_name])) {
 // on first start of session we check for errors
 // f.e. session dir cannot be accessed - session file not created
 $orig_error_count = $GLOBALS['error_handler']->countErrors();
 $r = session_start();
 if ($r !== true
 || $orig_error_count != $GLOBALS['error_handler']->countErrors()
 ) {
 setcookie($session_name, '', 1);
 * Session initialization is done before selecting language, so we
 * can not use translations here.
 PMA_fatalError('Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly. Also ensure that cookies are enabled in your browser.');
 } else {

在    session_start();  前面添加了  session_save_path(“/data/www/session”); 就解决了这个问题。
切记通过@ini_set(‘session.save_path’, ”/data/www/session”);无效!

我使用的都是php __autoload状态自动加载类的,今天好好的程序不知道怎么在运行时提示Fatal error: Cannot redeclare class 了,看是重复定义了类,下面我来分析一下解决办法。


Fatal error: Cannot redeclare class ….





 代码如下 复制代码
class Foo {}  
// some code here  
class Foo {}  

在第二个 Foo 的地方就会报错。




 代码如下 复制代码
if(class_exists('SomeClass') != true)  
   //put class SomeClass here  

if(class_exists('SomeClass') != true)
   //put class SomeClass here



 代码如下 复制代码

include "some_class.php"; 
include "some_class.php";


 代码如下 复制代码

include "a.php";  
include "some_class.php"; 
include "a.php";
include "some_class.php";






 代码如下 复制代码
class Com  

这时候提示Cannot redeclare class Com,说明这个类就是PHP内置的类。不能使用。



 代码如下 复制代码

if (!class_exists('pageModule')){    

上面的办法不适用于使用了php __autoload类加载的方法 ,但己经可以解决办法问题了,__autoload是自动加载的我们只要把相同类名找出来然后重命名即可。





PHP.ini 找到memory_limit参数,增大!


换一个浏览器!我之前一直用Google Chrome调试程序,之后就开始变慢下来,直到每个页面变成6秒才加载完毕,已经忍无可忍。换成了Firefox、IE均没有这个问题。 (这个方法很好用,还能立刻见效!)


使用 xdebug.profiler_enable_trigger配置,有选择的运行xdebug功能探查程序。


xdebug.remote_host 的值最好跟你服务器的IP一致,比方说你是通过localhost访问,则这里写localhost,你是127.0.0.1访问,就写127.0.0.1




xdebug.remote_enable = 0
xdebug.profiler_enable = 0
xdebug.remote_autostart = false

本文章来给大家介绍Strict Standards: Only variables should be passed by reference in解决办法,各位朋友可参考。


 代码如下 复制代码

$arr = array('one', 'two', 'three');

echo reset($arr);  

// 输出的结果为 one对于下述代码,如果php开启了"Strict Standards",将会看到"Strict Standards: Only variables should be passed by reference in"的提示。

 代码如下 复制代码

$str = '';

echo reset(explode('.', $str));


function reset (array &$array) {}从定义中可以看到,reset接受的参数是一个array的引用。而上述代码中explode返回值不是任何array的引用,所以在"Strict Standards"下会有上述提示。解决办法很简单,只需要把reset(explode('.', $str))分两步写即可。第一步先把explode的返回值赋给一个变量,第二步把这个变量作为reset参数。