HTML代码-js方式,查看源代码
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>ip2</title>
<script type="text/javascript">
function showJson(){
var test;
if(window.XMLHttpRequest){
test = new XMLHttpRequest();
}else if(window.ActiveXObject){
test = new window.ActiveXObject();
}else{
alert("请升级至最新版本的浏览器");
}
if(test !=null){
test.open("GET",location.protocol+'//'+location.hostname+'/ip/index.php',true);
test.send(null);
test.onreadystatechange=function(){
if(test.readyState==4&&test.status==200){
var data = JSON.parse(test.responseText);
document.getElementById("address").innerHTML = "IP:" + data.data.ip+data.data.region+data.data.city+'['+data.data.isp+']';
}
};
}
}
window.onload=function(){
showJson();
};
</script>
</head>
<body>
<center id="address"></center>
</body>
</html>HTML代码-jquery方式,查看源代码
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>ip</title>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type : 'get',
url : location.protocol+'//'+location.hostname+'/ip/index.php',
dataType : 'json',
success : function(data) {
if (data.code==0) {
var str = '';
if (data.data.country_id=='CN') {
str += data.data.ip+data.data.region+data.data.city+'['+data.data.isp+']';
} else {
str += data.data.country+' '+data.data.area;
}
$('#address').text("IP:"+str)
} else {
$('#address').text('(没查到)');
}
},
error : function(data) {
console.log(data);
}
});
});
</script>
</head>
<body>
<center id="address"></center>
</body>
</html>PHP代码-/ip/index.php-放在自己的网站里
<?php
function getip(){
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '127.0.0.1';
}
$ip = getip();
$ipjson = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip);
echo $ipjson;
die();织梦二次开发QQ群
本站客服QQ号:862782808(点击左边QQ号交流),群号(383578617)
如果您有任何织梦问题,请把问题发到群里,阁主将为您写解决教程!
转载请注明: 织梦模板 » 织梦网站js/jquery用php+ajax利用taobao淘宝IP库在html页面中获取访客ip和所在地





































