1

I have a page on a website that grabs a visitor's IP address and emails to an email account. My goal is to use a tool, GeoIP lookup, to associate the IP address with a city in the email content. What I want is to add the following command to the email content: geoiplookup $remoteIpAddress, but my attempts aren't working. I'm hoping somebody familiar with PHP can point me in the right direction. Here is the code that is currently working:

<?php
include('session.php');
$yourEmailAddress = "[email protected]";
$emailSubject = "Example Subject";
$remoteIpAddress = $_SERVER['REMOTE_ADDR'];
$emailContent = "Blah Blah Blah: ".$remoteIpAddress;

mail($yourEmailAddress, $emailSubject, $emailContent);

I tried setting the variable with $city = exec("geoiplookup $remoteIpAddress"); and updating $emailContent to: $emailContent = "Blah Blah Blah: ".$remoteIpAddress .$city; but that didn't work. I tried a few other variations of that, trying to get it working with shell_exec, with and without quotations, and nothing has worked. Hoping somebody can point me in the right direction. Thanks!

2
  • I would use a geolocation api with php for example https://ipstack.com/ (free with limitations) in order to achieve your goal Commented Nov 11, 2019 at 16:10
  • Can you explain what did not work? The mail() function or string concatenation? Commented Nov 11, 2019 at 16:36

2 Answers 2

4

Go to for example at ipstack.com and get your free API key.

Then using the following function and your newly generated API Access Key for the service you can achieve your goals. The service will give you a stdClass object as a response with multiple properties, e.g.: city.

function lookup($ipAddresses, $token)
{
    $userAgent = ' PHP/' . PHP_VERSION . ' CURL/' . curl_version()['version'];
    $curl = curl_init();
    $url = 'http://api.ipstack.com/' . implode(',', $ipAddresses) . '?access_key=' . $token;

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($curl, CURLOPT_HTTPGET, true);
    $info = curl_getinfo($curl);
    $response = curl_exec($curl);
    curl_close($curl);
    return json_decode($response);
}

then

$ip = $_SERVER['REMOTE_ADDR'];
$response = lookup([$ip], 'Your API Access Key comes here');
$city = $response->city;

var_dump($response, $city);
Sign up to request clarification or add additional context in comments.

1 Comment

@rdarellano This is the better answer. You should use this, instead of mine, unless there is a specific reason you do not want to make remote calls like this. And I really doubt this is the case.
1

What you should do is indeed what @marcell suggested and use some remote API for this. But if you really want to do it with a command, something like this should do the trick:

$geolookup = `geoiplookup {$remoteIpAddress}`;

Please note, that I am using ( ` ) and not ( ' )!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.