...

View Full Version : javascript to read and write IP cookie



juanlu
05-17-2009, 11:03 AM
Hello !

I have a script that i need to read the ip from a visitor and set a cookie, so the script will never be executed again for that particular ip address.

Right now i'm having problems with the current cookie set of my script, i can't seem to make it not appear again after it's executed (which is my goal). That's why i want to try using an IP cookie, and see if that works.

I'm kind of a newbie, i'd really appreciate any help.

juanlu
05-17-2009, 11:08 AM
/*****************************************************/
/*USER CUSTOMIZABLE FIELDS*/

/*HEADLINE*/
var blocker_headline = '';

/*INSTRUCTIONAL TEXT*/
var blocker_instructionalText = '';

/*FOOTER TEXT*/
var blocker_footerText = '';

/*OTR LINKS*/
var blocker_surveryLinks = [
{text:'CLICK HERE TO CONTINUE', url:'http://aaa.com'}
]

/*END OF USER CUSTOMIZABLE FIELDS*/
/******************************************************/

var blocker_originalHtmlOverflow;
var blocker_originalBodyOverflow;

function blocker_addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}
else
{
window.onload = function()
{
oldonload();
func();
}
}
}

blocker_addLoadEvent(blocker_init);

function blocker_init()
{
if(blocker_getCookie('content_unlocked')=='1')
return;

blocker_originalHtmlOverflow = document.getElementsByTagName('body')[0].style.overflow;
blocker_originalBodyOverflow = document.getElementsByTagName('html')[0].style.overflow;

document.getElementsByTagName('body')[0].style.overflow = 'hidden';
document.getElementsByTagName('html')[0].style.overflow = 'hidden';

var haze = document.createElement('div');
haze.id = 'blocker_haze';
haze.style.filter = 'alpha(opacity=50)';
haze.style.opacity = .5;
haze.style.height = '100%';
haze.style.width = '100%';
haze.style.backgroundColor = '#000';
haze.style.position = 'absolute';
haze.style.top = '0px';
haze.style.left = '0px';
haze.style.zIndex = 1000000;

var centerPane = document.createElement('centerPane');
centerPane.id = 'blocker_centerPane';
centerPane.style.width = '500px';
centerPane.style.height = '370px';
centerPane.style.border = '5px solid #CCC';
centerPane.style.width = '500px';
centerPane.style.position = 'absolute';
centerPane.style.left = '50%';
centerPane.style.marginLeft = '-250px';
centerPane.style.top= '100px';
centerPane.style.backgroundColor = '#FFF';
centerPane.style.zIndex = 1000001;
centerPane.style.backgroundImage = 'url(http://aaa.jpg)';
centerPane.style.backgroundRepeat = 'no-repeat';
centerPane.style.backgroundPosition = '20px 15px';
centerPane.style.padding = '20px';

var h1 = document.createElement('h1');
h1.style.color = '#000';
h1.style.textAlign = 'center';
h1.style.fontSize = '36px';
h1.style.margin = '0 0 10px 0';
h1.style.padding = '8px 0 0 15px';
h1.style.fontFamily = 'arial';
h1.style.lineHeight = '36px';
h1.innerHTML = blocker_headline;
centerPane.appendChild(h1);

var p1 = document.createElement('p');
p1.innerHTML = blocker_instructionalText;
p1.style.textAlign = 'center';
p1.style.padding = '20px 0 20px 0';
p1.style.margin = '0';
p1.style.fontSize = '18px';
p1.style.lineHeight = '18px';
p1.style.color = '#000';
p1.style.fontFamily = 'arial';
centerPane.appendChild(p1);

var ul = document.createElement('ul');
ul.style.textAlign = 'center';
ul.style.margin = '0 0 500px 0';
ul.style.padding = '0';
ul.style.listStyleType = 'none';
for(var i = 0;i<blocker_surveryLinks.length;i++){
var li = document.createElement('li');
var a = document.createElement('a');
a.style.display = 'block';
a.style.fontSize = '19px';
a.style.lineHeight = '450px';
a.style.color = 'blue';
a.style.fontFamily = 'arial';
a.style.textDecoration = 'underline';
a.target = '_blank';
a.href = blocker_surveryLinks[i].url;
a.innerHTML = blocker_surveryLinks[i].text;
a.onclick = function(){
window.setTimeout(unblockContent, 45000);
}
li.appendChild(a)
ul.appendChild(li);
}

centerPane.appendChild(ul);

var p2 = document.createElement('p');
p2.style.textAlign = 'center';
p2.innerHTML = blocker_footerText;
p2.style.padding = '20px 0 0 0';
p2.style.margin = '0';
p2.style.color = '#F50E0E';
p2.style.fontFamily = 'arial';
p2.style.fontSize = '15px';
p2.style.lineHeight = '15px';
centerPane.appendChild(p2);

document.getElementsByTagName('body')[0].appendChild(haze);
document.getElementsByTagName('body')[0].appendChild(centerPane);
}

function unblockContent(){
document.getElementsByTagName('body')[0].style.overflow = blocker_originalBodyOverflow;
document.getElementsByTagName('html')[0].style.overflow = blocker_originalHtmlOverflow;

document.getElementById('blocker_haze').style.display = 'none';
document.getElementById('blocker_centerPane').style.display = 'none';
blocker_setCookie("content_unlocked", 1, 365);
}

function blocker_setCookie(c_name,value,expiredays){
var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function blocker_getCookie(c_name){
if (document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

juanlu
05-17-2009, 11:15 AM
The objective of this script is to show a window that loads on top of the page blocking the access to it, the visitor has to click on the link included in that window, after visitor clicks the link, the window closes in 45 seconds and the page is unlocked.

I need this to happen just one time for each ip address that visits the page. This window cannot appear again for the same visitor.

Philip M
05-17-2009, 11:22 AM
Cookies can be and often are erased.

juanlu
05-17-2009, 11:34 AM
Yes, that's true. When i say never i mean, that i need the window to not appear again at least until the cookies are erased. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum