apache下启用二级域名泛解析,实现博客功能

httpd_vhosts.conf文件内容

<VirtualHost *:80>
DocumentRoot “d:/site/papake.cn”
DirectoryIndex index.htm index.php

<Directory “d:/site/papake.cn”>
Options Indexes MultiViews
AllowOverride None
order allow,deny
Allow from all
Options FollowSymLinks Includes
</Directory>

RewriteEngine on
RewriteLog       logs/re.log
RewriteLogLevel  1
RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9-]+.papake.cn$
RewriteCond %{HTTP_HOST} !^(www).papake.cn$
RewriteRule ^/?$ /%{HTTP_HOST} [NC]
RewriteRule ^/([a-zA-Z0-9-]+).papake.cn/?$ http://www.papake.cn/$1 [P,L]

RewriteLog “D:/Apache2.2/logs/rewrite.log”

</VirtualHost>

以上要实现输入lihai.papake.cn的网址,实现处理的是www.papake.cn/lihai此文件夹里的内容的,除了www这个以外. Continue reading

PHP安全配置详解

PHP勿庸置疑是非常强大的服务器端脚本语言,但是强大的功能总是伴随着重大的危险, 在这章里,你将学习到使用PHP的安全模式来阻止一些PHP潜在的危险因素。
【 安全模式 】

PHP的安全模式提供一个基本安全的共享环境,在一个有多个用户帐户存在的PHP开放的Web服务器上。当一个Web服务器上运行的PHP打开了安全模 式,那么一些函数将被完全的禁止,并且会限制一些可用的功能。

[ 使用安全模式来强制限制 ]
在安全模式下,一些尝试访问文 件系统的函数功能将被限制。运行Web服务器用户ID,如果想要操作某个文件,则必须拥有该文件读取或者写入的访问权限,实现这个限制功能对于PHP来说 是没有问题的。 Continue reading

PHP中echo和print的区别

PHP 和 HTML 最简单的交互是通过 print 和 echo 语句来实现的,在实际使用中, print 和 echo 两者的功能几 乎是完全一样。可以这么说,凡是有一个可以使用的地方,另一个也可以使用。但是,两者之间也还是一个非常重要的区别:在 echo 函数中,可以同时输出 多个字符串,而在 print 函数中则只可以同时输出一个字符串。同时,echo函数并不需要圆括号,所以echo函数更像是语句而不像是函数。

echo 和 print 都不是函数,而是语言结构,所以圆括号都不是必需的

echo命令和print命令相同,没有区别 ;
echo函数和print函数有区别;
print()有返 回值,当其执行失败(比如断线)时返回flase;
printf()和sprintf()类似,均为格式化输出,不同的是前者输出到 标准输出,后者输出到变量。

他们的区别在于:
(1) echo可以输出多个字符串,像下面这样:
echo ‘a’,’b’,’c’;
如果你非要加上圆括号,注意写成echo (‘a’,’b’,’c’);是错误的,应该写成:
echo (‘a’),(‘b’),(‘c’);
它没有像函数的行为,所以不能用于函数的上下文.

(2) print只能输出一个字符串,它可以表现得像一个函数,比如你可以如下使用:
$ret = print ‘Hello World’;
所有它能用在更复杂的表达式中。

另外,echo的效率相对比较快~

如下代码:

<?php
$a=’hello ‘;$b=’php world!’;echo $a,$b.'<br />’;//echo 可以用逗号分隔 字符串变量来显示print $a.$b.'<br />’;//而print不能使用逗号,只能用点号分 隔,print $a,$b.'<br />’;//使用逗号时报错。
?>

echo     命令和     print     命令     相同,没有区别
echo     函数     和     print     函数     有区别。
echo()     无返回值,与echo     命令相同
print()     有返回值,成功,返1,false,返0.

printf()和sprintf()类似,均为格式化输出,不同的是前者输出到标准输出,后者输出到变量

形如
echo     <<<     EOT
EOT;

print     <<<     EOT
EOT;
的书写格式,其含义如下:
<<<     运算符,将由自定义分界符间的内容视为字符串,可对其间的变量做处理
EOT     自定义分界符,结束时必须位于行首

windows下安装 Xdebug+WinCacheGrind

以PHP5.1.4,Windows平台为例(其它PHP版本,其它平台请参看官网文档):
1. 在http://www.xdebug.org/download.php下载适合自己php版本的php_xdebug- 2.0.1-5.1.2.dll【有附件提供下载,如果按照以下步骤完成后 phpinfo任无法显示xdebug,那么建议重新下载其他xdebug.dll文件试试 】;
2. 将下载的xdebug.dll放到php\ext目录里,可以重命名也可以不重命名,这里我没有重命名。
3. 编辑php.ini,加入下面几行:

extension=php_xdebug-2.0.1-5.1.2.dll

;xdebug配置
[Xdebug]
;开启自动跟踪
xdebug.auto_trace = On
;开启异常跟踪
xdebug.show_exception_trace = On
;开启远程调试自动启动
xdebug.remote_autostart = On
;开启远程调试
xdebug.remote_enable = On
;收集变量
xdebug.collect_vars = On
;收集返回值
xdebug.collect_return = On
;收集参数
xdebug.collect_params = On

xdebug.profiler_enable=on
xdebug.trace_output_dir=”D:/php/xdebug”
xdebug.profiler_output_dir=”D:/php/xdebug”  //(用来存放性能分析文件,可自由定义目录)

重启Apache;

写一个test.php,内容为<?php phpinfo(); ?>,如果输出的内容中有看到xdebug,说明安装配置成功。 Continue reading

启用Xdebug使用WinCacheGrind分析脚本执行时间

使用Xdebug调试和优化PHP程序系列教程之WinCacheGrind,教你如何利用Xdebug 配合WinCacheGrind工具来检测PHP代码的效率以及分析PHP代码。
另外还有一个结果分析展示工具webgrind。可参考:http://blog.sina.com.cn/s/blog_635833b3010127q5.html

有时候代码没有明显的编写错误,没有显示任何错误信息(如 error、warning、notice等),但是这不表明代码就是正确无误的。有时候可能某段代码执行时间过长,占用内存过多以致于影响整个系统的效 率,我们没有办法直接看出来是哪部份代码出了问题。这时候我们希望把代码的每个阶段的运行情况都监控起来,写到日志文件中去,运行一段时间后再进行分析, 找到问题所在。

回忆一下,之前我们编辑php.ini文件
加入

[Xdebug]
xdebug.profiler_enable=on
xdebug.trace_output_dir="I:\Projects\xdebug"
xdebug.profiler_output_dir="I:\Projects\xdebug"

这几行,目的就在于把执行情况的分析文件写入到”I:\Projects\xdebug”目录中去 (你可以替换成任何你想设定的目录)。如果你执行某段程序后,再打开相应的目录,可以发现生成了一堆文件,例如 cachegrind.out.1169585776这种格式命名的文件。这些就是 Xdebug生成的分析文件。用编辑器打开你可以看到很多程序运行的相关细节信息,不过很显然这样看太累了,我们需要用图形化的软件来查看。 Continue reading

mysql显示SQL语句执行时间

查看 MySQL 語法 詳細執行時間 與 CPU/記憶體使用量: MySQL Query Profiler

MySQL 的 SQL 語法調整主要都是使用 EXPLAIN , 但是這個並沒辦法知道詳細的 Ram(Memory)/CPU 等使用量.

於 MySQL 5.0.37 以上開始支援 MySQL Query Profiler, 可以查詢到此 SQL 會執行多少時間, 並看出 CPU/Memory 使用量, 執行過程中 System lock, Table lock 花多少時間等等.

MySQL Query Profile 詳細介紹可見: Using the New MySQL Query Profiler (2007.04.05 發表)

效能分析主要分下述三種(轉載自上篇):

  • Bottleneck analysis – focuses on answering the questions: What is my database server waiting on; what is a user connection waiting on; what is a piece of SQL code waiting on?
  • Workload analysis – examines the server and who is logged on to determine the resource usage and activity of each.
  • Ratio-based analysis – utilizes a number of rule-of-thumb ratios to gauge performance of a database, user connection, or piece of code.

MySQL Query Profile 使用方法

啟動
  • mysql> set profiling=1; # 此命令於 MySQL 會於 information_schema 的 database 建立一個 PROFILING 的 table 來紀錄.
SQL profiles show
  • mysql> show profiles; # 從啟動之後所有語法及使用時間, 含錯誤語法都會紀錄.
  • ex: (root@localhost) [test]> show profiles; # 注意 Query_ID, 下面執行時間統計等, 都是依 Query_ID 在紀錄
    	+----------+------------+---------------------------+
    	| Query_ID | Duration   | Query                     |
    	+----------+------------+---------------------------+
    	|        1 | 0.00090400 | show profile for query 1  |
    	|        2 | 0.00008700 | select * from users       |
    	|        3 | 0.00183800 | show tables               |
    	|        4 | 0.00027600 | mysql> show profiles      |
    	+----------+------------+---------------------------+
查詢所有花費時間加總
  • mysql> select sum(duration) from information_schema.profiling where query_id=1; # Query ID = 1
    	+---------------+
    	| sum(duration) |
    	+---------------+
    	|      0.000447 |
    	+---------------+
查詢各執行階段花費多少時間
  • mysql> show profile for query 1; # Query ID = 1
    	+--------------------+------------+
    	| Status             | Duration   |
    	+--------------------+------------+
    	| (initialization)   | 0.00006300 |
    	| Opening tables     | 0.00001400 |
    	| System lock        | 0.00000600 |
    	| Table lock         | 0.00001000 |
    	| init               | 0.00002200 |
    	| optimizing         | 0.00001100 |
    	| statistics         | 0.00009300 |
    	| preparing          | 0.00001700 |
    	| executing          | 0.00000700 |
    	| Sending data       | 0.00016800 |
    	| end                | 0.00000700 |
    	| query end          | 0.00000500 |
    	| freeing items      | 0.00001200 |
    	| closing tables     | 0.00000800 |
    	| logging slow query | 0.00000400 |
    	+--------------------+------------+
查詢各執行階段花費的各種資源列表
  • mysql> show profile cpu for query 1; # Query ID = 1
    	+--------------------------------+----------+----------+------------+
    	| Status                         | Duration | CPU_user | CPU_system |
    	+--------------------------------+----------+----------+------------+
    	| (initialization)               | 0.000007 | 0        | 0          |
    	| checking query cache for query | 0.000071 | 0        | 0          |
    	| Opening tables                 | 0.000024 | 0        | 0          |
    	| System lock                    | 0.000014 | 0        | 0          |
    	| Table lock                     | 0.000055 | 0.001    | 0          |
    	| init                           | 0.000036 | 0        | 0          |
    	| optimizing                     | 0.000013 | 0        | 0          |
    	| statistics                     | 0.000021 | 0        | 0          |
    	| preparing                      | 0.00002  | 0        | 0          |
    	| executing                      | 0.00001  | 0        | 0          |
    	| Sending data                   | 0.015072 | 0.011998 | 0          |
    	| end                            | 0.000021 | 0        | 0          |
    	| query end                      | 0.000011 | 0        | 0          |
    	| storing result in query cache  | 0.00001  | 0        | 0          |
    	| freeing items                  | 0.000018 | 0        | 0          |
    	| closing tables                 | 0.000019 | 0        | 0          |
    	| logging slow query             | 0.000009 | 0        | 0          |
    	+--------------------------------+----------+----------+------------+
  • mysql> show profile IPC for query 1;
    	+--------------------------------+----------+---------------+-------------------+
    	| Status                         | Duration | Messages_sent | Messages_received |
    	+--------------------------------+----------+---------------+-------------------+
    	| (initialization)               | 0.000007 |             0 |                 0 |
    	| checking query cache for query | 0.000071 |             0 |                 0 |
    	| Opening tables                 | 0.000024 |             0 |                 0 |
    	| System lock                    | 0.000014 |             0 |                 0 |
    	| Table lock                     | 0.000055 |             0 |                 0 |
    	| init                           | 0.000036 |             0 |                 0 |
    	| optimizing                     | 0.000013 |             0 |                 0 |
    	| statistics                     | 0.000021 |             0 |                 0 |
    	| preparing                      | 0.00002  |             0 |                 0 |
    	| executing                      | 0.00001  |             0 |                 0 |
    	| Sending data                   | 0.015072 |             0 |                 0 |
    	| end                            | 0.000021 |             0 |                 0 |
    	| query end                      | 0.000011 |             0 |                 0 |
    	| storing result in query cache  | 0.00001  |             0 |                 0 |
    	| freeing items                  | 0.000018 |             0 |                 0 |
    	| closing tables                 | 0.000019 |             0 |                 0 |
    	| logging slow query             | 0.000009 |             0 |                 0 |
    	+--------------------------------+----------+---------------+-------------------+
其它屬性列表
  • ALL – displays all information
  • BLOCK IO – displays counts for block input and output operations
  • CONTEXT SWITCHES – displays counts for voluntary and involuntary context switches
  • IPC – displays counts for messages sent and received
  • MEMORY – is not currently implemented
  • PAGE FAULTS – displays counts for major and minor page faults
  • SOURCE – displays the names of functions from the source code, together with the name and line number of the file in which the function occurs
  • SWAPS – displays swap counts
設定 Profiling 存的 Size
  • mysql> show variables where variable_name=’profiling_history_size’; # 預設是 15筆
關閉
  • mysql> set profiling=0;

PHP中如何使用header发送头部信息

在照彭武兴先生的《PHP BIBLE》中所述,header可以送出Status标头,如

<?php

header(“Status: 404 Not Found”);

?>

就可以让用户浏览器出现文件找不到的404错误,但是我试了这样是不行的。

后 来我到w3.org上查了http的相关资料,终于试出来了如何Header出状态代码(Status),与大家分享。

其实应该是这样 的:

<?php

Header(“http/1.1 403 Forbidden”);

?>

第一部分为 HTTP协议的版本(HTTP-Version)

第二部分为状态代码(Status)

第三部分为原因短语 (Reason-Phrase)

三部分中间用一个空格分开,且中间不能有回车,第一部分和第二部分是必需的,第三部分则是给人看的,可 写可不写甚至乱写。

还有,这一句的输出必须在Html文件的第一行。

下面我给出各代码所代表的意思(是从 w3.org上查到的,够权威了):

* 1xx: Informational – Request received, continuing process

* 2xx: Success – The action was successfully received, understood,and accepted

* 3xx: Redirection – Further action must be taken in order to complete the request

* 4xx: Client Error – The request contains bad syntax or cannot be fulfilled

* 5xx: Server Error – The server failed to fulfill an apparently

valid request

“100” ; Continue

“101” ; Switching Protocols

“200” ; OK

“201” ; Created

“202” ; Accepted

“203” ; Non-Authoritative Information

“204” ; No Content

“205” ; Reset Content

“206” ; Partial Content

“300” ; Multiple Choices

“301” ; Moved Permanently

“302” ; Moved Temporarily

“303” ; See Other

“304” ; Not Modified

“305” ; Use Proxy

“400” ; Bad Request

“401” ; Unauthorized

“402” ; Payment Required

“403” ; Forbidden

“404” ; Not Found

“405” ; Method Not Allowed

“406” ; Not Acceptable

“407” ; Proxy Authentication Required

“408” ; Request Time-out

“409” ; Conflict

“410” ; Gone

“411” ; Length Required

“412” ; Precondition Failed

“413” ; Request Entity Too Large

“414” ; Request-URI Too Large

“415” ; Unsupported Media Type

“500” ; Internal Server Error

“501” ; Not Implemented

“502” ; Bad Gateway

“503” ; Service Unavailable

“504” ; Gateway Time-out

“505” ; HTTP Version not supported

把不带前缀的域名转向到www.域名的Apache 301转向配置样例

各种不同的域名地址对于搜索引擎的除重(deduplication)来说是一个负担,有没有”/”和首页的文件连接,一个域名首页就可以有6个地址:
www.haohtml.com/
haohtml.com/
www.haohtml.com
haohtml.com
www.haohtml.com/index.php
haohtml.com/index.php

如果加上一些参数,比如用于来源跟踪等还会有更多无穷无尽的地址。
www.haohtml.com/?source=foobar

所以搜索引擎鼓励发布者把URL标准化(归一化)。首先就是域名的归一化,原先我的设置为: www.haohtml.com / haohtml.com为别名。现在改为haohtml.com 301转向到www.haohtml.com
<VirtualHost *:80>
ServerName haohtml.com
RewriteEngine on
RewriteRule ^(.*)$ http://www.haohtml.com$1 [R=301,L]
</VirtualHost>

如果没有mod_rewrite也可以设置mod_alias:
RedirectMatch 301 ^(.*)$ http://www.haohtml.com

windows 2003自带的FTP的设置?

windows 2003自带的FTP(iis里)如何设置?打开 Internet信息服务(IIS)管理器 (如下图)

ms_ftp_1

可以看到 Internet信息服务(IIS)管理器 中已经出现了 FTP站点 菜单(如下图)

ms_ftp_2

单击 FTP站点 ,右边呈现的是 相关数据和参数(如下图)

ms_ftp_3

接着,我们来打开它的属性栏 ,右键单击它,单击属性(如下图)

ms_ftp_4

选项卡 FTP站点 下,列出来相关参数,默认的FTP的TCP连接端口是21,这个一般不改它(如下图)

ms_ftp_5

单击 安全账户 选项卡,下面可以勾选匿名,也可以添加用户账号,我们这里只是演示,所以不改它(如下图)

ms_ftp_6

接下来,单击 主目录 设置修改我们这个ftp的指向访问目录,我们这里指向 F盘(如下图)

ms_ftp_7

选择后,再单击下一步(N)> (如下图)

ms_ftp_8

设置访问权限,读取 就是只能看里面内容,能下载,但不能上传;写入,就是可以看,下载,还有上传(如下图)

ms_ftp_9

在你的电脑上打开FTP客户端软件,输入IP,您windows远程桌面的用户名和密码,就可以登录FTP了。

用show table satus查看数据库表的相关信息

显然,我之前那个数据库备份程序太菜鸟了。还用select count(*) 来判断数据库的记录集数,其实完全不用那么麻烦。有一个简单的MML语句今天被我无意间发 现,”SHOW TABLE STATUS FROM `dbname`”,执行后会生成一个记录集。

记录集包含下列字段:

Name: newdb (表名)
Engine: MyISAM (表引擎)
Version: 10 (版本)
Row_format: Dynamic (行格式)

Rows: 56496 (表内总行数)
对于其它存储引擎,比如InnoDB,本值是一个大约的数,与实际值相差可达4050%。 在这些情况下,使用SELECT COUNT(*)来获得准确的数目。对于在INFORMATION_SCHEMA数 据库中的表,Rows值为NULL

Avg_row_length: 4749 (平均每行大小,这里是4.7K)
Data_length: 268352680 (该表总大小,单位字节)
Max_data_length: 281474976710655 (该表可存储上限,这个值所有表都一样,换算出来是256TB,应该用不到那么大吧)
如果给定了数据指针的大小,这是可以被存储在表中的数据的字节总数。mysql5以后的版本所能支持的最大存储容量是非常大的,上面的 列子为256TB。这时表的最大存储容量主要受限于OS了。不过到现在,Linux ext3 fs支持单个最大文件尺寸2T了,所以基本这个问题不必过于担心了。存储引擎是innodb的话,这个值在show table status显示的值总是为0,不知道为什么。

Index_length: 2464768 (索引大小)
Data_free: 0 (数据多余,即数据碎片)
Auto_increment: 69296 (自动累加ID 6W9,而前面的行数只有5W6,说明我有删掉了1W3笔数据)
Create_time: 2009-10-13 10:46:14 (这个不用说了吧)
Update_time: 2009-10-13 11:08:15
Check_time: 什么时候表被最后一次检查。不是所有的存储引擎此时都更新,在此情况下,值为NULL
Collation: gb2312_chinese_ci (编码)
Checksum: 活性校验和值
Create_options: row_format=DYNAMIC (为什么要重复一遍?)
Comment: (表注释)