顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2016年7月21日 星期四

[Wordpress] A Method for combining both of plain and custom URLs

In Wordpress, we usually set the custom URLs for the purpose of SEO.
For the old plain URLs which are previously published to other sites, we should maintain them by redirecting to the new custom URLs.

The htaccess setting can simply do the redirection to a static page, such as response 404 not found or see other new site. Sometimes, we don’t know how many old plain urls are published. It cannot fulfill our needs for mapping the old plain URLs to the corresponding new URLs. For example, we want to create the following rules of redirections:

redirect http://example.com/?p=123 to http://example.com/custom/link

redirect http://example.com/?p=456 to http://example.com/the/other_links

There may be more old URLs we don’t be aware of are published. A general soluton is to keep both of the plain and custom URLs.

In this case, we use add_filter function provided by Wordpress.
Navigate your directory of currently used theme and find functions.php and put a piece of codes as follows:

function compatible_link( $postlink, $redirect) {
    $id = get_query_var('p', -1);
    if($id !== -1 && is_numeric($id)) {
        return get_permalink($id);
    }
    return $postlink;
}

add_filter('post_type_link', 'compatible_link', 10, 2);

Done! Enjoy your website.

REFERENCE:

  1. Rewriting URLs in WordPress: Tips and Plugins. URL: http://www.hongkiat.com/blog/wordpress-url-rewrite/
  2. Wordpress的Hook機制與原理. URL: http://www.mrmu.com.tw/2011/10/10/wordpress-hook/
  3. Custom post type with post_id in permalink structure. URL: https://wordpress.org/support/topic/custom-post-type-permalink-structure
  4. Custom Login Url with plain permalink format. URL: https://wordpress.org/support/topic/custom-login-url-with-plain-permalink-format
  5. Function Reference/get query var. URL:https://codex.wordpress.org/Function_Reference/get_query_var
  6. CREATING CUSTOM PERMALINKS IN WORDPRESS. URL:http://wp-events-plugin.com/tutorials/creating-custom-permalinks-in-wordpress/

2015年3月19日 星期四

The Problem of Ternary Operators in PHP

How do you think of the ternary operators in PHP? Recently, I’’ve used the code written as below:
echo (1?"Y":0?"N":"");

The code returns “N” but not “Y”. How does it work?

It shoud be like:

echo ((1?"Y":0)?"N":"");

Finally, please follow the advice from PHP docs:
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious.
Then you will make better code in PHP.

Reference

1. Mac Taylor, “Stacking Multiple Ternary Operators in PHP, “ stackoverflow [Online]. Available: http://stackoverflow.com/questions/5235632/stacking-multiple-ternary-operators-in-php

2014年6月12日 星期四

An easy way to fix SQL injection and XSS

In my current case, I find some PHP codes have flaws in SQL injection and  XSS.

After analyzing the codes, I find that the URL pameters are usual in numeric type.

So, I just add the codes in the PHP file below:

/* Jyun-Yao Huang modified on 2014/06/12: 
* Check the values of $_GET are in numeric type or not.
* If some values are not in numeric type, then set them as null.
* If some values are in numeric, then strip them.
*/
foreach ($_GET as $key => $value) {
if (! is_numeric($value)) {
$_GET[$key] = null;
} else {
$_GET[$key] = mysql_real_escape_string($value);
}
}


2014年2月22日 星期六

[PHP] Be discreet to use crypt function

Recently, the PHP’s crypt function crashed in my designed new system. In fact, in my database design, the length of password field is 64. It ran fine at CentOS 5.5 with PHP 5.2.x. However, when I moved this system to Debian 7.3.0 with PHP 5.4.x, it was failed to generate the password.

In CentOS 5.5, it used MD5 as default crypt function and the php’s crypt also did (CRYPT_MD5). The MD5 hash generates 34 characters including 12 prefix salt characters like “$1$ … $”and 22 hashed characters in base64 encoding (128 digits / 6 bits_per_characters := 22 characters ), so the legnth of hashed password is enough with the field size = 64.

Unfortunately, the new operating system uses SHA512 crypt function as default crypto scheme and PHP’s crypt function also accepts the crypto scheme as default. In the default setting, the SHA512 hash function generates 102 characters including 16 prefix salt characters like “$6$…$” and 86 hashed characters in based64 encoding (512 digits / 6 bits_per_characters := 86 ). Therefore, it has no extra data length to save whole 102 characters because of the default field size is 64. Finally, the password verifier is also failed. :(

REFERENCE

  1. PHP: crypt function.[Recommend]
  2. Wiki: MD5
  3. Linux Programmer’s Manual : Crypt (3) [Recommend]
  4. What is the output length of PHP crypt()?

2013年1月27日 星期日

[PHP] MySQL Backup by GMail

[Chinese]
        先前已經介紹過有一個好用的MySQL資料庫備份工具(使用PHP):MySQLDump。但是預設的寄信函式,是透過SMTP伺服器寄信,這對於沒有SMTP伺服器的使用者而言,可就傷腦筋了。原先我有找到一篇教學,使用GMail的SMTP伺服器做備份,可惜連結已經失效了。
筆者只好自行撰寫,由於程式碼使用GPL授權,基於分享的精神下,便公開給各位。

        程式碼下載處:請按我。

[English]
I've introduced an useful backup tool for backuping MySQL databases (using PHP): MySQLDump. However,the default send mail function uses the SMTP server to send mails, and it may cause some problems for the users without SMTP servers. Initially, I've found one tutorial for teaching that how to backup MySQL databases by GMail SMTP server, but the link of source code is unavailable. Thus, I rewrite it by myself. Because the project uses GPL license, I share my project with all of you.

The download link: Click Here.

2012年12月20日 星期四

[PHP] The resources about calling windows programs in PHP

I've developed  one library about security and make as a .dll package. Now, I want to use the dll through PHP because my significant webdesign skill is PHP, not ASP.Net. The other reason is that I don't prefer to install two different web platforms in a non-enterprise server.

I've searched some tutorials about that how to use or develop dll in PHP. To my knowledge, there are at least two ways to apply.

Develop One Self-developed C/C++ .DLL File

The dll can be only written by C++. After completing .dll file, I just put the dll into the ext_dir of PHP and set some configurations in php.ini . There are some resources listed in below:
  1. Creating a PHP 5 Extension with Visual C++ 2005
  2. Win32 PHP extension development

Use COM/DOTNET Classes in PHP

The another way is that use COM/DOTNET classes built in PHP to call self-developed COM assembly objects.
  1. Calling a DLL with PHP
  2. The DOTNET class
  3. COM and .Net (Windows)


2012年11月12日 星期一

[PHP] Solution for the Message in PHPMyAdmin: Query was empty!

During the few days, I've encountered a problem for operating PHPMyadmin at my workstation. It is very strange situation while  clicking the viewing button in PHPMyadmin. Whenever I click the viewing button, the result is always the shown message with "query was empty" in PHPMyAdmin.
[Note: Nov 5, 2012 - I've also found the bug existed in phpMyAdmin-3.5.4-rc1. No matter in 2.11.11 or 3.5.4, this bug is still not solved. Further, I have submitted this bug to the SourceForge for PHPMyAdmin ] 

2012年10月27日 星期六

[PHP] 將_GET與 _POST陣列的變數,轉為全域變數的兩種作法

因為最近要改別人寫的舊專案,該專案的寫法為需要將_GET與_POST陣列的變數,改為全域變數。也就是說,我們可以用$register 直接存取$_GET['register'];

像這類的要求,可以分為兩種解決方法:

1. 如果管理員有給.htaccess操作的權限:
那麼,可以在.htaccess加入以下語法:

php_value register_globals On
php_flag register_globals On

2. 如果管理員沒有給.htaccess操作的權限:
我從這篇得到的方法,只要寫一個set_globals.php的程式,程式碼內容如下:

每當有程式要執行時,先加一句 require('set_globals.php'); 或include('set_globals.php'); 引入該檔案即可。

參考資料
[程式][PHP] 如何快速接收POST或是GET的變數,並轉存。

2012年9月29日 星期六

[PHP] 幸運輪盤點名系統

這學期為了教課,班上人數有60人。一個一個點又太麻煩,想說用抽點的。寫個程式來幫助我產生抽點名單。:)
首先,準備MySQL資料庫與一張資料表,以這個範例來說,我有張student的資料表,schema如下:

資料表:student
欄位名稱 資料類型描述
noVARCHAR(10)學生學號
nameVARCHAR(60)學生姓名


準備我要的資料庫設定檔:

準備好資料庫連線Class:
準備好點名程式:
上述東西準備好後,就可以開始點名了。:P
備註:我好像吃飽太閒! =.=

2012年8月2日 星期四

[PHP] INI設定與Debug教學

在DoubleService的教學中提及,在開發PHP測試函式庫時,由於各個伺服器對於php.ini的設定可能都不一樣。因此,在測試函式庫之前,最好加入以下幾行,以確保自己的程式正確無虞。


有了上述的程式碼,就可以輕鬆Debug囉。另外,請記得在將程式碼交給客戶之前,把Debug模式關閉喔。
至於為什麼要通過這些檢查呢?DobuleService曾經提及:


1. 避免變數名前後不小心打錯。
2. 避免不存在的鍵值,隱藏可能的邏輯錯誤。
3. 強制變數初始化,避免變數交互引用覆蓋或誤用。
4. 強制正規化變數型態,避免隱藏性錯誤。
5. 自然養成嚴謹的編程習慣。
6. 強化程式本身的環境可攜性。


另外,值得一提的是,並非所有ini設定都可以透過ini_set()這個函式來設定的,只有被系統規範為PHP_INI_ALL或是 PHP_INI_USER的選項,才可以使用ini_set()。若不清楚哪些可設定的話,請參見twphp提供的資料:http://tw.php.net/manual/en/ini.list.php


參考資料

  1. 開發PHP時可能會做的環境設定
  2. PHP開發開啟E_ALL的重要性
  3. List of php.ini directives
  4. PHP:ini_set()可更改的項目
  5. Why is the result of E_ALL | E_STRICT the same as only E_ALL?
  6. php.ini開啟error設定

[PHP] PEAR、MVC、ZendFramework相關教學資源

PEAR 教學資源




其它相關教學資源:

2012年5月5日 星期六

[PHP] 隱藏圖片網址

最近在想說PHP如何隱藏圖片網址?
因此,在手癢之下寫了一段PHP函數,來隱藏我的程式碼。
有了以上的程式碼,我可以寫一個demo_picture.php,來負責處理我要顯示的圖片資源。

從上例可以得知,我們只要輸入 http://your_domain/demo_picture.php?resource_id=XXRBcFwvGmxNeV1tVS5UbwgvXy9QL0x3HWhHLw9oGDAMcEByFmxQLgRpUg==&type=picture 就可以得到圖片,而且圖片網址是隱藏的喔。

2012年2月29日 星期三

URL最大長度

URL最大長度的限制,在標準規範中並無定義。但是,Microsoft Internet Explorer 有規定,需在
2,083個字元內。其他瀏覽器,如Firefox、Chrome、Opera等,至少為65,536個字元。

但Apache Server可接受的字元數為4,000,IIS則為16,384。

因此,若開發人員想要利用URL傳遞參數的話,URL長度盡量在2,000個字元內,會比較安全。


參考資料

  1. URL的最大長度限制
  2. 最大 URL 長度是在 Internet Explorer 中的 2,083 字元

2011年12月25日 星期日

[PHP] 多國語言網站設計

就筆者目前的整理,在設計多國語言網站時,有三種作法。
(1) 採用簡易的PHP專案設計模式,架構如下:

language/
|---> English/about.php
|---> Zh-TW/about.php
這種設計模式,最為簡單明瞭。若要用這種設計模式,可用的library有Wu-Boy設計的php-i18n,該作者寫了兩篇教學文,可以看一看:

  1. PHP 多國語系製作 i18n library 筆記 (一)
  2. PHP 多國語系製作 i18n library 筆記 (二)

(2). 透過PHP gettext套件,實現UTF-8實現多國語言。該套件需要讀取事先定義好的.po檔(.po檔常見的編輯器為GNU POEDITOR),gettext套件的教學文章筆者蒐集了兩篇,有興趣的讀者不妨看一看:

  1. 使用 gettext 來實做 PHP 多國語系支援(I18N)
  2. php gettext方式實現UTF-8國際化多語言(i18n)

(3). 透過新增資料庫欄位的方式,實作多國語言。最簡單的示意教學文在這邊。不過實作時仍有許多方面要考慮就是了。

[PHP] 實作時應注意的SQL Injection

SQL Injection一直是Web Designer 頭痛的問題,所幸我們可透過一些習慣設定、檢查與驗證機制,來防堵SQL Injeciton攻擊手法。

例如,在「PHP SQL Injection 和 XSS 的偵測程式 和 程式撰寫注意事項 - 2008」這篇文章中,對於php.ini設定檔、參數傳遞、DB操作等事項,逐一簡要列出要點,有興趣的讀者,可以看一看。

另外,對於較為詳細的PHP SQL Injection檢查與驗證機制,筆者在這邊推薦以下幾篇文章給讀者閱讀:

  1. mysql - sql injection prevention
  2. PHP+MySQL 環境下 SQL Injection 攻防戰
  3. [PHP]解決網站被 SQL injection 攻擊

僅此,筆者希望PHP Designer都能夠做到這些基本工,以防止自己設計的網站被黑客破解。

2011年12月11日 星期日

[PHP] PHPMailer 基本範例 (含發送附件檔)

參考了一下人家的寫法,加入附件檔,並寄信出去。

如上程式碼,我們只要改寫帳號、密碼,寄件人資訊以及收件人即可。

切記!PHP記得打開openssl功能喔!這個功能由 php-common 這個套件所包含。
相關參考資料
  1. 實作PHPMailer郵件函式庫
  2. PHP安裝SSL方法(in Windows)
  3. 使用GMail的SMTP送信
  4. PHP教學─利用GMail的SMTP製作線上客服系統

2011年9月29日 星期四

CAPTCHA

        CAPTCHA全名是(Completely Automated Public Turing test to tell Computers and Humans Apart),中文翻譯為「全自動公開區分電腦與人類圖靈測試」,由於近年來黑客用機器人程式破解網站帳號密碼的事件不斷發生,因此科學家藉由人類影像辨識遠優於電腦程式的特性,發展出此一機制。

        目前大家比較常在用的CHAPTCHA為 reCHAPTCHA,此一程式是由卡內基美隆大學發展出來,他們原本藉由電腦來辨識古老書籍,讓圖書電子化。但是電腦的辨識度不足,執行困難的情況下,便藉由這個程式,讓人類幫他辨識。目前此一計畫由Google支援。
    reCHAPTCHA官方網站:http://www.google.com/recaptcha

        另外,筆者在此介紹PHP版本的CHAPTCHA產生器:Securimage,此一程式套件也是一個免費套件。協助程式設計師開發出更為安全的網頁程式。
    Securimage官方網站:http://www.phpcaptcha.org/
Reference

  1. 小薑雜談:CAPTCHA
  2. reCAPTCHA
  3. Securimage