• 2012-03-08

    PHP的优势

    Views: 22565 | 5 Comments

    以前偶尔被人问到, 为什么你(和大部分互联网公司)做Web开发要选择PHP, PHP有什么好处. 简单的回答便是"PHP简单,开发快速".

    但是, ASP.NET不简单吗? ASP.NET上手不快吗? Python呢?

    有些人用各种PHP框架能快速搭建简单博客的例子来说明PHP的优势, 但这全是狗屎! 实际的业务不可能是博客那么简单.

    我认为, PHP的最大优势便是他的数据结构和内置函数, 具体地说便是字符串和数组, 以及字符串和数组的函数.

    PHP的字符串既能表示一般文本, 也能表示任意二进制数据, 也就是说, PHP的字符串就是一段内存. PHP的的字符串操作函数囊括了大部分常见和不常见的文本操作: 截取, 查找, 正则, 字符集编码转换...每一个都是一把利器.

    PHP的数组是整合了列表和哈希表的数据结构. 由于"树"是最能描述现实世界的数据结构, 而PHP的数组可以(轻松地)表示任意树, 所以, PHP的数组也最能描述现实世界(建模).

    如果要用一句话来描述PHP的优势, 我会用这一句:"PHP语言的数据结构和内置函数, 可以几乎直接地描述和处理实际业务. PHP是计算机与现实业务的最直接胶合剂."

    Posted by ideawu at 2012-03-08 10:43:18
  • 2012-03-01

    MySQL 基本配置

    Views: 20803 | No Comments

    query_cache_size, tmp_table_size 这两个选项一定要设置!

    # The following options will be passed to all MySQL clients
    [client]
    #password	= your_password
    port		= 3306
    socket		= /home/work/mysql/tmp/mysql.sock
    
    # Here follows entries for some specific programs
    
    # The MySQL server
    [mysqld]
    port		= 3306
    bind-address = 127.0.0.1
    socket		= /home/work/mysql/tmp/mysql.sock
    #skip-locking
    max_allowed_packet = 1M
    table_open_cache = 16
    sort_buffer_size = 8M
    read_buffer_size = 512K
    read_rnd_buffer_size = 256K
    net_buffer_length = 8K
    thread_stack = 128K
    
    # Size of the Key Buffer, used to cache index blocks for MyISAM tables.
    # Do not set it larger than 30% of your available memory, as some memory
    # is also required by the OS to cache rows. Even if you're not using
    # MyISAM tables, you should still set it to 8-64M as it will also be
    # used for internal temporary disk tables.
    key_buffer_size = 8M
    
    # If the temporary file used for fast index creation would be bigger
    # than using the key cache by the amount specified here, then prefer the
    # key cache method.  This is mainly used to force long character keys in
    # large tables to use the slower key cache method to create the index.
    myisam_sort_buffer_size = 8M
    
    # Query cache is used to cache SELECT results and later return them
    # without actual executing the same query once again. Having the query
    # cache enabled may result in significant speed improvements, if your
    # have a lot of identical queries and rarely changing tables. See the
    # "Qcache_lowmem_prunes" status variable to check if the current value
    # is high enough for your load.
    # Note: In case your tables change very often or if your queries are
    # textually different every time, the query cache may result in a
    # slowdown instead of a performance improvement.
    query_cache_size=16M
    
    # Maximum size for internal (in-memory) temporary tables. If a table
    # grows larger than this value, it is automatically converted to disk
    # based table This limitation is for a single table. There can be many
    # of them.
    tmp_table_size=16M
    
    
    # Don't listen on a TCP/IP port at all. This can be a security enhancement,
    # if all processes that need to connect to mysqld run on the same host.
    # All interaction with mysqld must be made via Unix sockets or named pipes.
    # Note that using this option without enabling named pipes on Windows
    # (using the "enable-named-pipe" option) will render mysqld useless!
    # 
    skip-networking
    server-id	= 1
    
    # Uncomment the following if you want to log updates
    #log-bin=mysql-bin
    
    # binary logging format - mixed recommended
    #binlog_format=mixed
    
    # Uncomment the following if you are using InnoDB tables
    #default_storage_engine=InnoDB
    
    innodb_data_home_dir = /home/work/mysql/var/
    innodb_data_file_path = ibdata1:10M:autoextend
    innodb_log_group_home_dir = /home/work/mysql/var/
    # You can set .._buffer_pool_size up to 50 - 80 %
    # of RAM but beware of setting memory usage too high
    innodb_buffer_pool_size = 32M
    innodb_additional_mem_pool_size = 4M
    # Set .._log_file_size to 25 % of buffer pool size
    innodb_log_file_size = 10M
    innodb_log_buffer_size = 16M
    innodb_flush_log_at_trx_commit = 1
    innodb_lock_wait_timeout = 50
    
    [mysqldump]
    quick
    max_allowed_packet = 16M
    
    [mysql]
    no-auto-rehash
    # Remove the next comment character if you are not familiar with SQL
    #safe-updates
    
    [myisamchk]
    key_buffer_size = 8M
    sort_buffer_size = 8M
    
    [mysqlhotcopy]
    interactive-timeout
    
    Posted by ideawu at 2012-03-01 10:41:23 Tags:
  • 2012-02-18

    网站如何禁止非浏览器访问?

    Views: 13170 | No Comments

    网站如果禁止非浏览器访问, 一般主要还是防止自动 Spam, 主要是:

    • 防止网站被机器人恶意抓取
    • 防止网站被机器人恶意提交

    判断恶意请求(程序和软件直接发送 HTTP 请求)的方法有很多, 比如同 IP 请求频率是. 但一旦判定某个请求为异常行为之后, 并不适合直接关闭该连接, 或者返回出错页. 因为绝大多数的判定方法都无法做到很高的正确率, 其主要原来就是缺少"交互性". "交互性"是判断人和机器的主要因素.

    这里判断一种利用交互性来判别是否浏览器正常访问的方法:

    1. 当初步判定请求为异常时, 给该请求返回一个动态令牌, 要求对方在下次重新带着该令牌来访问原先请求的资源;
    2. 如何告知用户进行这个行为, 根据交互性的强弱依次为:

      1. 设置 Cookie
      2. 通过 JavaScript 脚本的执行(设置 Cookie, 生成带令牌的链接并访问)
      3. 显示网页表单, 要求用户进行点击和输入等操作(单纯的无输入表单, 图片验证码, 声音验证码)
    3. 第一种设置 Cookie 太过于简单, 因为一般的机器人都能执行该行为. 第二种 JavaScript 脚本的方式, 其实已经能阻挡大部分机器人. 第三种是非常好的识别那漏掉的一小撮高级机器人, 但同时也可能影响正常用户体验.
    Posted by ideawu at 2012-02-18 11:50:43
  • 2011-10-12

    PHP重用curl句柄, CURLOPT_HTTPGET的BUG

    Views: 23632 | No Comments

    重用一个CURL句柄时, 发现curl_setopt($ch, CURLOPT_HTTPGET, TRUE) 不起作用. 期望在调用这条语句之后发起请求, 应该发送的是GET, 但看服务器log, 却使用了和前一次请求相同的HTTP方法.

    PHP脚本:

    <?php
    $url = 'http://www.ideawu.net/';
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_exec($ch);
    
    curl_setopt($ch, CURLOPT_HTTPGET, true); // 错误! BUG
    curl_exec($ch);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // 正确
    curl_exec($ch);
    ?>
    

    web server log:

    124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net
    124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net
    124.127.130.50 "2011-10-12 18:55:09" "GET / HTTP/1.1" 200 3516 "-" www.ideawu.net
    

    这个BUG目前还没找到相关的资料.

    补充: 不仅仅是CURLOPT_HTTPGET, CURLOPT_POST也有同样的问题. 所以, 结论是: 只有CURLOPT_CUSTOMREQUEST才是正确的方法.

    Posted by ideawu at 2011-10-12 18:55:51
  • 2011-09-09

    PHP浮点数显示和转成字符串

    Views: 21337 | 2 Comments

    你可能会觉得PHP中将浮点数(float)转成字符串非常简单, 但是, 常用的方法隐藏着严重的bug. 因为, PHP在处理浮点数时有非常不合理的做法, 会有精度丢失. 经研究, 其实是PHP在显示浮点数时的问题, 也可以说是BUG. PHP内置的echo, var_dump, json_encode, 字符串拼接等函数(指令)在显示浮点数时都有问题, 导致精度丢失.

    <?php
    $a = 1315537636.338467;
    printf("%f", $a); echo "\n";
    echo $a . "\n";
    echo $a; echo "\n";
    

    结果

    1315537636.338467
    1315537636.3385
    1315537636.3385
    

    也就是说, 用PHP最顺手的方法将浮点数转成字符串或者显示是不行的, 必须使用printf/sprintf将浮点数转成字符串.

    Posted by ideawu at 2011-09-09 11:33:47
  • 2011-09-04

    Apache用mod_rewrite配置子域名

    Views: 23747 | 2 Comments

    虽然用vhost可以支持子域名, 但不方便.

    RewriteCond $1 !^bbs/
    RewriteCond %{HTTP_HOST} bbs.example.com
    RewriteRule (.*) /bbs/$1 [L]
    

    RewriteCond $1 !^bbs/ 避免内部无限redirect.

    Continue reading »

    Posted by ideawu at 2011-09-04 22:26:02 Tags:
|<<<456789101112>>>| 8/28 Pages, 163 Results.