返回http / https协议

时间:2013-03-18 15:03:33

标签: php

我从以下地方获得了以下代码,但它似乎没有起作用:

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 vs https。我对功能知之甚少,所以我不确定如何排除故障。

4 个答案:

答案 0 :(得分:4)

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());

答案 1 :(得分:2)

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

答案 2 :(得分:1)

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

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

$http仅在http()函数范围内定义。

答案 3 :(得分:1)

该功能将按原样触发E_NOTICE错误,请尝试以下操作:

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

然后正如mkjasinski所说,

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