Notice: Undefined offset: 1

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
User avatar
jeffshead
Forum Newbie
Posts: 2
Joined: Sun Jun 08, 2008 4:05 am

Notice: Undefined offset: 1

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Notice: Undefined offset: 1

Post 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'.
User avatar
jeffshead
Forum Newbie
Posts: 2
Joined: Sun Jun 08, 2008 4:05 am

Re: Notice: Undefined offset: 1

Post 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
Post Reply