Topic: JavaScript Cookie Redirect Pages that link to <a href="https://ozoneasylum.com/backlink?for=30213" title="Pages that link to Topic: JavaScript Cookie Redirect" rel="nofollow" >Topic: JavaScript Cookie Redirect\

 
Author Thread
cyanide
Obsessive-Compulsive (I) Inmate

From:
Insane since: Apr 2008

IP logged posted posted 04-19-2008 07:27 Edit Quote

Hello everybody. I just joined this forum today and I must say I'm glad I did, helped me in making a cookie redirect script. I though to share this one because believe me I've searched everywhere... no skills = no power I suppose .

This is what I wanted the script to do:

User enters page A and moves on to page B, if he tries to access page A again to be redirected to page C.

I tried at first with a PHP cookie redirect but I didn't like the fact that I needed to use the Refresh button for the cookie to load... I don't know much PHP coding so...

Script placed in page B:

code:
<script language="JavaScript">
function setCookie(name, value, expires)
{
	
        if (!expires) expires = new Date(); 
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() + "; path=/";
}
var expdate = new Date ();
expdate.setTime(expdate.getTime() + 999999999);
setCookie("NameOFtheCOOKIE","true",expdate);
</script>



Script placed on page A:

code:
<script>
<!--

go_to = "http://www.site.com/pageC.html";

function readCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
        document.cookie = "NameOFtheCOOKIE" + ged(num_days);
    } else {
        window.location = go_to;
    }
}

readCookie("NameOFtheCOOKIE");
// -->
</script>



This was done by copying the source from another script...if there's a way to make it simpler I'd really appreciate the effort. It gives some errors on this line:

document.cookie = "NameOFtheCOOKIE" + ged(num_days);



Thanks.

Original script:

code:
<script>
<!--

go_to = "http://www.site.com";

num_days = 60;
function ged(noDays){
    var today = new Date();
    var expr = new Date(today.getTime() + noDays*24*60*60*1000);
    return  expr.toGMTString();
}

function readCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
        document.cookie = "seenit=yes; expires=" + ged(num_days);
    } else {
        window.location = go_to;
    }
}

readCookie("seenit");
// -->
</script>



(Edited by cyanide on 04-19-2008 08:11)

kuckus
Paranoid (IV) Mad Librarian

From: ber/ger
Insane since: Dec 2001

IP logged posted posted 04-21-2008 21:42 Edit Quote

Hello cyanide, welcome to the funny farm

Looks like your script is missing this chunk of the original code:

code:
num_days = 60;
function ged(noDays){
    var today = new Date();
    var expr = new Date(today.getTime() + noDays*24*60*60*1000);
    return  expr.toGMTString();
}



Without that, your script can't know about the "ged" function and about the "num_days" variable that you're passing to it as an argument.

So if you just put these declarations anywhere inside your <script> tags you should be fine.

cyanide
Nervous Wreck (II) Inmate

From:
Insane since: Apr 2008

IP logged posted posted 04-22-2008 01:26 Edit Quote

Hi kuckus, thank you for your help.

Modified the script placed on page A that looks like this:

code:
<script>
<!--

num_days = 5;
function ged(noDays){
    var today = new Date();
    var expr = new Date(today.getTime() + noDays*24*60*60*1000);
    return  expr.toGMTString();
}

go_to = "index2.php";
num_days = 5;
function readCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
        document.cookie = "bin" + ged(num_days);
    } else {
        window.location = go_to;
    }
}

readCookie("bin");
// -->
</script>



No more errors but unfortunately it sets another cookie with no name which messes everything up, I have no skills to edit the script myself and I'm sorry if I sound like a broken record .

Is there another solution for the script to just read the cookie and if its there to redirect to a given page ? After/If the first script writes the cookie .

I thank anyone who helps me make this script !

On the first page I had another script that placed a cookie based on a PHP command:

code:
<script language="JavaScript">
function setCookie(name, value, expires)
{
	
        if (!expires) expires = new Date(); 
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() + "; path=/";
}
var expdate = new Date ();
expdate.setTime(expdate.getTime() + 999999999);
setCookie("<?php echo $sold;?>","true",expdate);
</script>

<script>



And in the config.php file I have:

code:
$sold = "no";



On the same page below the first script is:

code:
<script>
<!--

go_to = "index3.php";
num_days = 5;
function readCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
    if (start == -1){ 
        document.cookie = "yes" + ged(num_days);
    } else {
        window.location = go_to;
    }
}

readCookie("yes");
// -->
</script>



After I edit the config.php file with the "yes" value page A redirects to a completely different page ( for the people who don't have the first cookie set on page B ...so, I modified both scripts with your suggestion kuckus, and it somehow writes this cookie:



This cookie is the 'yes' or 'no' cookie:



Any help in making this simple script SIMPLE would be great, without num_days or ged functions. The problem is the script that reads the cookie, not the one that sets it. Simple means: Read the cookie, if its not set/there do nothing, if its there redirect/go to another page...

On another note everything works fine with the first version of the script that gave those errors. IE 7 doesn't display "Done. But with errors on page" and on Firefox it works fine also, I know people don't use IE 6 that much but I don't like IE 7 at all and I want this script to work w/o any errors. I'm sure there are lots of people who might find this script usefull , I found this forum browsing on google for help

Thanks for reading.

(Edited by cyanide on 04-22-2008 01:34)

cyanide
Nervous Wreck (II) Inmate

From:
Insane since: Apr 2008

IP logged posted posted 04-26-2008 06:34 Edit Quote

Any ideas on the request i made ? I admit i'm a complete and utterly dumb ass when it comes to php/javascript coding!

1) A nice script to set the cookie
2) A nice script to read the cookie and if its there to redirect to a given page.
3) Note that the redirect must be based on cookie name not cookie value.




Thanks and Happy Easter !

(Edited by cyanide on 04-26-2008 06:36)



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu