2016-12-28 86 views
0
外部安全的URL

我使用的功能esc_url_raw ..產生的WordPress

WordPress function: 
esc_url_raw(string $url, array $protocols = null) 

如何打印以 「https」 網址?

Example: 
input: http://example.com 
ouput: https://example.com 

Doc Function

回答

0

可以提供在$protocols變量的可接受的協議。它可以是array()的協議,如http,https。您可以參考wp_allowed_protocols獲取允許的協議。

爲了您的答案,您可以提供$protocols的數組作爲array('http', 'https')作爲esc_url_raw的參數。

如果你實際上是尋找到一個URL從http轉換爲https,那麼你可以定義是這樣的:

function convert_to_https(url){ 
    $new_url = preg_replace("/^http:/i", "https:", $url); 
    return $new_url; 
} 


$https_url = convert_to_https('http://example.com'); 
echo $https_url; // https://example.com 

// then escape the URL 
esc_url_raw($https_url); 
+0

謝謝,功能** ** esc_url_raw什麼可以讓我爲驗證URL,逃離不屬於該網址的字符。 –