How do you save a session value in JavaScript?

Please help!
Sally

Recommended Answers

All 4 Replies

Use cookies. google it with javascript and cookies. There are a ton of functions for it.

There are more ways to do it than just cookies - it basicaly depends if you want to store the session between visits that happend at the same time, or if you want to save sessions for later use.

If the later is what you want, go for cookies:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

If you just want to save the session while the user leaves your pages you could use a hidden frame and store the session value inside a hidden input.

Please help anybody.

how to use php session value in javascript?

There are more ways to do it than just cookies - it basicaly depends if you want to store the session between visits that happend at the same time, or if you want to save sessions for later use.

If the later is what you want, go for cookies:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

If you just want to save the session while the user leaves your pages you could use a hidden frame and store the session value inside a hidden input.

javascript only support cookies. You can set them up to keep track of a user session but they do not support the use of sessions. PHP uses both sessions ans cookies. If you trying to pass in session variables from PHP to Javascript you will have to come up with a good work around since there is no direct way to do this. you can use hidden fields that generate the variable which you can then get in javascript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.