检查表单提交的URL是否具有HTTPS

时间:2017-05-17 22:46:11

标签: php ssl

我有一个脚本可以检查SSL的当前页面,但是我需要它才能工作,所以当用户在表单中输入URL并单击提交按钮时,它会返回“是”你说的URL输入有SSL“,或者如果没有,则返回”此站点没有SSL“。

这是我的PHP代码:

if ( !empty( $_SERVER['HTTPS'] ) ) {
            echo "<p>This page does not have SSL. It would be worth adding SSL to this website as it's a ranking factor</p>";
        }else{
            echo '<p>Good job, this page has SSL.</p>';
        }

提前致谢。

1 个答案:

答案 0 :(得分:1)

您似乎正在允许用户检查他们的网站,基于此,您可以使用:

$url = parse_url( 'https://user.url' );
if( $url['scheme'] == 'https' )
{
    echo '<p>Good job, this page has SSL.</p>';
}else{
    echo "<p>This page does not have SSL. It would be worth adding SSL to this website as it's a ranking factor</p>";
}

老问题 - 是否适用于OP的答案但可以检查当前网址是否使用HTTPS:

if ( $_SERVER['REQUEST_SCHEME'] == "https" )
{
    echo '<p>Good job, this page has SSL.</p>';
}else{
    echo "<p>This page does not have SSL. It would be worth adding SSL to this website as it's a ranking factor</p>";
}

您也可以使用:

if ( isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS'] )
{
    echo '<p>Good job, this page has SSL.</p>';
}else{
    echo "<p>This page does not have SSL. It would be worth adding SSL to this website as it's a ranking factor</p>";
}

最后一个选项似乎是确定https的最安全方式。