2011-04-26

PHP讀取EXCEL 2007 2003

一個功能強大的Excel函式庫,一般的讀寫都可以處理,且支援圖片插入。
官方網站:http://www.codeplex.com/PHPExcel/


/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';


if (!file_exists("05featuredemo.xlsx")) {
exit("Please run 05featuredemo.php first.\n");
}

echo date('H:i:s') . " Load from Excel2007 file\n";
$objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");

echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));


// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";

// Echo done
echo date('H:i:s') . " Done writing files.\r\n";

PHP 使用 Apache WWW-Authenticate 認證身份

<?php
$user = '認證帳號';
$pw = '認證密碼';

if(!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_PW']=='') {
    Header("WWW-Authenticate: Basic realm=\"login\"");
    Header("HTTP/1.0 401 Unauthorized");
    die('認證失敗');
} else {
    if($_SERVER['PHP_AUTH_USER']!=$user || $_SERVER['PHP_AUTH_PW']!=$pw) {
        Header("WWW-Authenticate: Basic realm=\"login\"");
        Header("HTTP/1.0 401 Unauthorized");
        die('帳號或密碼錯誤');
    }
}

echo "認證成功";

json xss 防範

更改MIME文檔類型來防範。 測試的時候發現text/plain 、application/x-zip-compressed 是會被IE解析了。
 其他應該還是會有的,沒有一個一個測試。

header ( "Content-type: application/json" ) ; 

本文地址: http://www.woyigui.cn/2009/04/28/json-xss/

2011-04-25

mysql_num_rows VS COUNT 效率問題分析

一個直觀的對比 
測試數據: 
條數:139764條 
數據表大小:500M 

結果: 
fetch_num_rows 用時:35.272329092026 
count(*) 用時:0.071956872940063 

如果單純統計數量當然是count(*) 
fetch_num_rows必須遍歷數據庫以後才能得出效率低於count(*)

原文參考:http://jb51.net/article/26935.htm

2011-04-21

Mysql 文字轉日期排序

SELECT STR_TO_DATE(concat(AC_Month,'/', AC_Year + 1911), '%m/%Y') as dd,s. *
FROM tndg_activities_month AS m, tndg_activities_service AS s
WHERE s.A_SERIALNO = m.SERIALNO
AND s.MemberID = '??'
ORDER BY dd

解決javascript的window.close()在IE7會有提示的問題

<script language=javascript> 
window.opener=null
window.open("","_self"); window.close();
</script>

2011-04-18

Mysql數據庫like模糊查詢中文字段不準確的臨時解決辦法


select  *  from test.news where  binary title like  ' % s % '

字母區分大小寫
select  *  from test.news where  binary  ucase ( title )  like   ' % s % '

2011-04-14

PHP 求質數

function IsPrime($i) 

if($i<2) 

return false; 

//var $iterator; 
for($iterator = 2 ; $iterator <= sqrt($i) ; $iterator++) 

if($i % $iterator==0) 

return false; 


return true; 

window.open所開的小視窗出現在螢幕正中間

<script>
t = 200;
l = 200;
window.open('about:blank','','width='+l+',height='+t+',top='+(screen.availHeight/2-t/2)+',left='+(screen.availWidth/2-l/2));
</script>