Jump to content

notice: undefined offset 0 after wrong dns_get_record

Recommended Posts

My first question:


I have a message like this: notice: undefined offset 0


I use an input form to check with dns_get_record and DNS_MX whether the domain of an email exists.

I can check the emails if the domain does exist, but a wrong domain like: mail@verynonexistingdomain.com throws in the notice undefined offset 0

so does an empty input.

    <br> email input :<br>
    <form action="#" method = "post">
    <input type ="text" name ="mail">
    <input type="submit" name = "click" value ="zend">


    $domain ='';
    $data ='';

    if (isset($_POST['mail'])) { 
    echo $_POST['mail']; 
    $input = $_POST['mail'];

     // //

    function check_address($input){
        list($user, $domain) = explode('@', $input);
        $data= dns_get_record($domain, DNS_MX);
                return $data[0]['target'];
        } else 
		{ }}


      echo check_address($input);
      Echo '<br><br>';
    echo $input . " : " ;

    if(check_address($input)  ) {
        echo('<br>This MX records exists; I will accept this email as valid.<br> ');
    else {
        echo('<br>No MX record exists;  Invalid email.<br>');
    echo '</center>';

I had it working without the form part so with a constant variable, but when I added the form it threw the error.


I tried several options, but cant find the logic behind executing a good input without throwing an error and a bad domain giving this notice.


Anyone any idea?


Share this post

Link to post
Share on other sites

You can use isset to check if a variable exists. If it doesn't exist then it probably wasn't found and you can show an error message.

Share this post

Link to post
Share on other sites



thanks for reading my post.


I found a solution in using only one = in

    if($data[0]['host']=$domain&&!empty($data[0]['target']) )

I also placed

      if (!empty($input)){

in the very beginning of the function.


I do the programming to understand what PHP does, but some things are not easy to do just by sheer logic. I had to reduce the number of operators from 2 to 1, at php.net there is brief explanation about operators regarding equality (==) and identity (===) of the different variables, but where can you find more about using just one = .


To get a better understanding of these kind of operators I would like to receive some explanation how I should interpretate it in the example I posted.

Edited by Balderick

Share this post

Link to post
Share on other sites

A single '=‘ means whatever preceeds '=‘ will equal whatever value following the '=‘, it is never used within a if condition for comparing one value against another.

Share this post

Link to post
Share on other sites

Yeah, that's not much of a solution. That's the same thing as doing this:


if ($data[0]['host'])
Now you've changed the value of the variable you're trying to check, it's always going to have the same value as $domain. If that's what you're trying to do, great, but if that's not what you're trying to do then it's not a solution to your problem.

Share this post

Link to post
Share on other sites



You're not making it clearer to me.


The function works, I drag data through it and with I can check with IF(name_of_function) { ..... if the output is ok or not.

Share this post

Link to post
Share on other sites

I'm not sure if I can make it clearer, but I'll try.


The = operator is for assigning a value, not for checking a value. == and === are for checking values. When you do this:


it is the same as doing this:


if (if($data[0]['host'])
You are changing the value, because = is only used for assigning values. To prove that you are changing the value, run this:


$test = 'foo';
if ($test = 'bar') {
  echo '$test is set to ' . $test;
Every time you run that code it will print "$test is set to bar". $test will never be set to foo, it will never have the value that it had before the if statement with the assignment. The if statement changes the value of the variable, because it uses =.


So, your original code:

does not do the same thing as your new code:

if($data[0]['host']=$domain&&!empty($data[0]['target']) )
Those do 2 different things. The first one checks if $data[0]['host'] is the same value as $domain, and the second one copies the value in $domain into $data[0]['host'].


If you're trying to check if $data[0]['host'] has a value and avoid the undefined offset error that you were getting, then you use isset to check if the variable is even set before you try to check the value:




isset will return false if the value is not set, and you can avoid the error of trying to access something that is not set. If it's not set then the domain lookup failed.


Also, you should change your check_address function so that it always returns a value. Right now it only returns a value if everything matched. If it didn't match you should return a value like false so that you can tell whether or not the domain was found. A function that returns a value should always return a value in every case, not only some cases. Your other code depends on it to return a value.


Hopefully that makes more sense.

Share this post

Link to post
Share on other sites

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...