2016-10-11 116 views
1

我試圖建立一個腳本,將顯示一個新的基於狀態名稱的IP。 IP應該從與狀態名稱相同的文件中取得。然後它需要一個隨機IP並分配它(這是它應該如何工作)。file_get_contents沒有得到正確的文件

問題是它分配完全隨機IP並從隨機文件加載它們的IP,而不是從狀態名稱正確的文件中取出IP。

如何這應該工作:加州> california.txt>隨機IP

這是如何工作現在:加利福尼亞州>紐約/喬治亞州/得克薩斯州/ etc.txt>隨機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)]; 
} 

用法:

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") { 

     } 

    } 
+0

看到這些語句,'$ state_content [$ state_name] = explode(「\ n」,$ state_text);''和'$ country_content [$ country_name] = explode(「\ n」,$ country_text);'。每當從'while'循環調用相應的函數時,原始數組即'$ state_content'和'$ country_content'都會被操作/更改。 –

+0

我明白了。任何想法如何解決這個問題? – Ricardo

+0

這些文件包含什麼? IP地址?另外,還有哪些嘗試使用*這些*語句? –

回答

1

正如我在評論說,

這些聲明每次從while()循環中調用相應的函數時,和$country_content[$country_name] = explode("\n", $country_text);都會操作/更改原始數組。

解決方案是,(在聊天討論後)

變化getStateContent(),並以下列方式getCountryContent()功能,

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; 
    } 
} 

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; 

    } 
} 
+0

它只顯示了一個空白頁面,其中包含'state/California.txt' ...我需要查看頁面上新分配的IP的所有記錄,這就是我使用'while'的原因。這裏的東西是非常錯誤的。也許你忘了getStateIpByName($ state_name)'函數從關聯文件中檢索一個隨機IP?或者,也許我不明白我應該如何將這個應用到'while'循環中? – Ricardo

+0

@Ricardo哦,我的壞。我不小心把'die($ file_name)'保存在那個函數中,這就是爲什麼。我用它來調試你的問題。我已經更新了我的答案。你不需要改變任何'while循環,你只需要修改這些函數,就是這樣。 –

+0

我應該在'while'循環中寫什麼,而不是'echo $ stateip;'? 我覺得你不明白這個問題。 我從我的數據庫中獲取數據,並需要從文本文件中爲每行分配一個新IP。我假設你所做的只是顯示特定.txt文件的內容。每個數據庫行分配新IP的位置在哪裏? – Ricardo