Page 1 of 1

Notice: Undefined offset: 1

Posted: Sun Jun 08, 2008 4:13 am
by jeffshead
Googled for hours and can't find an answer :banghead:

I get a "Notice: Undefined offset: 1" warning referring to line 2 in the the code below:

Code: Select all

/* add domain name to username if whatnot and soforth */
    if (!list($login_host, $other) = split(':', $_SERVER['HTTP_HOST'])) {
      $login_host = $_SERVER['HTTP_HOST'];
    }
    if (!strstr($login_username, '@')) {
      /* strip any leading 'www.' so that only a username is required */
      $login_host = preg_replace('/^www\./i', '', $login_host);
      $login_host = preg_replace('/^webmail\./i', '', $login_host);
      $login_host = preg_replace('/^mail\./i', '', $login_host);
      $login_username .= '@' . $login_host;
    }
I did not write the code, just trying to stop the warning.

I'm sure the fix is is to add "isset" somewhere, but I can't figure it out.

Can someone help?

Re: Notice: Undefined offset: 1

Posted: Sun Jun 08, 2008 5:23 am
by Eran
The problem is with the shorthand assignment on line 2. You are trying to split the HTTP_HOST environment global into an array using ':' as a delimiter (by the way explode is more appropriate to use here), and assigning the contents of the array into two variables.
In the probable case you are developing on your computer, the host name is 'localhost' which will not yield two array keys - causing the list() function to throw a offset error (since there is nothing to assign in the $other variable).
You should probably check first to see if the host name is 'localhost'.

Re: Notice: Undefined offset: 1

Posted: Sun Jun 08, 2008 9:26 am
by jeffshead
pytrin,
Thank you for your reply. :D

I grasp what you are saying, but I am new to PHP and I did not write the code so it is not being developed on my computer so the host name would not be "localhost". Would it?

This code is for logging into webmail with the username being an email address (multiple domains) and the error is thrown while accessing the script remotley.

Would checking first to see if the host name is "localhost" still apply? If so, what would the code look like? As I said, I'm new to PHP and I want to learn, but I need to see an example. :D