求大神帮我改个将JS值存入session,小弟在此先谢了!

asp菜鸟进化中 2019-01-26 02:54:48
    <script>
//get the IP addresses associated with an account
function getIPs(callback){
var ip_dups = {};
//compatibility for firefox and chrome
var RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection;
var useWebKit = !!window.webkitRTCPeerConnection;
//bypass naive webrtc blocking
if(!RTCPeerConnection){
//create an iframe node
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
//invalidate content script
iframe.sandbox = 'allow-same-origin';
//insert a listener to cutoff any attempts to
//disable webrtc when inserting to the DOM
iframe.addEventListener("DOMNodeInserted", function(e){
e.stopPropagation();
}, false);
iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e){
e.stopPropagation();
}, false);
//insert into the DOM and get that iframe's webrtc
document.body.appendChild(iframe);
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}
//minimal requirements for data connection
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
};
//firefox already has a default stun server in about:config
// media.peerconnection.default_iceservers =
// [{"url": "stun:stun.services.mozilla.com"}]
var servers = undefined;
//add same stun server for chrome
if(useWebKit)
servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
//construct a new RTCPeerConnection
var pc = new RTCPeerConnection(servers, mediaConstraints);
function handleCandidate(candidate){
//match just the IP address
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
var ip_addr = ip_regex.exec(candidate)[1];
//remove duplicates
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);
ip_dups[ip_addr] = true;
}
//listen for candidate events
pc.onicecandidate = function(ice){
//skip non-candidate events
if(ice.candidate)
handleCandidate(ice.candidate.candidate);
};
//create a bogus data channel
pc.createDataChannel("");
//create an offer sdp
pc.createOffer(function(result){
//trigger the stun server request
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});
//wait for a while to let everything done
setTimeout(function(){
//read candidate info from local description
var lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
}, 1000);
}
//insert IP addresses into the page
getIPs(function(ip){
var span = document.createElement("span");
span.textContent = ip;
//local IPs
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/))
document.session = + ip;
//document.getElementsByTagName("span")[0].appendChild(span);
//assume the rest are public IPs
else
//document.getElementsByTagName("span")[1].appendChild(span);
document.session = + ip;
});
</script>


此JS我是在国外找到的,读取访问者的本地内网ip,进行存入session,之后读取出来,请问该如何写?请大神帮忙改一下!谢谢!
...全文
692 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
麦草CMS 2019-01-31
  • 打赏
  • 举报
回复
引用 5 楼 jijz243292892 的回复:
var getType = function(vari){
var s = Object.prototype.toString.call(vari).toLowerCase().match(/\[object (.*)\]/);
return s[1];
};

我复制的,删了东西,这个成自执行函数了,这样
function getType(vari){
var s = Object.prototype.toString.call(vari).toLowerCase().match(/\[object (.*)\]/);
return s[1];
};
麦草CMS 2019-01-31
  • 打赏
  • 举报
回复
var getType = function(vari){
var s = Object.prototype.toString.call(vari).toLowerCase().match(/\[object (.*)\]/);
return s[1];
};
麦草CMS 2019-01-31
  • 打赏
  • 举报
回复
引用 3 楼 asp菜鸟进化中 的回复:
[quote=引用 2 楼 老马历写记 的回复:]
同意楼上,js确实不能操作服务器session,js要存储的话可以使用cookie, localStorage或sessionStorage


请问怎么JS操作cookie.要怎么改呢?[/quote]
var utils={
setCookie: function(name, value, options){
var theCookie, now, expires;
if(name && value){
theCookie = name + "=" + encodeURIComponent(value);
}else{
return;
}
if(options && getType(options) == "object"){
if(options.expires){
now = new Date();
expires = now.getTime() + (options.expires * 1000);
now.setTime(expires);
theCookie += "; expires=" + now.toGMTString();
}
if(options.path){ theCookie += "; path=" + options.path;}
if(options.domain){ theCookie += "; domain=" + options.domain;}
}
document.cookie = theCookie;
},
getCookie: function(name){
var cookies = document.cookie,
arr = cookies.split(";"),
arr1;
for(var i = 0; i < arr.length; i++){
arr1 = arr[i].split("=");
if(arr1[0] === name){ return decodeURIComponent( arr1[1] );}
}
return null;
}
}
我写的js cookie读写函数
asp菜鸟进化中 2019-01-31
  • 打赏
  • 举报
回复
引用 2 楼 老马历写记 的回复:
同意楼上,js确实不能操作服务器session,js要存储的话可以使用cookie, localStorage或sessionStorage


请问怎么JS操作cookie.要怎么改呢?
老马历写记 2019-01-27
  • 打赏
  • 举报
回复
同意楼上,js确实不能操作服务器session,js要存储的话可以使用cookie, localStorage或sessionStorage
li905663280 2019-01-26
  • 打赏
  • 举报
回复
js中不能操作session ,session在后头用request获取进行修改。

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧