Jump to content

[SOLVED] Undefined offset, how do I detect?


oni-kun

Recommended Posts

I have this code to get the IP address and turn it into the host, and extract 'isp.net' for example out of it.. But if there is no host, it throws the error.. "Undefined offset(0) in line xxx"..

 

$fullhost = gethostbyaddr($ip);
preg_match("/^(http:\/\/)?([^\/]+)/i",$fullhost, $matches);
$host = $matches[2]; 
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "<b>ISP : ";
if($matches[0]==null){
echo strtoupper($matches[0]);
}else{ 
echo "ISP : [Cannot resolve]";
}

My code works, it simply throws that error when it can't resolve a host, since the preg_match doesn't pass through, how do I catch the 'offset not defined' and make it display the ELSE?

Link to comment
Share on other sites

in line xxx...

 

in which line would that be? did you place your if (isset(... right? It needs to be right after the first preg_match...

 

Notice: Undefined offset: 0 in D:\wamp\www\projects\trace.php on line 152

 

$fullhost = gethostbyaddr($ip);
echo $fullhost;
preg_match("/^(http:\/\/)?([^\/]+)/i",$fullhost, $matches);
if(isset($matches[0])){
$host = $matches[2]; 
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "<b>ISP : ";
	echo strtoupper($matches[0]); //The error line 152
}else{ 
	echo "ISP : [Cannot resolve]";
}

It still displays undefined offset, I don't know if isset() is the thing to use to catch this.. help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.