/    Sign up×
Community /Pin to ProfileBookmark

Help with javascript – Cookies and redirect

Hi i need to redirect users using javascript and cookies.

The desired script need to work in this way

Any page on the site (including pages on folders) check for a cookie.
If the cookie isn’t there the script redirect to the home page of the site.
This need to happen once.

Basically the cookie will be set by the home page, so all pages will check if the users had been there or not. if not, the user will get redirected, if yes the script do nothing.

The script need to work in a subdomain – the index is in a subdomain (example.domain.com) so all pages like
example.subdomain.com/folder/
example.subdomain.com/folder/folder2/
example.subdomain.com/folder/folder 2/page.html

All these types of pages redirect to example.subdomain.com

So far i get something like this

function 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 “”;
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toUTCString());
}

function checkCookie()
{
username=getCookie(‘username’);
if (username!=null && username!=””)
{

}
else
{
window.location.replace = “http://subdomain.domain.com
if (username!=null && username!=””)
{
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toUTCString());
}
}
}
}

But that don’t work

If you have any idea how to achieve that please let me know.

Sorry for my poor english

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineOct 18.2010 — [CODE]<script type='text/javascript'>

function readCookie( cName )
{
var v;

return ( document.cookie !== undefined )
? decodeURIComponent ( (v = document.cookie.match( "(^|\s|;)"+cName+"=([^;$]+)" ) ) ? v[2] : "" )
: "";
}

function setCookie( cName, value, days, path )
{
var expires = "", dt = new Date();

if( days )
expires = "; expires=" + new Date( dt.setDate( dt.getDate() + days ) ).toGMTString();

document.cookie = cName + "=" + encodeURIComponent( value ) + expires + (path ? "; path=" + path : "" );

return readCookie( cName );
}

if( !readCookie( 'userName' ) && location.href != "http://subdomain.domain.com" )
location.href = "http://subdomain.domain.com";

setCookie( 'userName', 'ok', 0, '/' );

</script>[/CODE]
Copy linkTweet thisAlerts:
@GymsauthorOct 18.2010 — A MILLON THANKS !!!

You just make me really happy ?

Thanks
Copy linkTweet thisAlerts:
@GymsauthorOct 22.2010 — Sterling Isfine:

Your code works perfect, and do the job, but in recent days i just spot a problem. Using Google Analytics i notice that i get a lot of trafic from no referal sites most of my visit come now from "((direct)(none))"

That becomes a problem as i can't track the keywords that bring visitors to my site from search engines.

After a few experiments i found a way to solve the issue.

I just replace the last part of the code with this

if( !readCookie( 'userName' ) && location.href != "http://subdomain.domain.com" )

document.write('<meta http-equiv="refresh" content="0;url=http://subdomain.domain.com/p/new-search.html">');

document.close();

setCookie( 'userName', 'ok', 0, '/' );


I don't know if there is other ways to perform the same task without losing the "referal sites" or "Keywords" info from Google Analytics, but since that small change works for me i will like to you to take a look and see if there is something wrong, or something that can be improved, since i make that change after a few tests.... I don't know anything about javascript, so i guess it was just luky
Copy linkTweet thisAlerts:
@GymsauthorOct 22.2010 — Sorry... that code is not the right one, the correct code is this:

if( !readCookie( 'userName' ) && location.href != "http://subdomain.domain.com" )

document.write('<meta http-equiv="refresh" content="0;url=http://subdomain.domain.com">');

document.close();

setCookie( 'userName', 'ok', 0, '/' );

You get the idea any way

Thanks again
Copy linkTweet thisAlerts:
@khstumpOct 22.2010 — I have everything working, I think. I believe my problem is comparing the cookie to the actual text typed into the First and Last Name Fields. How do you compare a cookie value to the text value? I'm guessing that it is in the checkCookie() function:

function checkCookie()

{

username=getCookie('name');

if (username!=null && username!="") --- PROBABLY HERE?

{

alert('Welcome again '+username+'!');

}

else {

if (username!=null && username!="")

{

setCookie('name',document.contactform.fname.value+' '+document.contactform.lname.value,365);

}

}

}
×

Success!

Help @Gyms spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...