Nội dung bài viết Decode và encode là một trong những cách thường được sử dụng để tránh các cuộc tấn công tập lệnh trên nhiều trang web [XSS] bằng cách mã hóa các ký tự đặc biệt trong một URL. Bài viết này cung cấp cách triển khai cũng như giải thích rõ ràng hơn. Sử dụng Trong bài viết này, bạn sẽ học cách Video học lập trình mỗi ngày
encode
chuyển đổi một chuỗi thành một định dạng URL hợp lệ để làm cho dữ liệu được truyền đi đáng tin cậy và an toàn hơn. Và sử dụng decode
ngược lại với quá trình mã hóa. Nó chuyển đổi các chuỗi URL được mã hóa và các tham số truy vấn trở lại định dạng bình thường của chúng.encode
hoặc decode
String URL và
2 trong ứng dụng Node.js.const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
Xem thêm: Mẹo viết code javascript và nodejs
URL encode
Trong Node.js xây dựng trên công cụ JavaScript V8 của Chrome hỗ trợ hai phương pháp encode
đó là
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5 để mã hoá một URL. Vậy nó dùng như thế nào?encodeURI javascript
Nếu bạn muốn mã hoá một URL hoàn chỉnh thì có thể sử dụng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4, phương thức này không hỗ trợ các ký tự sau: const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7. Ví dụ sau cho thấy điều đó:const url = '//example.com/!Learn Node$/ Example';
// Encode complete URL
const encodedUrl = encodeURI[url];
// Print encoded URL
console.log[encodedUrl];
// //example.com/!Learn%20Node$/%20Example
encodeURIComponent
Nếu như dùng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 để mã hoá một URL thì việc sử dụng const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5 thì chỉ mã hoá const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0 hay còn gọi là tham số chuỗi truy vấn chứ không phải một URL hoàn chỉnh. Xem tiếp ví dụ sau:const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
Ngoài phương pháp
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5 thì chúng tôi cung câp thêm cho bạn một module tương tự nhưng nó được tích hợp sẵn trong Node.js để mã hoá URL đó là const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2.querystring module
Bạn cũng có thể sử dụng
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 được tích hợp Node.js để mã hóa URL. Module này cung cấp các phương thức tiện ích để phân tích cú pháp và định dạng chuỗi truy vấn URL:const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
decode URL
Trên đó là việc hướng dẫn cũng như giải thích về việc mã hoá
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4. Đương nhiên, khi mã hoá thì phải có giải mã, tương tự khi encode xong thi phải decode.Tương tự thôi
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5 cũng cung cấp cho hai methods đó là const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 và const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 để thực hiện giải mã URL.decodeURI javascript
Phương pháp
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 được sử dụng để giải mã một URL đầy đủ:const encodedUrl = '//example.com/!Learn%20Node$/%20Example';
// Decode URL
const url = decodeURI[encodedUrl];
// Print URL
console.log[url];
// //example.com/!Learn Node$/ Example
decodeURIComponent
Chức năng
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 được sử dụng để các thành phần URL giải mã:try {
var a = decodeURIComponent['%E0%A4%A'];
} catch[e] {
console.error[e];
}
// URIError: malformed URI sequence
Lời kết
Bài viết đã quá rõ ràng cho việc sử dụng
const encodedUrl = '//example.com/!Learn%20Node$/%20Example';
// Decode URL
const url = decodeURI[encodedUrl];
// Print URL
console.log[url];
// //example.com/!Learn Node$/ Example
0 rồi, cho nên việc tóm tắt là việc làm dư thừa. Nhưng các bạn cũng nên để ý việc khác nhau giữa const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
5, cũng như const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 và const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 trong Node.js. Tránh nhầm lẫn để sử dụng không đúng mục đích của việc làm của mình.Xem thêm: Mẹo viết code javascript và nodejs
URL encode
Chìa khóa
Trong hướng dẫn này, bạn sẽ tìm hiểu lý do tại sao bạn cần mã hóa hoặc giải mã URL của mình và cách sử dụng các hàm PHP tích hợp để làm như vậy.
- Nhu cầu mã hóa và giải mã URL
- Mã hóa URL với urlencode[] và rawurlencode[]
- Giải mã URL với urldecode[] và rawurldecode[]
- Lời kết
Nhu cầu mã hóa và giải mã URL
Mã hóa URL với urlencode[] và rawurlencode[]
Giải mã URL với urldecode[] và rawurldecode[] | Lời kết |
Lời kết | Có lẽ bạn muốn chuyển một URL dưới dạng tham số truy vấn đến một dịch vụ net hoặc một trang net khác. Ví dụ: bạn muốn gửi dữ liệu sau đến một trang net dưới dạng một chuỗi truy vấn: |
Chìa khóa | dữ liệu |
chuyển hướng | 2 |
//code.tutsplus.com
//www.instance.com?redirect=httpspercent3Apercent2Fpercent2Fcode.tutsplus.com&creator=montypercent20shokeen&web page=2
tác giả
monty shokeen
Mã hóa URL với urlencode[] và rawurlencode[]
Giải mã URL với urldecode[] và rawurldecode[]
Lời kết
Lời kết