本文记录了一些自己遇到的PHP常见错误和解决方法,方便今后查询。

问题1  Could not reliably determine the server's fully qualified domain name

解决方案:

1) 用记事本打开 vim httpd.conf

将里面的 #ServerName localhost:80 注释去掉即可(启动ServerName)

2) 重启apache 

sudo ./bin/apachectl restart

3) 通过浏览器访问 http://localhost:80

如果页面显示 “It works!” ,即表示apache已安装并启动成功。

补充说明:

using localhost.localdomain for ServerName  说不能确认服务器完全确认域名 localhost.localdoman  这个问题怎么解决

最佳答案: 

vi /etc/httpd/conf/httpd.conf   加入一句  ServerName  localhost:80

 

问题2 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 the timezone 'UTC' for now, but please set date.timezone to select your timezone. in/home/homer/workspace/proxy_client/index.php on line 174 2015-01-10)

解决方案:

出现这个问题是因为网站代码里面没有正确设置时区造成的,从PHP 5.3开始,当使用date()等函数时,如果timezone设置不正确,在每一次调用时间函数时都会产生E_NOTICE 或者 E_WARNING信息。解决的方法有三种:

方法一:使用较低版本的PHP,如PHP 5.2.17

方法二:修改 php.ini配置文件,例如: 

/opt/php-5.5.15-server/lib/php.ini

/opt/php-5.5.15-server/etc/php.ini

修改上述两文件的 

date.timezone = "Asia/Shanghai" 或 date.timezone = PRC 

步骤如下:

1) 打开php.ini 配置文件 ,找到 

[Date] 

; Defines the default timezone used by the date functions 

; http://php.net/date.timezone 

;date.timezone = 

修改为: 

[Date] 

; Defines the default timezone used by the date functions 

; http://php.net/date.timezone 

date.timezone = PRC 

;date.timezone = "Asia/Shanghai"

2)重启apache服务,使配置生效

sudo ./bin/apachectl restart

 

方法三:在网页页头使用date_default_timezone_set()函数设置

例如: <?php date_default_timezone_set('PRC');  ?>

// 表示中国东8区时间,测试 echo date('Y-m-d H:i:s'); 

 

方法四:在页头使用 ini_set('date.timezone','Asia/Shanghai'); 

对于中国可选值:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次为重庆,上海,乌鲁木齐)港台地区可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次为澳门,香港,台北),还有新加坡:Asia/Singapore,老的PRC也行。 

PHP所支持的时区列表可参见:http://www.php.net/manual/zh/timezones.php

 

问题3 PDO连接MySQL数据库错误

Notice: Undefined index: pageindex in /home/homer/workspace/proxy_client/index.php on line 211

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /home/homer/workspace/proxy_client/index.php:223 Stack trace: #0 /home/homer/workspace/proxy_client/index.php(223): PDO->__construct('mysql:host=115....', 'user', 'pass') #1 {main} thrown in/home/homer/workspace/proxy_client/index.php on line 223

解决方案:

需要给PHP加PDO扩展,打开PHP.INI 把extension=php_pdo.dll 前面的分号去掉,如下图:

php5-pdo-mysql

 

重启APACHE

homer@homer-pc:~$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                                               [ OK ] 

 

如果没有 pdo_mysql 模块,则查看并安装

You need to have a module called pdo_mysql. Looking for following in phpinfo()

pdo_mysql

PDO Driver for MySQL, client library version => 5.1.44

----


Check that you have the mysql extension installed on your server.

In Ubuntu/Debian you check for the package with:

dpkg --get-selections | grep php5-mysql
Install the php5-mysql package if you do not have it.

In Ubuntu/Debian you can use:

sudo apt-get install php5-mysql
Lastly, to get it working with Apache, you will need to restart Apache.

sudo /etc/init.d/apache2 restart

 

通过 phpinfo.php 验证 pdo_mysql 安装成功

php5-pdo-mysql_check 

添加网站路径

sudo vim /etc/apache2/apache2.conf

php5-website_config

 

更多相关 pdo_mysql 请参见米扑博客: PHP5中PDO的简单使用

 

 

PHP 线上的生产环境不显示警告(Warning)

Warning: sizeof(): Parameter must be an array or an object that implements Countable in lib/PayPal/Common/PayPalModel.php on line 179

Warning: sizeof(): Parameter must be an array or an object that implements Countable in lib/PayPal/Common/PayPalModel.php on line 179

解决办法:

1、修改 php.ini (发现没有效果)

; error_reporting
;error_reporting = E_ALL
;error_reporting = E_ALL & ~E_DEPRECATED
;error_reporting = E_ALL & ~E_ERROR
;error_reporting = E_ALL & ~E_NOTICE
;error_reporting = E_ALL & ~E_WARNING
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT

说明:~E_WARNING 表示不显示警告,波浪线表示否的意思

 

2、修改 php.ini 配置参数(发现没有效果)

编辑php.ini ,查找"display_errors =" ,将“=”后面的值改为"Off

 

3、PHP 文件开头添加函数 (有效,推荐

ini_set("error_reporting","E_ALL & ~E_NOTICE");

例如:

<?php

namespace PayPal\Common;

ini_set("error_reporting","E_ALL & ~E_NOTICE");
use PayPal\Validation\JsonValidator;
use PayPal\Validation\ModelAccessorValidator;