Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if cookie exists, and if it does - has a value other than ""

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

Can anyone shed any light on why this won't work? If the cookie doesn't exist, it works fine (i.e "null"), but if the cookie exists - but doesn't have a value - it won't work :/

Code:
<script type="text/javascript">
 function Get_Cookie( name ) {
 var start = document.cookie.indexOf( name + "=" );
 var len = start + name.length + 1;
 if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
 { return null;  }
 if ( start == -1 ) return null;
 var end = document.cookie.indexOf( ";", len );
 if ( end == -1 ) end = document.cookie.length;
 return unescape( document.cookie.substring( len, end ) );
 }
 function cookiecheck() {
 var cookietest = Get_Cookie('s');
 if (cookietest == null || cookietest == "") { alert("foo") }
 }
</script>

I have a "developer" plugin in FireFox, which lets me view the cookies - it shows;

Name s
Value
Host mydomain.info
Path /
Secure No
Expires At End Of Session


Any ideas? :/

TIA

Andy
 
>>but if the cookie exists - but doesn't have a value - it won't work :/


what does it say? throws an error???

Known is handfull, Unknown is worldfull
 
>> what does it say? throws an error???

Nope - doesn't give anything :/

TIA!

Andy
 
but when there is no value in the cookie right???

Known is handfull, Unknown is worldfull
 
Exactly. Basically, its a session cookie - and when someone logs out, the cookie simply gets given a blank value (so the "session" no longer exists).

Hope that makes sense!

TIA :)

Andy
 
sorry, i am unable to make any sense. if the cookie is empty it will return back no values. thats how it is working now right? so what exactly is the problem???

Known is handfull, Unknown is worldfull
 
I'd check that the cookie has not blank spaces: you're ssuming a blank cookie cannot be " ".

Cheers,
Dian
 
Hi,

I'm almost certain it isn't a blank space - but interesting idea - thanks for your input :))

The actual HTML from the "Web Developer Toolbar" via Cookies > View Cookie Information is:

<table><tr><th>Name</th><td>s</td></tr><tr><th>Value</th><td class="shaded"></td></tr><tr><th>Host</th><td>domain.info</td></tr><tr><th>Path</th><td class="shaded">/</td></tr><tr><th>Secure</th><td>No</td></tr><tr><th>Expires</th><td class="shaded">At End Of Session</td></tr></table>

Any more suggestions? :/ Maybe check the length of the value - i.e if less than 5 characters, then its not classed as a "valid" session (we use 8 character session ID's, randomly generated - so anything less than that, could in theory be classed as a "dud")

TIA

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top