Click to See Complete Forum and Search --> : What are Sessions?


therealphil
09-09-2003, 05:50 AM
Hi.
I've seen lots of posts about sessions and cookies. What are sessions and how can I use them? Can I use Javascript to create sessions? How similar are they to using cookies to store information?

Thanks, Phil

Fang
09-09-2003, 06:09 AM
sessions are PHP, ask in their forum.

Khalid Ali
09-09-2003, 06:12 AM
Literally, a session is probably time between to seconds, minutes, hours and so on, in web programming, a session is that starts in the browser the very moment a client comes to a website. Typically this session is valid until the user does not close the window. Sessions are one of the very useful features of programming languages such as JSP, PHP etc.
In these programming languages you can explicitly create a session object, and use it to pass data from one page to another with the current browser session for X user.

Sessions are beyond JavaScript's scope, however, you may be able to mimic session behavior using cookies( setting at one page and then reading it on another page).

I hope this h elps

Scriptage
09-09-2003, 11:02 AM
Hi,
I've written a program to emulate sessions in Javascript:

Source of session.js

function session(){
// Hold all of the commands to run the session withing this function
this.get = get;
this.set = set;
// End commands

// Set variables
this.cookie = new Array();
this.length = 0;
return this;
}

function set(name, value){
with (this);
// Set the cookie
document.cookie = name + "=" + value + ";expires=";
}

function get(){
with (this);
if(!document.cookie) return false;
// Retrieve the cookie and put the information in "cookie"
var thisCookie = document.cookie.split(";");
for (var i=0; i<thisCookie.length; i++){
this.cookie[this.length] = new Array();
this.cookie[this.length][0] = thisCookie[i].split("=")[0];
this.cookie[this.length][1] = thisCookie[i].split("=")[1];
this.length = this.length+1;
}
return this;
}

To load the source:

<script src="session.js"></script>

To start a session:

var mySession = new session();

To set a cookie:

mySession.set("firstname","Carl");
mySession.set("lastname","Bates");

To get all cookies:

if(mySession.get()){
for(i=0; i<mySession.length; i++){
alert(mySession.cookie[i][0] + " = " + mySession.cookie[i][1]);
}
}

NB:

To retrieve a cookie on a new page you MUST start a new session.

I have uploaded all the necessary files for this to work and an example.

Regards

therealphil
09-09-2003, 11:20 AM
Brilliant!! :D

Thanks ;)

Webskater
09-09-2003, 12:04 PM
One thing you might want to be aware of is that Sessions occur on the web server - cookies are on the client. So, if you requests a page that requires cookies the server has to go to the users machine, retrieve the cookies and then carry on with the page. Normally this takes little time and little bandwidth but if you were to use this sort of thing on every page on a site so you could keep accessing variables you would be better off using session variables.
You should also note that session variables invariably time out - commonly after 20 minutes of inactivity between the browser and server. On the other hand a lot of people are nervous about cookies and the latest browsers have more sophisticated tools to disable cookies etc.