简体   繁体   中英

Why strpos PHP not work with fsockopen response?

Why strpos PHP not work with fsockopen response ?

When load this code. This code will be requests sdgsgsdgsfsdfsd.ca to whois.cira.ca server and find text Domain status: available with strpos PHP if found it's will be echo

{"domain":"sdgsgsdgsfsdfsdca","availability":"available"}

but if not found text. It's will be echo

{"domain":"sdgsgsdgsfsdfsdca","availability":"TAKEN"}

In this case found text but still echo

{"domain":"sdgsgsdgsfsdfsdca","availability":"TAKEN"}

How can i do ?

<?php
$server = "whois.cira.ca";
$response = "Domain status: available";
showDomainResult(sdgsgsdgsfsdfsd.ca,$server,$response); 

function checkDomain($domain_check,$server,$findText)
{
    $con = fsockopen($server, 43);
    if (!$con) return false;
    fputs($con, $domain_check."\r\n");
    $response = ' :';
    while(!feof($con)) 
    {
        $response .= fgets($con,128); 
    }
    echo $response."<BR><BR><BR><BR><BR>";
    fclose($con);
    if (strpos($response, $findText))
    {
        return true;
    }
    else 
    {
        return false; 
    }
}

function showDomainResult($domain_check,$server,$findText)
{
    if (checkDomain($domain_check,$server,$findText))
    {
        class Emp 
        {
            public $domain = "";
            public $availability = "";
        }
        $e = new Emp();
        $e->domain = $domain_check;
        $e->availability = "available";
        echo json_encode($e);
    }
    else
    {
        class Emp 
        {
            public $domain = "";
            public $availability = "";
        }
        $e = new Emp();
        $e->domain = $domain_check;
        $e->availability = "TAKEN";
        echo json_encode($e);
    }
}
?> 

you're using strpos wrong, if the string START with what you're searching for, it will return int(0), which is "kinda false" by PHP's definition. explicitly check for false , like this

return false!==strpos($response, $findText);

and make sure you're using !== not !=

and as a rule of thumb, never use loose comparison operators in PHP if you can avoid it, hilarious bugs can occur if you do: https://3v4l.org/tT4l8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM