我試圖建立一個腳本,將顯示一個新的基於狀態名稱的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") {
}
}
看到這些語句,'$ state_content [$ state_name] = explode(「\ n」,$ state_text);''和'$ country_content [$ country_name] = explode(「\ n」,$ country_text);'。每當從'while'循環調用相應的函數時,原始數組即'$ state_content'和'$ country_content'都會被操作/更改。 –
我明白了。任何想法如何解決這個問題? – Ricardo
這些文件包含什麼? IP地址?另外,還有哪些嘗試使用*這些*語句? –