1

I've the following code which I got from somewhere and it doesn't appear to be working:

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

Can someone help?

What I am attempting to do is return the website protocol when I type in $http

ex:

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

I've got the $websiteurl down, I just can't seem to get it to echo http vs https. I don't know much about functions, so I'm not sure how to troubleshoot it.

Vikram
  • 3,996
  • 8
  • 37
  • 58
kdjernigan
  • 309
  • 2
  • 5
  • 14
  • 3
    Use `http()` instead of `$http`. – zneak Mar 18 '13 at 15:05
  • 4
    no need just use // http://stackoverflow.com/questions/12069156/protocol-less-urls – Waygood Mar 18 '13 at 15:05
  • 1
    `$http` is never defined (neither in the function nor in the other code) – str Mar 18 '13 at 15:05
  • 2
    You are returning the wrong variable :-) – Bart Mar 18 '13 at 15:06
  • 1
    @Waygood > if he is using this method to send an e-mail that wont work, because outlook will turn it into a networkshare lookup making e-mail very slow. Although using `//` is good, you still need to know what you are doing. – Hugo Delsing Mar 18 '13 at 15:10
  • +1 cheers for the heads-up – Waygood Mar 18 '13 at 15:12
  • relative protocol links Stack exchange: http://stackoverflow.com/questions/12069156/protocol-less-urls so using //website/index.php will link to https IF your on https otherwise http – Waygood Mar 18 '13 at 15:07

4 Answers4

4

The http is a function, so you dont call it like a variable using $

try:

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>

To clarify:

$http = 'variable';

function http() {
  return 'function';
}

var_dump($http);
var_dump(http());
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
  • +1 for catching both apparent mistakes, although your 'clarification' doesn't seem that helpful. – Levi Morrison Mar 18 '13 at 15:11
  • You are right. If you understand the clarification, you probably didnt need it in the first place. – Hugo Delsing Mar 18 '13 at 15:14
  • Awesome, except.... when I do URL and click on the link...it leaves out the : in all the urls. It is passing everything except the colon. When I echo out http() the colon is there. How do I fix this? – kdjernigan Mar 18 '13 at 15:18
  • That seems strange. Is the : there when you check the source code? Might be some javascript library messing with the links. If you cant find it in the source just try an `echo http() . $websiteurl . '/index.php';` – Hugo Delsing Mar 18 '13 at 15:25
  • aha! It was my fault. Inside my $websiteurl code I had an extra http:// which was throwing everything off. I coded that first for something else and forgot to change it. haha...you've been staring at the same code for too long when you miss things like that... thanks for all your help. – kdjernigan Mar 18 '13 at 15:39
2
<a href="<?php echo http() . $websiteurl .'/index.php' ?>">Website URL including Protocol</a>
mkjasinski
  • 3,115
  • 2
  • 22
  • 21
1

You are attempting to get the value of http() via $http. Try this:

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

$http in only defined in a scope of http() function.

kamituel
  • 34,606
  • 6
  • 81
  • 98
1

The function will trigger E_NOTICE errors as is, try this:

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

Then as mkjasinski said,

<a href="<?php echo http() . $websiteurl .'/index.php'; ?>">Website URL including Protocol</a>
Xeoncross
  • 55,620
  • 80
  • 262
  • 364