473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

using session values

I have done alot of web work in all sorts of languages but I have never used
the simple cookie. Question is
How does one set and retrieve a simple session cookie. All it will do is
have an image change on 1 page only with every new visitor, there are 5
images it should be random and stay the same unless the session is ended.
Sounds pretty simple but I cannot find simple examples of a cookie doing
anything similar to this. I found oodles of stuff on what a cookie is and
how it works but I could use an example, even if someone knows a website
that has simple clear examples, and to prove my ignorance what prog.
language are they written in. I have always used variables associated with a
db in asp but asp is not an option so it looks like html or JavaScript ????

Here is a simple image rotator but how would you set the cookie and check to
see if it is a new session for the rotator?
<SCRIPT LANGUAGE="JavaScript">
image = new Array(3);
image[0] = 'images/bodymidrow1col2.jpg'
image[1] = 'images/bodymidrow1col2_2.jpg'
image[2] = 'images/bodymidrow1col2_3.jpg'
index = Math.floor(Math.random() * image.length);
document.write("<DL>\n");
document.write("<IMG SRC="+image[index]+" height=120>");
</SCRIPT>

J
Jul 23 '05 #1
1 3571
In article <8d**************************@posting.google.com >,
st**************@yahoo.co.uk enlightened us with...
I have done alot of web work in all sorts of languages but I have never used
the simple cookie. Question is
How does one set and retrieve a simple session cookie.


A session cookie is merely one that has no expiration set, thus doesn't
really save itself to the cache.
So, the same way one uses any cookie, but don't set an expires time.

Here is my cookies file for JS. Have fun.
(watch for word wrap)

/* jsCookies.js */
/* This file contains cookie functions. */
/* File Functions:
1. setCookie - writes cookie
2. getCookie - gets value of cookie
3. removeCookie - deletes a cookie
4. detectCookies - checks if cookies are enabled
*/

function setCookie(cookieName, cookieValue, expireDays)
{
/* Pass in three strings - the name of the cookie, the value, and the
expire date.
Pass in a "" empty string for expireDate to set a session cookie
(no expires date).
Pass in any other date for expire as a number of days to be added
to today's date. */

if (expireDays== "")
{
expires = "";
}
else
{
expires = new Date();
expires.setDate(expires.getDate() + expireDays);
expires = expires.toGMTString();
}
document.cookie = cookieName+"="+cookieValue+";expires="+expires;
}

function removeCookie (cookieName)
{
/* Pass in the name of the cookie as a string and it will be removed.
*/
expires = Now();
document.cookie = cookieName+"= ;expires="+expires.toGMTString();
}

function getCookie (cookieName)
{
cookieValue = ""
if (document.cookie.indexOf(cookieName) == -1)
{
// there is no cookie by this name for this user
return cookieValue;
}
else
{
// get the beginning index of the cookie by looking for the cookie
name
cookieStart = document.cookie.indexOf(cookieName);
// get the beginning index of the cookie value by looking for the
equal sign after the name
cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
// get the end index of the cookie value by looking for the semi-
colon after the value
cookieValEnd = document.cookie.indexOf(";", cookieStart);
// if no semi-colon, then use the whole length
if (cookieValEnd == -1)
{
cookieValEnd = document.cookie.length
}
// use substring to get the text between the two indices and that
is the value of the cookie
cookieValue = document.cookie.substring(cookieValStart,
cookieValEnd);
return cookieValue;
}
}

function detectCookies()
{
/* function returns true if cookies are enables, false if not */
setCookie("test", "test", "");
tmp = getCookie("test")
if (tmp != "test")
{
return false;
}
else
{
return true;
}
}

--
--
~kaeli~
If that phone was up your a$$, maybe you could drive a
little better!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Anne | last post by:
hie there, i want to be able to pass multiple parameters to another page. currently, i am able to do so, but somehow i feel it is not the correct way to do it. below is part of what i have so far....
5
by: Abhilash.k.m | last post by:
This is regarding the session management using Out of proc session management(SQL SERVER). Among the samples below which one is better to set the session? 1. There are 20 session...
17
by: Rico | last post by:
Hello, I am in the midst of converting an Access back end to SQL Server Express. The front end program (converted to Access 2003) uses DAO throughout. In Access, when I use recordset.AddNew I...
17
by: Rico | last post by:
Hello, I am in the midst of converting an Access back end to SQL Server Express. The front end program (converted to Access 2003) uses DAO throughout. In Access, when I use recordset.AddNew I...
2
by: charliej2001 | last post by:
Hi all I'm trying to set up an Access database that will be able to import contact information from the lotus notes 6.5 Address book. The idea is that the code runs from access to import all of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.