Javascript Function สำหรับการทำ URL Encode

ในบทความเรื่อง Cross site scripting ผมต้องการเครื่องมือที่จะทำการ url encode ที่ซึ่งจะสามารถแปลงตัวอักษรทุกตัวรวมทั้ง Alphanumeric characters ด้วยผมจึงการทำการค้นหาจาก google ก็พบ Javascript Url encoder/decode จาก www.webtoolkit.info ซึ่งรองรับ UTF-8 encoding ด้วย แต่ฟังก์ชันดังกล่าวก็ไม่ได้เข้ารหัสตัวอักษรทุกตัว ผมจึงต้อง ฟังก์ชันดังกล่าวได้ Javascript code ด้านล่าง

 function urlEncode(inputString, encodeAllCharacter){
       var outputString = '';
       if (inputString != null){
         for (var i = 0; i < inputString.length; i++ ){
            var charCode = inputString.charCodeAt(i);
            var tempText = "";
            if (charCode < 128) {
                if (encodeAllCharacter)
                {
                  var hexVal = charCode.toString(16);
                  outputString += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();  
                } else {
                  outputString += String.fromCharCode(charCode);
                }
                            
            } else if((charCode > 127) && (charCode < 2048)) {
                tempText += String.fromCharCode((charCode >> 6) | 192);
                tempText += String.fromCharCode((charCode & 63) | 128);
                outputString += escape(tempText);
            } else {
                tempText += String.fromCharCode((charCode >> 12) | 224);
                tempText += String.fromCharCode(((charCode >> 6) & 63) | 128);
                tempText += String.fromCharCode((charCode & 63) | 128);
                outputString += escape(tempText);
            }
         }
       }
       return outputString;
    }

ด้านล่างเป็นตัวอย่างโปรแกรมที่ผมเรียกใช้ฟังก์ชันดังกล่าวทดลองใช้งานกันได้ครับ

Plain Text:
Encode All Characters:
Encoded Text:

Code ที่คุณ Post มานี่ใช้งานได้ดีเลยครับ เป็นประโยชน์กับผมมาก ขอบคุณมากๆ เลยครับ

การทำ url encode คืออะไรหรอครับ

เด็กน้อยอยากรู้ด้วย...อิๆๆ

ขอบคุณครับ เป็นประโยชน์กับมือใหม่อย่างผมเหมือนกันครับ

ส่วนที่คุณ #2 ถามมาขอตอบแบบมือใหม่นะครับ อาจไม่ถูกทั้งหมด
การทำ URI ENCODE เป็นการเข้ารหัสข้อความให้เป็น Code ที่เราต้องการ เช่นใช้ฟังก์ชั่น
encodeURIComponent() เข้ารหัสข้อความ ASCII ให้เป็น UTF-8 ก่อนส่งข้อมูลกลับไปให้ server
ซึ่งมีประโยชน์คือป้องกันคนทั่วไปมองเห็นข้อมูลที่แนบไปกับ URL (กรณีส่งแบบ GET) และยังทำให้ไม่ต้องกังวลกับเครื่องหมาย & ในข้อมูลเปลียนไปทำให้การส่งข้อมูลแบบ GET ไม่เกิดปัญหาด้วยคับ

แก้ไข #3 นะครับ
URL encoding ไม่ได้เป็นการแปลง ASCII ให้เป็น UTF-8 แต่เป็นการนำเสนอตัวอักษร (encode) ในอีกรูปแบบหนึ่งที่สามารถส่งข้อมูลนั้นผ่านทาง URL ได้

ตามข้อกำหนด "RFC 1738: Uniform Resource Locators (URL) specification", อักษรที่สามารถใช้ใน URL โดยไม่ต้อง encode ได้แก่ [A-Za-z0-9] และอักษรพิเศษ "$-_.+!*'(),"

นอกจากนี้อักษรพิเศษบางตัวยังมีความหมายใน URL เช่น "+" จะใช้แทน ช่องว่าง (white space) เป็นต้นครับ ฉะนั้นจึงเป็นเหตุให้เราต้องทำการ encode ครับ

ส่วนการป้องกันคนทั่วไปมองเห็นข้อมูล เป็นความเข้าใจผิด ๆ ครับ เพราะว่า การ encoding/decoding URL สามารถทำได้ง่ายมาก ๆ ครับ ผมเคยเจอโปรแกรมเมอร์บางคนเขียนโปรแกรมอย่างนี้แล้วเขาเข้าใจว่าเนื่องจากเขา encode ข้อมูลไว้แล้ว Hacker จะไม่สามารถ ทำ SQL Injection, XSS ได้เพราะว่าผู้ใช้มองไม่เห็นข้อมูล อันนี้ผิดเต็มประตูครับ

ลองอ่านเพิ่มเติมที่นี่ครับ
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

สุดยอดจริงๆ ครับ

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><img> <object> <embed> <param>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.
ญาณรักข์ วรรณสาย