-1

I am trying to display city in my webpage content if that city is in Texas using php and a geo paid service. Why isn't my if....else statement working?

Paid SSL Geolocation service through ipstack. Host GoDaddy using cpanel

<html>
    <h1>
    <?php
    $ip = $_SERVER['REMOTE_ADDR']; 
    $api_key = "myipstackkeygoeshere";
    $freegeoipjson = file_get_contents("http://api.ipstack.com/".$ip."?access_key=".$api_key."");
    $jsondata = json_decode($freegeoipjson);
    $cityfromip = $jsondata->city;
    $region_namefromip = $jsondata->region_name;
    if ($region_namefromip = "Texas") {
    echo "". $cityfromip ."";
    } else {
    echo "Texas";
    }
    ?>
    Services</h1>  
 </body>
</html>

I want the City to be displayed before "Services" if the visitor's location is in Texas (Example: "Dallas Services"). If visitor's location is not in Texas, I want "Texas" to display (Example: "Texas Services").

Before adding the if...else statement and using just echo "". $cityfromip .""; the page would display city (no matter what state). With the if...else statement, no city displays. Where is my code going wrong?

Jeto
  • 14,596
  • 2
  • 32
  • 46

1 Answers1

-1

Jack's answer helped and I was able to come up with a working solution using:

<?php  
 $ip = $_SERVER['REMOTE_ADDR']; 
 $api_key = "AccessKeyGoesHere"; 
 $freegeoipjson = file_get_contents("https://api.ipstack.com/".$ip."?access_key=".$api_key.""); 
 $data = json_decode($freegeoipjson, TRUE); 
 $city = print_r($data['city'], true); 
 $state = print_r($data['region_code'], true); 
 $charlotte_cities_in_75_mile_radius = "City1 City2 City3 City4 City5 City6 City7 City8"; 

 if ((strpos($charlotte_cities_in_75_mile_radius,$city) !== false) AND ($state == "NC")) { 
     echo $city ."&nbsp;"; 
 }
 else { 
     echo 'North Carolina '; 
 } 
 ?> 
  • This question is off-topic and should be deleted rather than answered. It's unlikely to help future visitors. – miken32 Apr 08 '19 at 20:48
  • I am new on stack. Would you explain why this question is off topic? – VicLeeg Apr 10 '19 at 17:21
  • The real idea for the site is not so much to get answers to our own questions, but to provide value for future visitors. Someone else making the same error in the future is very unlikely to find this question because it's described by its symptoms, not the actual problem. From https://stackoverflow.com/help/on-topic: "Questions about a problem that can no longer be reproduced or that was caused by a simple typographical error: While similar questions may be on-topic here, these are often resolved in a manner unlikely to help future readers." – miken32 Apr 10 '19 at 18:15
  • To be clear, your question was fine – it's better than questions from a lot of new users – but once it became clear that the problem was a typo, that's when it became off-topic. It will get deleted eventually, but that will involve review by a bunch of people so if you delete it yourself you'll save that work by others. – miken32 Apr 10 '19 at 18:18