preg_match to only allow https:// in an URL

感情迁移 提交于 2019-12-12 03:46:22

问题


I'd like to allow only https:// links to be used as remote avatar images in phpbb to avoid mixed content. This seems to be the code that is used to check whether the entered url is correct (to be found in /phpbb/avatar/driver/remote.php):

if (!preg_match('#^(http|https|ftp)://(?:(.*?\.)*?[a-z0-9\-]+?\.[a-z]{2,4}|(?:\d{1,3}\.){3,5}\d{1,3}):?([0-9]*?).*?\.('. implode('|', $this->allowed_extensions) . ')$#i', $url))
{
    $error[] = 'AVATAR_URL_INVALID';
    return false;
}

I'd like to add a if{}-condition before this code block to give an informative error message if the user selected an image from a non-secure server. Can anyone help me defining the correct preg_match() string please?


回答1:


Based on the suggestion by @casimir, I used the following code and it works:

    $urlchk = parse_url($url);
    $urlscheme = isset($urlchk['scheme']) ? $urlchk['scheme'].'://' : 'http://';
    if ($urlscheme=='http://'){
        // error message
    }


来源:https://stackoverflow.com/questions/36561834/preg-match-to-only-allow-https-in-an-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!