[HELP] My server is crashing - is this if else statement the problem?

Discussion in 'PHP' started by phase1, May 19, 2011.

  1. #1
    My tech support guys said that apache is having muptiple requests spawned and they say its due to php code - i've only got 2 pieces i've modified - is this ok or is it gonna cause problems?

    
    <?php
    function countryCityFromIP($ipAddr)
    {
    //function to find country and city from IP address
    //Developed by Roshan Bhattarai http://roshanbh.com.np
    //verify the IP address for the
    ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
    $ipDetail=array(); //initialize a blank array
    //get the XML result from hostip.info
    $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
    //get the city name inside the node <gml:name> and </gml:name>
    preg_match("@<Hostip>(s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
    //assing the city name to the array
    $ipDetail['city']=$match[2];
    //get the country name inside the node <countryName> and </countryName>
    preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
    //assign the country name to the $ipDetail array
    $ipDetail['country']=$matches[1];
    //get the country name inside the node <countryName> and </countryName>
    preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
    $ipDetail['country_code']=$cc_match[1]; //assing the country code to array
    //return the array containing city, country and country code
    return $ipDetail;
    }
    
    $ipAddr=$_SERVER['REMOTE_ADDR'];
    //echo "<b>IP Address= $ipAddr</b>";
    
    $IPDetail=countryCityFromIP($ipAddr);
    //echo $IPDetail['country']; //country of that IP address
    $countryN=$IPDetail['country'];
    
    $countryN = str_replace(" ","",$countryN);
    
    if ($countryN==CANADA) {
    
    $countrycode='CAN';
    }
    else { }
    if ($countryN==ARGENTINA) {
    
    $countrycode="AR";
    }
    else { }
    if ($countryN==CHILE) {
    
    $countrycode="CH";
    }
    else { }
    if ($countryN==MEXICO) {
    
    $countrycode="MX";
    }
    else { }
    if ($countryN==SPAIN) {
    
    $countrycode="SP";
    }
    else { }
    if ($countryN==USA) {
    
    $countrycode="USA";
    }
    else { }
    if ($countryN==COLUMBIA) {
    
    $countrycode="COL";
    }
    else { }
    if ($countryN==VENEZUELA) {
    
    $countrycode="VE";
    }
    else { }
    if ($countryN==PERU) {
    
    $countrycode="PE";
    }
    else { }
    if ($countryN==URUGUAY) {
    
    $countrycode="UR";
    }
    else { }
    if ($countryN==ECUADOR) {
    
    $countrycode="EC";
    }
    else { }
    if ($countryN==DOMINICANREPUBLIC) {
    
    $countrycode="DR";
    }
    else { }
    if ($countryN==BRAZIL) {
    
    $countrycode="BR";
    }
    else { }
    if ($countryN==PANAMA) {
    
    $countrycode="PA";
    }
    else { }
    if ($countryN==COSTARICA) {
    
    $countrycode="CR";
    }
    else { }
    if ($countryN==GUATEMALA) {
    
    $countrycode="GU";
    }
    else { }
    if ($countryN==ELSALVADOR) {
    
    $countrycode="ES";
    }
    else { }
    
    
    ?>
    
    Code (markup):
    This code is located in the index file of a wordpress theme

    Thanks for your help!!
     
    phase1, May 19, 2011 IP
  2. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    If this is on it would echo the stirng "$ipAddr" not the var //echo "<b>IP Address= $ipAddr</b>";


    this = if ($countryN==MEXICO)
    might have to be = if ($countryN=="MEXICO")
    on ALL of them. Its not a NULL or a numeric its a string .
     
    ezprint2008, May 19, 2011 IP