我是网络编程的新手.我创建了一个简单的登录表单(使用php).现在我想把这个转换为HTTPS(带SSL)登录表单.我该怎么做才能做到这一点?
获取SSL证书.在服务器上运行HTTPS(与托管公司合作使其运行)后,您可以使用类似于以下内容的代码强制在PHP中建立HTTPS连接..htaccess是另一种选择,在别处提到.
<?
function current_protocol()
{
$protocol = 'http';
if ( array_key_exists( 'HTTPS', $_SERVER ) && $_SERVER['HTTPS'] === 'on' )
{
$protocol = 'https';
}
return $protocol;
}
//------------------------------------------------------------------------
function current_has_ssl()
{
return current_protocol() == 'https';
}
//------------------------------------------------------------------------
function force_https()
{
if ( current_has_ssl() == false )
{
header( "HTTP/1.1 301 Moved Permanently" );
header( 'Location: https://example.com' );
exit();
}
}
//------------------------------------------------------------------------
// Usage:
force_https(); // at the top of a script before any output.
Run Code Online (Sandbox Code Playgroud)