简体   繁体   中英

file_get_contents doesn't get the content of the correct file

I'm trying to build a script that will show a new IP based on the state name. The IP's should be taken from a file with the same name as the state name. Then it takes a random IP and assigns it (this is how it should work).

The problem is that it assigns totally random IP's and loads them IP's from a random file instead of taking it from the correct file with the state name.

How this should work: california > california.txt > random IP

How this works now: california > newyork/georgia/texas/etc.txt > random IP

$state_content=["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","NewHampshire","NewJersey","NewMexico","NewYork","NorthCarolina","NorthDakota","Ohio","Oklahoma","Oregon","Pennsylvania","RhodeIsland","SouthCarolina","SouthDakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","WestVirginia","Wisconsin","Wyoming"];

$country_content=["UnitedStates", "UnitedKingdom", "Canada", "Australia", "Germany", "France", "Spain"];

$a = 0;

function getStateContent($state_name) {

    // checks do we already have content for this state
    if(!isset($state_content[$state_name])) {

        // generate file name
        $file_name = "state/";
        $file_name .= str_replace(" ", "", ucwords($state_name));
        $file_name .= ".txt";

        $state_text = file_get_contents($file_name);
        $state_content[$state_name] = explode("\n", $state_text);
    }

    return $state_content[$state_name];
}

function getStateIpByName($state_name) {
    $content = getStateContent($state_name);
    return $content[array_rand($content)];
}

function getCountryContent($country_name) {

    // checks do we already have content for this state
    if(!isset($country_content[$country_name])) {

        // generate file name
        $file_name = "country/";
        $file_name .= str_replace(" ", "", ucwords($country_name));
        $file_name .= ".txt";

        $country_text = file_get_contents($file_name);
        $country_content[$country_name] = explode("\n", $country_text);
    }

    return $country_content[$country_name];
}

function getCountryIpByName($country_name) {
    $content = getCountryContent($country_name);
    return $content[array_rand($content)];
}

Usage:

if ($result->num_rows > 0) {
    // output data of each row
    echo "<div class='block'><div class='rows'>";

    while($row = $result->fetch_assoc()) {

        if (!empty($row["state"]) && $row["country"] == "united states") {
            $stateip = getStateIpByName($row["state"]);
            $a++;
            echo $stateip;
        }

        else if (empty($row["state"]) && $row["country"] == "united states") {
            $countryip = getCountryIpByName($row["country"]);
            $a++;
            echo $countryip;
          }

        else if ($row["country"] != "united states") {

        }

    }

As I said in the comment,

These statements $state_content[$state_name] = explode("\\n", $state_text); and $country_content[$country_name] = explode("\\n", $country_text); manipulates/changes the original array every time the corresponding functions get called from the while() loop.

The solution is, (After discussing on chat)

Change getStateContent() and getCountryContent() functions in the following way,

function getStateContent($state_name) {
    // checks do we already have content for this state
    global $state_content;
    $state_name = str_replace(" ", "", ucwords($state_name));
    if(in_array($state_name, $state_content)) {
        // generate file name
        $file_name = "state/";
        $file_name .= $state_name;
        $file_name .= ".txt";

        $state_text = file_get_contents($file_name);
        $ipaddresses = explode("\n", $state_text);
        return $ipaddresses;
    }
}

and

function getCountryContent($country_name) {
    // checks do we already have content for this state
    global $country_content;
    $country_name = str_replace(" ", "", ucwords($country_name));
    if(in_array($country_name, $country_content)) {
        // generate file name
        $file_name = "country/";
        $file_name .= $country_name;
        $file_name .= ".txt";

        $country_text = file_get_contents($file_name);
        $ipaddresses = explode("\n", $country_text);
        return $ipaddresses;

    }
}

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