Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 05-12-2008, 08:56 PM   #1 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Setting cookies in javascript


I looked all around the web, but could not find an article simple enough (for dummies) on setting and manipulating session cookies.

I have a poll running on PHP/MySQL and simple refreshing the page inserts the same data to the table. I don't want people to post again, so I want a session cookie which would expire (say in 5-10 minutes).

My requirements are as follows:
1. A function would check as the poll loads if the browser is set to accept sessions cookie. The javascript code, the functions and where to place, how to call etc.
2. A sessions cookie is set with expiry for 5 minutes. Code with example and explanation
3. Poll is submitted, if user resubmits or refreshes, a function should disable it and inform that it has been disabled.

I found information on setting, manipulating and reading very confusing. Someone please in as elementary language as possible, explain with examples.
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 05-12-2008, 09:24 PM   #2 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Setting cookies in javascript

Cookies are much simpler in PHP than in javascript.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Old 05-12-2008, 09:32 PM   #3 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Re: Setting cookies in javascript

Thanks Rohan, but your reply is not helping me. I have a problem and I need solution; with javascript or PHP.
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Old 05-12-2008, 10:02 PM   #4 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Setting cookies in javascript

Quote:
Originally Posted by Sridhar_Rao View Post
Thanks Rohan, but your reply is not helping me. I have a problem and I need solution; with javascript or PHP.
Lookup the PHP manual or Google on setting and manipulating cookies with PHP.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Old 05-12-2008, 10:06 PM   #5 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Re: Setting cookies in javascript

Thanks Rohan! I know you edited and removed those lines.
I know you have given plenty of helpful replies even before. I need your help!
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com

Last edited by Sridhar_Rao; 05-12-2008 at 10:15 PM.
Sridhar_Rao is offline  
Old 05-12-2008, 10:20 PM   #6 (permalink)
Broken In
 
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
Default Re: Setting cookies in javascript

I have been using cookies for quite some time now, but am not sure what they are - browser cookies or session cookies

See if my sample code is indeed using session cookies using JavaScript, and if we are on the same page, then we can move ahead with some discussion later.

Code from sridhar_rao_1.htm:
Code:
<HTML>
	<HEAD>
		<TITLE>Sridhar Rao</TITLE>
		<script language='JavaScript'>
		function doCooking()
		{
			//document.SridharsForm.SubmitButton.disabled='true';
			if(cookieExists())
			{
				alert('Stop fooling around, nigga!');
				return false;
			}
			else
			{
				setCookie(5);	// 5 minutes
				alert('Good going, mate!');
			}
			return true;
		}
		function cookieExists()
		{
			if(document.cookie.length > 0)
			{
				var begin = document.cookie.indexOf("SriCookie=");
				if(begin != -1)
				{
					begin += "SriCookie".length + 1;
					var end = document.cookie.indexOf(";", begin);
					if(end == -1)
						end = document.cookie.length;
					if('true' == unescape(document.cookie.substring(begin, end)))
						return true;
				}
			}
			return false;
		}
		function setCookie(expiryMins)
		{
			var ExpireDate = new Date();
			ExpireDate.setTime(ExpireDate.getTime() + (expiryMins * 60 * 1000));
			document.cookie = "SriCookie=true" + ((expiryMins == null) ? "" : "; expires=" + ExpireDate.toGMTString());
		}
		function deleteCookie()
		{
			if(cookieExists())
				document.cookie = "SriCookie=; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
		</script>
	</HEAD>
	<BODY>
	<form name='SridharsForm'>
		Poll. Question#1
			<LI>Blah Blah Blah... Choice 1</LI>
			<LI>Blah Blah Blah... Choice 2</LI>
			<LI>Blah Blah Blah... Choice 3</LI><BR><BR>
		<input type='radio' name='SridharRao1' value='1'/>1<BR>
		<input type='radio' name='SridharRao1' value='2'/>2<BR>
		<input type='radio' name='SridharRao1' value='3'/>3<BR>
		<input type='submit' name='SubmitButton' value='Submit' onClick='return doCooking()'/>
		<input type='button' name='NextPage' value='Next Page ==&gt;' onClick='window.location="sridhar_rao_2.htm"'/>
		<BR><BR><BR><BR><BR><BR><BR><BR>
		<input type='button' name='ClearButton' value='Clear Cookie (just to test)' onClick='deleteCookie()'/>
	</form>
	</BODY>
</HTML>
Code from sridhar_rao_2.htm:
Code:
<HTML>
	<HEAD>
		<TITLE>Sridhar Rao</TITLE>
		<script language='JavaScript'>
		function doNothing()
		{
			return false;
		}
		</script>
	</HEAD>
	<BODY>
	<form name='SridharsForm'>
		Poll. Question#2
			<LI>Dooo da doo da do... Choice 1</LI>
			<LI>Dooo da doo da do... Choice 2</LI>
			<LI>Dooo da doo da do... Choice 3</LI><BR><BR>
		<input type='radio' name='SridharRao2' value='1'/>1<BR>
		<input type='radio' name='SridharRao2' value='2'/>2<BR>
		<input type='radio' name='SridharRao2' value='3'/>3<BR>
		<input type='button' name='PrevButton' value='&lt;== Previous page' onClick='window.location="sridhar_rao_1.htm"'/>
		<input type='submit' name='SubmitButton' value='Submit' onClick='return doNothing()'/>
	</form>
	</BODY>
</HTML>
Copy the above 2 code snippets as sridhar_rao_1.htm and sridhar_rao_2.htm respectively.

Tip: If you uncomment the line document.SridharsForm.SubmitButton.disabled='true'
you might have almost similar result as you expect without using cookies, leaving apart the page refresh option though.
Bandu is offline  
Old 05-12-2008, 10:33 PM   #7 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: Setting cookies in javascript

Quote:
Originally Posted by Sridhar_Rao View Post
Thanks Rohan! I know you edited and removed those lines.
I know you have given plenty of helpful replies even before. I need your help!
Yes, I realized it was not kind enough of me to post that.
Anywayz, coming to your problem, its a simple 3 line solution:
On the form submission page:
1. check for a cookie everytime
2. if that cookie exists, return an error saying multiple submission not allowed.
3. if that cookie does not exist allow form submission and set the cookie.

PHP Code:
if(isset($_COOKIE['allow_form_submission']))
{
//sorry, cant submit multiple times
}
else
{
setcookie('allow_form_submission','no',time()+864000)//set the cookie with a life of 10 days
//proceeed with form submission

__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Old 05-12-2008, 11:35 PM   #8 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Re: Setting cookies in javascript

Thanks Rohan, that is exactly what I was looking for. It is indeed so simple in PHP than in javascript. I have implemented it already. The poll is on response to recent terror attack, please have a look. http://www.microrao.com/terror_poll.php
Thanks again.
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
cookies!!! prinz QnA (read only) 4 09-07-2007 01:05 PM
Rapidshare cookies ! ! ! ax3 QnA (read only) 3 31-03-2007 03:46 PM
Help on cookies wanted (javascript) Sridhar_Rao Tutorials 4 22-03-2007 07:14 PM
How to access third party cookies using ASP/JSP? mahendraraut QnA (read only) 1 13-03-2007 10:17 AM
Cookies ! ! ! ax3 QnA (read only) 1 21-11-2005 12:22 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:02 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2