Undefined index: HTTP_HOST and more

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Undefined index: HTTP_HOST and more

Post by bian101 »

Hey guys, for some reason this code:

Code: Select all

  protected function getCurrentUrl() {
    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'
      ? 'https://'
      : 'http://';
    $currentUrl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $parts = parse_url($currentUrl);

(although its the last 2 lines of that where the apparent error is according to my debugger)

Generates these errors:

Undefined index: HTTP_HOST
Undefined index: REQUEST_URI
parse_url(http://) [<a href='function.parse-url'>function.parse-url</a>]: Unable to parse URL



Any ideas?

Thanks!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Undefined index: HTTP_HOST and more

Post by califdon »

What version of PHP are you using?

The reason for the 3rd error is probably that you didn't give the function anything to parse. Here is the manual page: http://php.net/manual/en/function.parse-url.php
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Undefined index: HTTP_HOST and more

Post by bian101 »

califdon wrote:What version of PHP are you using?

The reason for the 3rd error is probably that you didn't give the function anything to parse. Here is the manual page: http://php.net/manual/en/function.parse-url.php
Hey thanks for the link, and im using 5.2.1
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Undefined index: HTTP_HOST and more

Post by califdon »

OK, then the problem is not that you're using an old version of PHP. I'm not an expert on $_SERVER variables, but the manual says, in reference to $_SERVER['HTTP_HOST'], "Contents of the Host: header from the current request, if there is one." That last "if" suggests that there may not be a value under some circumstances.

The thing is, all 3 of these error messages relate to the same issue. The parse-url() function isn't working because all it's getting is "http://", so there's nothing to parse. It apparently IS getting your $protocol value. But the messages about "undefined index" are talking about not finding those parts of the $_SERVER global array. The $_SERVER array can have dozens of indexes, with a value corresponding to each index. You can find all of the possible indexes at http://php.net/manual/en/reserved.variables.server.php. So that's the root of your problem. Only I don't know why those 2 indexes would be missing. Sorry.

I just had a thought: you ARE testing this by calling this script from a browser, aren't you? Because if you were just trying to execute the PHP code on its own, there would BE NO REQUEST, thus no values or even indexes in the $_SERVER array.
Post Reply