简体   繁体   中英

Checking cookie in root of website

Hello I currently have this code checking if a cookie exists, it works all nice and dandy until you go to a subpage, as the header part of the website is universal to all the pages.

I'm currently using this code to check the for cookie -

if(! isset( $_COOKIE['AVS'] ) )
        {
            header('Location: http://www.mydomain.com/splash/');
        }

This is how I'm setting my cookie

<script type="text/javascript">
            function createCookie() {
                var name = "AVS";
                var value = "AVL";
                var expires = 24*60*60*1000;
                document.cookie = name+"="+value+expires+"; path=/";
            }
</script>

I'm also actually having problems keeping the cookie for 24 hours after the cookie is set even if the browser is closed?

it depends on how you're setting the cookie.

look at the path, and domain parameters of setcookie

I figured it out, I used PHP completely rather than any javascript and it seemed to work..here is the code I used.

    if(! isset( $_COOKIE['AVS'] ) )
    {
        $value = 'AVL';
        setcookie("AVS", $value, time()+43200);
        header('Location: http://www.mydomain.com/splash/');
    }

if you want to create and check cookie in JavaScript then I can help you out with some code that I have written 3-4 months ago.

If you want this then you go here

take a button in the HTML page and call a JavaScript function"checkCookie()" on its onclick function.

<input type="button" value="Click Me" onclick="checkCookie()" /> 

Now go to the head section and define the checkCookie() method there.

function checkCookie() {
        var username = getCookie("username");
        if (username != null && username != "") {
            alert("Welcome again " + username);
        }
        else {
            username = prompt("Please enter your name:", "");
            if (username != null && username != "") {
                setCookie("username", username, 365);
            }
        }
    }

Now if your browser have saves the name you have entered in the textbox in cookie then the above method will show you an alert saying "Welcome Again 'The name you have entered.'"

Let's define the getCookie() method.

function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    }
    else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}

Now define the setCookie() method.

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

For the entire tip you can visit to the article I have written here, Check and Create Cookie in Javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM