标签 php 下的文章

WordPress显示数据库查询次数和查询花费时间

WordPress提供了一些功能函数可以轻松地显示数据库查询的统计信息,这些信息可以公开地显示在网页中,或者隐藏在源代码中,更或者只有你自己可以看到。

WordPress主要提供了两个统计函数:

1.网页加载时查询数据库的次数:

 

2.服务器端完成这些查询所花费的时间:

 

可以使用3种方式在网页中显示:

1. 公开地显示查询的统计信息:

如果你觉的你的服务器或空间的处理速度好,或者想让你的访问者看到这些查询统计信息的话,你可以将这些统计信息公开显示在页面中:(效果查看博客的页面底部)

 

2. 将统计信息显示在源代码中,而不出现页面的内容中:

如果你不想将统计信息显示在页面上供访问者查看,但又想自己可以知道这些统计信息,那么你可以通过html的注释将结果只显示在源代码中(PS:大家也可以从源代码中查看的哦!):

 

3. 统计信息只有你自己登录了后可以看到:

该方法是第二种显示的优化方法,只需博客管理员登录后就可以查看查询统计信息,访问者是无法查看的:

 

如果想查看具体查询了数据库哪些内容,可以用如下解决方法:

1、首先在 wp-config.php 里添加如下代码:

 

然后在 footer.php 里添加如下代码:

 

分析:

1、if (is_user_logged_in()) 用于判断当前访客是否已登录,也可以用 if (current_user_can('level_10')) 来判断是否为管理员登录,目的是为了不让游客查看到这些数据,此代码可省;

2、global $wpdb; 定义全局变量$wpdb,这是Wordpress默认的数据库类;

3、<pre></pre>将结果嵌套在HTML标签<pre>内;

4、print_r($wpdb->queries); 输出各次数据库查询的信息。

刷新首页或日志页,可看到类似如下的输出结果:

 

下面的活儿就得自己分析了,看看哪些是可以删除,哪些是可以改进的,以减少数据库查询次数。

 

WordPress登陆文件wp-login.php添加相关参数,安全有保障

只需要在主题的funtions.php 的最后一个 ?> 前文件添加以下代码就可以了:

 

按照上面的代码,你的WordPress 登陆地址应该是 http://域名/wp-login.php?love=someone 。?love=someone就是参数名,这段代码的作用大概就是通过获取文件头,如果没有参数?love=someone,那么就跳转到http://www.dammit.hk/。

你可以修改的地方就是第四行的 love、someone以及http://www.dammit.hk/。

这样一来,安全性就大大提高了。而且不要向上次的那样修改WordPress本身的代码,只需要在主题那里添加就可以了。

在iPhone/iPad中搭建web服务器和PHP开发环境:Lighttpd + PHP(with GD) + MySQL

1. Install the Package in Cydia Add the Cydia Source http://ios-webstack.tk/cydia, find and install the package ios-lighttpd-php-mysql. Your lighttpd web server is now running. You can test it in Safari. But you need to do some more work with php and mysql.
2. Mysql Configuration
Log in your IOS device and execute the following shell commands.
# bin/mysql_install_db –user=daemon
# /usr/local/bin/mysqladmin -u root password ‘new-password’
# /usr/local/bin/mysql_secure_installation
执行mysql_secure_installation
Change the root password? [Y/n]
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] n
# mysql -u root -p’yourpassword’
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection idis 8
Server version: 5.1.66 Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.
mysql>
mysql>
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
+——————–+
2 rows in set (0.01 sec)
mysql>
3. PHP Configuration
Create File: /etc/php.ini
;This is an ‘EXAMPLE’ configuration file for PHP.
;Be careful with the socket path
mysql.default_socket = /tmp/mysql.sock
pdo_mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
;Limits
upload_max_filesize = 10M
post_max_size= 10M
;TimeZone
;You should simply replace ‘Europe/Berlin’ with the desired timezone.
;The full list of supported time zones is available here: http://www.php.net/manual/en/timezones.php
date.timezone = ‘UTC’
;I recommends to place following lines to /etc/php.d/security.ini (it will not be touched
;from a cydia packages update).
;========================================================================================
;Please have a look at http://www.cyberciti.biz/tips/php-security-best-practices-tutorial.html
;It is up to you to comment-out some directives.
;Make sure you log all php errors to a log file.
;Make sure that php and lighttpd have write permissions.
log_errors=Off
error_log=/var/log/lighttpd/php_scripts_error.log
;To restrict PHP information leakage disable expose_php.
;Have a check befor and after: curl -I http://your.iOS-Web.server/yourPHPsite.php
expose_php=Off
;Do not expose PHP error messages toall site visitors.
; display_errors=Off
;The allow_url_fopen option allows PHP’s file functions – such as file_get_contents() and the
;include and require statements – can retrieve data from remote locations using ftp or http
;protocols. Programmers frequently forget this and don’t do proper input filtering when passing
;user-provided data to these functions, opening them up to code injection vulnerabilities.
;A large number of code injection vulnerabilities reported in PHP-based web applications are
;caused by the combination of enabling allow_url_fopen and bad input filtering.
; allow_url_fopen=Off
;I also recommend to disable allow_url_include for security reasons:
; allow_url_include=Off
;If turned On, mysql_connect() and mysql_pconnect() ignore any arguments passed to them.
;Please note that you may have to make some changes to your code. Third party and open source
;application such as WordPress, and others may not work at all when sql.safe_mode enabled.
; sql.safe_mode=On
;It is also recommended that you turn off magic_quotes_gpc for all php 5.3.x installations
;as the filtering by it is ineffective and not very robust. mysql_escape_string() and
;custom filtering functions serve a better purpose (hat tip to Eric Hansen):
; magic_quotes_gpc=Off
;You can set maximum execution time of each php script, in seconds.
;Another recommend option is to set maximum amount of time each script
;may spend parsing request data, and maximum amount of memory a script may consume.
;GD picture converting needs a lot of memory. Be carefull with ‘memory_limit’
;Set in seconds:
; max_execution_time = 30
; max_input_time = 30
; memory_limit = 40M
The packages include configuration files for PHP in /etc/ and extension configuration files in /etc/php.d/
Create File: /etc/php.d/php.ini
;You can splite this file in separate INI files.
;If you do that, please remember the extension loading
;order of INI files (alphabetically).
extension=iconv.so
extension=mbstring.so
extension=curl.so
;The loading order is important
extension=sqlite3.so
extension=pdo_sqlite.so
;The loading order is important
extension=mysqlnd.so
extension=mysqli.so
extension=mysql.so
extension=pdo_mysql.so
;Must be loaded after mbstring.so
extension=exif.so
extension=gd.so
extension=zip.so
extension=bz2.so
extension=calendar.so
extension=mcrypt.so
extension=xsl.so
extension=openssl.so
Finally, input http://127.0.0.1/phpinfo.php in Safari, you’ll get a long PHP information page like this:
PHP Version 5.4.8 System
Darwin mengtaos-iPhone 10.4.0 Darwin Kernel Version 10.4.0: Wed Oct 20 20:08:31 PDT 2010; root:xnu-1504.58.28~3/RELEASE_ARM_S5L8920X iPhone2,1
Build Date
Nov 14 2012 17:33:08
Configure Command
‘./configure’
Server API
CGI/FastCGI
Virtual Directory Support
disabled
Configuration File (php.ini) Path
/etc
Loaded Configuration File
/etc/php.ini
Scan this dir for additional .ini files
/etc/php.d
Now, Enjoy this little web server!

SAE中使用TmpFS功能

前一段时间做的项目需要提供导出CSV报表的功能,我用php直接拼出csv的文件流输出到页面,以提供csv文件的下载,功能在wamp环境中运行很正常,但是到了SAE平台却怎么都不好使。不管是改请求的响应头content-type 还是修改输出的文件格式,每次SAE都是把内容直接输出在了页面。因为SAE不支持通用方式的本地IO,于是想到了使用SAE提供的tmpFS功能。
TmpFS
因为平台安全性的考虑,SAE限制了用户对于本地IO的使用,但这样对于一些传统的PHP项目,也许带来了很多不便,因为它们都或多或少的有对本地 IO的操作,像Smarty的编译模板。为了解决这个问题,SAE提供了TmpFS功能。TmpFS允许开发者通过标准的IO函数临时读写本地IO,这样 方便了很多非SAE项目的移植。
特别注意:
临时文件的生存周期等同于PHP请求,也就是当该PHP请求完成执行时,所有写入TmpFS的临时文件都会被销毁
TmpFS是本地临时文件,不是共享存储,而SAE是全分布式环境,所以不同请求之间无法通过TmpFS共享操作文件
TmpFS操作的文件限于SAETMPPATH目录内,而不同App的SAETMPPATH是不同的
TmpFS的文件为纯内存存储
应用场景
用户的可持久化存储,请使用Storage或者MySQL存储,而缓存存储请使用Memcache服务存储,TmpFS是满足用户的一个请求的临时 文件的读写需求。比如抓取一个URL的图片,判断一下大小,再决定是否写入Storage。需要在本地生成文件的情况大致分以下几种:
缓存
配置文件
静态文件
临时文件
例子:
appname: saetest
appversion: 1
在一个php文件中:
file_put_contents( SAE_TMP_PATH . ‘/mycode.txt’ , ‘dummy test’ );
echo file_get_contents( SAE_TMP_PATH . ‘/mycode.txt’ ); // will echo dummy test;
如果是两个独立的php文件:
a.php
file_put_contents( SAE_TMP_PATH . ‘/mycode.txt’ , ‘dummy test’ );
b.php
echo file_get_contents( SAE_TMP_PATH . ‘/mycode.txt’ ); // 出错啦,文件已经不存在了…
说到这里,大家应该明白了, tmpFS中的文件在后台PHP代码执行完后就已经不存在了,囧。最后只好用了个折中的办法,临时把文件放在了SAE的永久化存储的Storage里面。不知看到这里的朋友有无更好的办法解决SAE中导出文件的问题,欢迎联系告知~

2024年5月
« 2月    
 12345
6789101112
13141516171819
20212223242526
2728293031  

广告

分类目录

近期评论

标签

历史上的今天

    历史上的今天没有存档

文章归档