返回http/https协议


Returning http/https protocol

我有以下代码,我从某处得到的,它似乎不工作:

function http() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 return $http;
}

有人能帮忙吗?

我想做的是返回网站协议时,我输入$http

,

<a href="<?php echo $http . $websiteurl .'/index.php' ?>">Website URL including Protocol</a>

我已经把$websiteurl弄下来了,我似乎不能让它回显http和https。我对功能了解不多,所以我不确定如何排除故障

http是一个函数,所以你不能像调用变量一样使用$

试题:

function http() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 return $pageURL; // <-changed
}
<a href="<?php echo http() . $websiteurl . '/index.php'; ?>">Website URL including Protocol</a>

澄清:

$http = 'variable';
function http() {
  return 'function';
}
var_dump($http);
var_dump(http());

<a href="<?php echo http() . $websiteurl .'/index.php' ?>">Website URL including Protocol</a>

您正试图通过$http获取http()的值。试试这个:

<a href="<?php echo http() . $websiteurl .'/index.php' ?>">Website URL including Protocol</a>

$http只定义在http()函数的作用域中

函数将触发E_NOTICE错误,试试这个:

function http() {
     return (getenv('HTTPS') == "on" ? 'https://' : 'http://');
}

然后正如mkjasinski所说,

<a href="<?php echo http() . $websiteurl .'/index.php'; ?>">Website URL including Protocol</a>