2013-03-28

CODE - Dovehair 的 php 筆記



初步的 sql injection 防禦


1. 把 php.ini 裡的 magic_quotes_gpc 設成 on
    若沒有查看 php.ini 的權限, 可用 echo get_magic_quotes_gpc(); (0:off / 1:on)
2. 承上, 若是 off 則可利用 addslashes() 或 mysql_real_escape_string() 來處理
# 用 addslashes() 來做基本的 sql injection 防禦
$name = addslashes($_POST['try_imput_name']);
$reason = addslashes($_POST['reason']);
$other = addslashes($_POST['try_other_input']);
$email = addslashes($_POST['try_input_email']);
$mobile = addslashes($_POST['try_from_tel']);
echo $trailModel->add($conn, $name, $reason, $other, $email, $mobile);
ref : http://jishus.org/?p=666

Upload & Preview Image

這裡使用 ajaxfileupload.js 跟 jquery.livequery.js (因為萬惡的 ie8)
// 記得要先 include ajaxfileupload.js 與 jquery.livequery.js
$(document).ready(function() { 
	// 這裡原本用 .live 就好, 但 ie8 不吃
	// 還好用 .livequery 這個外掛可以解決
    $('#pic').livequery('change', function() {
        // 圖還沒傳完, 不能按下一步
        $('#stop02_upload_next_btn').removeClass('upload_next_btn');
        // 顯示 loading icon
    	$('#loading').ajaxStart(function(){
    		$(this).show();
    	});
    	/*.ajaxComplete(function(){
    		$(this).hide();
    	});*/
        
    	$.ajaxFileUpload({
    		url: './Image.php',
    		secureuri: true,
    		fileElementId: 'pic', // 這裡指 input type="file" 的 id/name
    		dataType: 'json',
    		//data: {name:'logan', id:'id'},
    		success: function (data, status) {
    			if (typeof(data.error) != 'undefined') {
    				if (data.error == 'true') {
    					// failed
    				} else {
    					// success
    				}
    			}
    		},
    		error: function (data, status, e) {
    			alert(e);
    		}
        })
    });
});
ref :
http://www.phpletter.com/Our-Projects/AjaxFileUpload/


js 的資料檢查

email : /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/
市話 : /^[0-9]{6,11}$/ 僅檢查11個數字
手機 : /^09[0-9]{8}$/
function checkEmail(no)
{
    if (no.search(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/)!=-1) {
        return true;
    } else {
        return false;
    }
}
function checkMobile(no)
{
    if (no.search(/^09[0-9]{8}$/)!=-1) {
        return true;
    } else {
        return false;
    }
}
function checkTel(no)
{
    // 可以吃 11 個數字
    if (no.search(/^[0-9]{6,11}$/)!=-1) {
        return true;
    } else {
        return false;
    }
}
說明 : /^[A-Z]\d{9}$/ 就是一個正規式,[A-Z] 代表由 A 至 Z 的所有可能英文字母,\d 代表由 0 至 9 的數目字(事實上也可以寫成 [0-9]),{9} 則代表需要有九個數目字,^ 代表字串開始位置,$ 代表字串結束位置,因此 /^[A-Z]\d{9}$/ 就代表可以比對身份證字號的正規式。formElement.value 代表使用者輸入的字串,re.test() 則會傳回 true 或 false,代表比對是否成功。若要不限定是大寫英文字母,只需將正規式改成 /^[a-zA-Z]\d{9}$/g 就可以了!(注意:若不加入 ^ 和 $,那麼 /[A-Z]\d{9}/ 就會比對到其他不合法的身份證字號,例如 AGF123456789 或是 F1234567890 等。因此,加入 ^ 和 $ 可保證比對正確的字串一定是由由一個大寫英文字母加上九個數字所構成

ref :
http://www.wretch.cc/blog/johnsswu/1237216


匯出 csv 的編碼與符號跳脫

//...
//...
//...
$filePath = Config::getIncludePath() . 'file/' . $csvName;
$fh = fopen($filePath, 'w');

$line = "\"姓名\",\"受損原因\",\"信箱\",\"手機\",\"上傳時間\"".chr(13); # 表頭
fwrite($fh, iconv('UTF-8', 'BIG5', $line));
foreach ($rows as $row) {
    $name = $row['name'];
    $reason = $util->getReasonName($row['reason'], $row['reason']=='4'?$row['other']:'');
    $email = $row['email'];
    $mobile = $row['mobile'];
    $ctime = $row['ctime'];
    # 要用 \" 跳脫一下特殊符號; 就算這樣若 input data 裡有 " 仍然會錯, 所以要用下面的 readFromDB() 把 " 濾掉
    $line = "\"$name\",\"$reason\",\"$email\",\"$mobile\",\"$ctime\"".chr(13);
    fwrite($fh, iconv('UTF-8', 'BIG5', $line)); # 將 utf-8 轉換為 big5, 沒辦法, 因為是 ms 的東西
}
fclose($fh);
//...
//...
//...
public function readFromDB($str)
{
    return str_replace("\"", "", $str);
}

description 自動斷行

後端由 db 取出時 : <?=nl2br($row['content'])?>

admin 的 login session

ref : http://joy0626.pixnet.net/blog/post/25272532-%5B%E7%AD%86%E8%A8%98%5D-php---session  


3 則留言:

  1. upload preview image 補充
    ie8 抓不到選完檔後的 onchange 動作, 所以無法繼續觸發 ajax upload load
    多拉一個上傳鈕讓 user 自己按便解決, 但畫面就弱了
    把 live 改成 bind 就好了 => 但無法做第二次上傳

    有三個解法
    1. upgrade jquery
    2. http://blog.miniasp.com/post/2010/08/29/jQuery-142-live-API-change-event-on-IE.aspx => 沒試過
    3. 外掛 livequery => 最後用這個
    http://taswar.zeytinsoft.com/2010/05/04/jquery-live-change-doesnt-work-in-ie/
    https://github.com/brandonaaron/livequery

    回覆刪除
  2. 匯出 csv 部份的補充

    以 iconv('UTF-8', 'BIG5', $line) 執行時, 有時會發生問題, 因為有的 utf-8 字, big5 無法解析
    碰到這樣的情況, iconv 預設會終止該筆轉換, 造成處理後的資料筆數有少
    此時可以改為 iconv('UTF-8', 'BIG5//IGNORE', $line),
    加上 IGNORE 以忽略某些字元無法轉換

    回覆刪除
    回覆
    1. ref : http://www.ps3w.net/modules/psbb/?op=openthr&lead=1109

      刪除