0

I'm getting an error with this code:

$type = "DNS_" . strtoupper($_GET['type']);
$array = dns_get_record($domain, $type);

Helpful info:

$_GET['type'] = "a"

If I enter DNS_A in dns_get_record, the code works. But when entering $type, I get an error.

The type is dynamic and I get it from a URL parameter, which is why I can't hard code it in.

What am I doing wrong?

4
  • 1
    What "error" do you get? Commented Oct 18, 2022 at 15:53
  • @talha2k It doesn't show any error message with E_ALL. Same as if you enter an invalid DNS type like DNS_XXXXX. Commented Oct 18, 2022 at 15:56
  • Do you have error reporting turned on? Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. <?php error_reporting(E_ALL); ini_set('display_errors', 1); Commented Oct 18, 2022 at 15:57
  • 2
    DNS_A is a pre defined integer not a string Commented Oct 18, 2022 at 16:12

3 Answers 3

2

Parameter 2 has to be one of these predefined constants

DNS_A, DNS_CNAME, DNS_HINFO, DNS_CAA, DNS_MX, DNS_NS, DNS_PTR, DNS_SOA,
DNS_TXT, DNS_AAAA, DNS_SRV, DNS_NAPTR, DNS_A6, DNS_ALL or DNS_ANY

so you will need to get the value of the constant in order to make your code flexible like that.

You were passing a string so use constant() to convert that string into the integer value associated with that constant

Example

$_GET['type'] = 'a';

$type = constant("DNS_" . strtoupper($_GET['type']));
$domain = 'www.php.net';

$result = dns_get_record($domain, $type);
print_r($result);

RESULT

Array
(
    [0] => Array
        (
            [host] => www-php-net.ax4z.com
            [class] => IN
            [ttl] => 283
            [type] => A
            [ip] => 185.85.0.29
        )
)
Sign up to request clarification or add additional context in comments.

Comments

0

dns_get_record expects second parameter to be Integer. In yoru case it is a string.

Please replace following numbers based on your required records:

$dns_record = dns_get_record('domain', REPLACE_RECORD_ID);
var_dump($dns_record);


DNS_A = 1
DNS_NS = 2
DNS_CNAME = 16
DNS_SOA = 32
DNS_PTR = 2048
DNS_HINFO = 4096
DNS_MX = 16384
DNS_TXT = 32768

List of DNS Record Types

Comments

0

DNS_A actually means integer 1, NOT a string "DNS_A"

So please use

<?php
$type=1; 
$result = dns_get_record("php.net", $type );
print_r($result);
?>

or use $_GET['type'] = 1 and then use

<?php
$type=$_GET["type"]; 
$result = dns_get_record("php.net", $type );
print_r($result);
?>

Please see official documentation: ( int $type = DNS_ANY .... and so on)

https://www.php.net/manual/en/function.dns-get-record.php

Note: you can mix the $type like:

<?php
$dnsr = dns_get_record('php.net', DNS_A + DNS_NS);
// same as $dnsr = dns_get_record('php.net', 3);
print_r($dnsr);
?>

Keys (pre-defined constants)

DNS_A = 1
DNS_NS = 2
DNS_CNAME = 16
DNS_SOA = 32
DNS_PTR = 2048
DNS_HINFO = 4096
DNS_MX = 16384
DNS_TXT = 32768
DNS_A6 = 16777216
DNS_SRV = 33554432
DNS_NAPTR = 67108864
DNS_AAAA = 134217728
DNS_ALL = 251713587
DNS_ANY = 268435456

As advised by @bilbodog, there is also DNS_CAA:

DNS_CAA = 8192

3 Comments

I will just add one more record to your list: DNS_CAA = 8192
@bilbodog, Thank you for your help ! (I have amended my answer to incorporate DNS_CAA)
Anytime, that is how we get better. Sharing is caring.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.