gpt4 book ai didi

php - 检查特定访客的 COOKIE 是否已设置/为空

转载 作者:搜寻专家 更新时间:2023-10-31 21:05:36 25 4
gpt4 key购买 nike

首先,我想检查 cookie 是否为 null。如果它是 null,则运行条件以尝试检索访问者 IP。如果设置了 cookie,那么它将检查该访问者的国家并打印基于特定区域的消息。目前,当我运行我的脚本时,我所描述的都没有实际发生:

<?php
function ip_visitor_country()
{
GLOBAL $country ;

if(isset($_COOKIE["country"])=='') {

$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
//$remote ='1.6.0.0';
$country = "Unknown";
setcookie('country',$country,time()+(86400));

if(filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $forward;
}
else {
$ip = $remote;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$ip_data_in = curl_exec($ch); // string
curl_close($ch);

$ip_data = json_decode($ip_data_in,true);
$ip_data = str_replace('&quot;', '"', $ip_data); // for PHP 5.2 see stackoverflow.com/questions/3110487/
if($ip_data && $ip_data['geoplugin_countryName'] != null) {
$country = $ip_data['geoplugin_countryName'];
}
}
else {
// return 'IP: '.$ip.' # Country: '.$country;
echo $country;
if($country=='India') {
echo'Msg';
}
else {
echo'Msg1';
}
}
}

echo ip_visitor_country(); // output Coutry name
?>

最佳答案

看看这是否是您想要做的。很难说,但就关键路径而言,您的脚本似乎无处不在:

<?php
// Create a cURL function to accept a URL. Helps keep your code clean
function cURL($url = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch); // string
curl_close($ch);
// for PHP 5.2 see stackoverflow.com/questions/3110487/
// Convert your json to an array here
return (!empty($response))? json_decode(str_replace('&quot;','"',$response),true) : false;
}
// This will both set and return the country to and from cookie
function set_country_cookie()
{
// Globals are not great, but do it here if you
// are going to use it
global $country;
// Assign the ip defaults here
// Don't suppress errors, fix them
$client = (!empty($_SERVER['HTTP_CLIENT_IP']))? $_SERVER['HTTP_CLIENT_IP'] : false;
$forward = (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))? $_SERVER['HTTP_X_FORWARDED_FOR'] : false;
$remote = $_SERVER['REMOTE_ADDR'];
// Get the ip
if(filter_var($client, FILTER_VALIDATE_IP))
$ip = $client;
elseif(filter_var($forward, FILTER_VALIDATE_IP))
$ip = $forward;
else
$ip = $remote;
// Apply the handy dandy cURL function
$ip_data = cURL("http://www.geoplugin.net/json.gp?ip=".$ip);
// Assign country here
if(!empty($ip_data['geoplugin_countryName']))
$country = $ip_data['geoplugin_countryName'];
// Check that the return is not empty
if(!empty($country))
setcookie('country',$country,time()+(86400));
// Return the country, even after setting cookie
return $country;
}
// This is the main function that applies all the others
function ip_visitor_country()
{
// If you want to echo the function, use a buffer here
ob_start();
// Call your...ugh...global
global $country;
// Get the country from the cookie or create a new one
$country = (empty($_COOKIE["country"]))? set_country_cookie() : $_COOKIE['country'];
// Echo your country-specific message
echo ($country == 'India')? 'You get a message specific to India' : $country." is not as cool as India";
// Get the contents of the buffer
$data = ob_get_contents();
ob_end_clean();
// Return the contents
return $data;
}

// Echo the message
echo ip_visitor_country();
// See the results of the cookie
print_r($_COOKIE['country']);

在我的例子中,这是我会得到的:

United States is not as cool as India
United States

关于php - 检查特定访客的 COOKIE 是否已设置/为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32879302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com