/    Sign up×
Community /Pin to ProfileBookmark

How do I extract ip address from an array

The php function getUserIpAddr() yields an array, [$ip] when called from my index.php. I need to know how to extract just the ip from it so that I can use php function inet_pton before inserting the ip in the database.

Code for getUserIpAddr():

“`
<?php
function getUserIpAddr(){
if(!empty($_SERVER[‘HTTP_CLIENT_IP’])){
//ip from share internet
$ip = $_SERVER[‘HTTP_CLIENT_IP’];
}elseif(!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])){
//ip pass from proxy
$ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
}else{
$ip = $_SERVER[‘REMOTE_ADDR’];
}
return $ip;
}
?>
“`

Code in index.php:

“`
<?php

session_start();
$ip = null;
$php_scripts = ‘../php/’;

require $php_scripts . ‘PDO_Connection_Select.php’;
require $php_scripts . ‘GetUserIpAddr.php’;

$ip = GetUserIpAddr();
if (!$pdo = PDOConnect(“test”)):
{
echo “Failed to connect to database” ;
exit;
}
else:
{
/* need code to extract ip from [$ip] */
$ip = inet_pton([$ip]);
$stmt = $pdo->prepare(“INSERT INTO download (ADDRESS) VALUES (?)”);
$stmt->execute($ip) ;
}
endif;
?>

“`

Thanks in advance

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 12.2020 — > @larry29936#1618368 The php function getUserIpAddr() yields an array

Can you show us what it returns? (It doesn't look to me as if it would return an array.) Maybe add this just to get some debug info, and if it does return an array, at least then you'll know what element you're interested in).
[code]
$ip = GetUserIpAddr();
die("<pre>".print_r($ip, 1)."</pre>");
[/php]
Copy linkTweet thisAlerts:
@larry29936authorMay 12.2020 — > @NogDog#1618375 $ip = GetUserIpAddr();

> die("<pre>".print_r($ip, 1)."</pre>");

> [/php]


Returns 127.0.0.1

How do I convert that to a string? Using this code, inet_pton requires one.
``<i>
</i>
{
$test = ([$ip, 1]);
$add = inet_pton($test);

$stmt = $pdo-&gt;prepare("INSERT INTO download (ADDRESS, ip_address) VALUES (?, ?)");
$stmt-&gt;execute([$add, $test]) ;
}<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogMay 12.2020 — > @larry29936#1618378 How do I convert that to a string? Using this code, inet_pton requires one.

Isn't it already a string? Why do you say it's an array?

It seems that all you should need is:
<i>
</i>else:
{
// $ip = inet_pton([$ip]); // why did you make it an array here? Just do...
$ip = inet_pton($ip);
$stmt = $pdo-&gt;prepare("INSERT INTO download (ADDRESS) VALUES (?)");
$stmt-&gt;execute([$ip]) ; // This is where you need an array argument (assuming PDO?)
}
Copy linkTweet thisAlerts:
@larry29936authorMay 12.2020 — @NogDog#1618387 -DUH! I can't believe I've been struggling with this for 5 hours! Thanks.
Copy linkTweet thisAlerts:
@ArifulAiMay 14.2020 — Should Your Email List Comprise Buyers Or Enquirers? Or on the other hand Should it Be Both?

Nobody is questioning the way that the best rundown is a colossal rundown of purchasers, individuals who've just purchased from you and trust you and might purchase from you for a long time to come.

In any case, what of individuals just enquiring about your item at the present time, individuals who may become clients once certain obstacles are beaten which right now keep them from purchasing?

Imagine a scenario in which, for instance, those individuals are shy of money and unfit to bear the cost of your item until their next compensation day; consider the possibility that they're grinding away when they read your direct mail advertisement and they like to arrange later from the security of home.

Or on the other hand perhaps your item is near what your contact is urgent to purchase yet not exactly close enough. That individual may need an alternate shading, for instance, or an alternate size, or maybe the person doesn't confide in you yet and wants to become familiar with you before giving you their well deserved money.

Whatever the explanation individuals decide not to purchase immediately, it's sheltered to state on the off chance that you don't take their contact subtleties you'll never get notification from them again. Also, in light of the fact that an enormous extent of those individuals may some way or another become rehash, high ticket purchasers, it's a smart thought to get them onto your mailing list, close by purchasers, to entice a buy some time soon.

That question of not believing obscure venders is a valid justification for growing a rundown to create certainty, which the legitimate merchant does by offering exhortation and data just as giving free examples of paid-to items, by and large to limit the hole of vulnerability for enquirers and in the long run get them to purchase.
×

Success!

Help @larry29936 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...