Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2006
    Location
    Nottingham, UK
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Redirect when cookie NOT present?

    Hello there. I'm very new to javascript and was hoping somebody could help me.

    I've found a script that forwards you to a new page if you have stored a cookie on a previous visit... and am trying to make it do the opposite!

    What I'm trying to achieve is a script that will leave the user on the page if they HAVE the cookie, but will forward them somewhere else if they DO NOT have it (ie in order to prevent people without the cookie from staying on the page).

    The script I'm using is here:

    Code:
    <script>
    <!--
    /* Copyright http://www.perlscriptsjavascripts.com 
       Free and commercial Perl and JavaScripts     */
       
    // page to go to if cookie exists
    go_to = "enter.htm";
    
    function readCookie(cookieName){
        var start = document.cookie.indexOf(cookieName);
        if (start == -1){ 
            document.cookie = "access=no; expires=" + ged(num_days);
        } else {
            window.location = go_to;
        }
    }
    
    readCookie("access");
    // -->
    </script>
    I don't particularly want people to have to log in to access the page - one password for everyone should suffice... is this possible? Am I making things unnnecessarily complicated?

    All suggestions welcomed!


  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    You could swap your statements round maybe

    PHP Code:
    <script>
    <!--
    /* Copyright http://www.perlscriptsjavascripts.com 
       Free and commercial Perl and JavaScripts     */
       
    // page to go to if cookie does not exist
    go_to "enter.htm";

    function 
    readCookie(cookieName){
    var 
    start document.cookie.indexOf(cookieName);

    if (
    start == -1){ 
    window.location go_to;
    }
    else{
    document.cookie "access=no; expires=" ged(num_days);
    }
    }

    readCookie("access");
    // -->
    </script> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    or invert the test:
    Code:
    function readCookie(cookieName){
        var start = document.cookie.indexOf(cookieName);
        if (start != -1){ 
            document.cookie = "access=no; expires=" + ged(num_days);
        } else {
            window.location = go_to;
        }
    }
    != means "is not"

  • #4
    New Coder
    Join Date
    Jan 2006
    Location
    Nottingham, UK
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's brilliant, thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •