如果PHP无法加载特定的URL内容,如何使用jquery创建cookie


How to create cookie using jquery if PHP fails to load a particular URL content

以下PHP+jQuery代码中缺少什么

$ip_address=$_SERVER['REMOTE_ADDR'];    
if(!isset($_COOKIE['isp'])){
    $ispinfo=@file_get_contents("http://ipinfo.io/".$ip_address."/org");
    if($ispinfo==FALSE){
    echo '<script>
    $.get( "http://ipinfo.io/'.$ip_address.'/org", function( data ) {
       var d = new Date();
        d.setTime(d.getTime() + (24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = "isp=" + data+ "; " + expires+ "; " + path=/";
    });
    </script>';
    } else {
    $isp_code_string = explode(" ",$ispinfo);
    $isp_code=$isp_code_string[0];
    $isp_name=str_replace($isp_code, '', $ispinfo);
    setcookie("isp", $isp_name, time()+10*365*24*60*60, "/");
    }
    } else {
    $isp_code_string = explode(" ",$_COOKIE["isp"]);
    $isp_code=$isp_code_string[0];
    $isp_name=str_replace($isp_code, '', $ispinfo);
    $isp_name="**".$isp_name;
    }

PHP运行良好,但当$ispinfo=@file_get_contents("http://ipinfo.o/".$ip_address."/org");失败时,它不会使用jQuery创建cookie,尽管jQuery cum JS的源代码出现在源代码(HTML(中我对PHP有很好的了解,但对jQuery缺乏了解。ipinfo.io的问题是它的IP访问权限有限,使用jQuery我想使用用户的IP来获取和创建稍后可以访问的cookie。

使用jQuery和jQuery。Cookies我用以下代码解决了问题

$ip_address=$_SERVER['REMOTE_ADDR'];    
if(!isset($_COOKIE['isp'])){
    $ispinfo=@file_get_contents("http://ipinfo.io/".$ip_address."/org");
    if($ispinfo==FALSE){
    echo '<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<script>
$.get( "http://ipinfo.io/'.$ip_address.'/org", function( data ) {
$.cookie("isp", data, { path: "/", expires: 7 });
$isp_name="jQ";
});
</script>';
    } else {
    $isp_code_string = explode(" ",$ispinfo);
    $isp_code=$isp_code_string[0];
    $isp_name=str_replace($isp_code, '', $ispinfo);
    setcookie("isp", $isp_name, time()+10*365*24*60*60, "/");
    }
    } else {
    $isp_code_string = explode(" ",$_COOKIE["isp"]);
    $isp_code=$isp_code_string[0];
    $isp_name=str_replace($isp_code, '', $ispinfo);
    $isp_name="**".$isp_name;
    }