file_get_contents 没有得到正确文件的内容
file_get_contents doesn't get the content of the correct file
我正在尝试构建一个脚本,该脚本将根据州名称显示新 IP。 IP 应取自与州名称同名的文件。然后它获取一个随机 IP 并分配它(这就是它应该的工作方式)。
问题是它分配完全随机的 IP 并从随机文件加载 IP,而不是从具有状态名称的正确文件中获取 IP。
这应该如何工作:加利福尼亚 > california.txt > 随机 IP
现在如何运作:加利福尼亚 > newyork/georgia/texas/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);
manipulates/changes 每次从 while()
循环调用相应函数时的原始数组。
解决方案是,(在聊天中讨论后)
按以下方式更改 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;
}
}
我正在尝试构建一个脚本,该脚本将根据州名称显示新 IP。 IP 应取自与州名称同名的文件。然后它获取一个随机 IP 并分配它(这就是它应该的工作方式)。
问题是它分配完全随机的 IP 并从随机文件加载 IP,而不是从具有状态名称的正确文件中获取 IP。
这应该如何工作:加利福尼亚 > california.txt > 随机 IP
现在如何运作:加利福尼亚 > newyork/georgia/texas/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);
manipulates/changes 每次从 while()
循环调用相应函数时的原始数组。
解决方案是,(在聊天中讨论后)
按以下方式更改 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;
}
}