cookies set in php, read in javascript

P

Paul.Lee.1971

Hi,
I'm a beginner with javascript and am a bit confused about how to
access cookies.
I have set a cookie in php using setcookie("allow", "allow", time()
+31536000)
and I can see this in my cookie list in Firefox (tools->options), and
the Name, content, expiry date etc. are all OK.

But when I try to access this cookie using javascript I get nothing. I
got some code from the internet and it looks OK to me; I am putting
this in the head section of my webpage.

<script type="text/javascript">
<!--

var x = readCookie('allow');
if (x)
{
window.location = "http://www.paullee.com/placeholder.html"
}


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

//-->
</script>

Basically, I want any people who satisfy the "allow" cookie criteria
to be forwarded
to another webpage. I really can't see what is wrong with the code.

Many thanks

Paul
 
E

Evertjan.

Hi,
I'm a beginner with javascript and am a bit confused about how to
access cookies.
I have set a cookie in php using setcookie("allow", "allow", time()
+31536000)
and I can see this in my cookie list in Firefox (tools->options), and
the Name, content, expiry date etc. are all OK.

PHP is not spoken here, we are not interested how cookies are set if not
by Javascript, and if don correctly, that si without spurious whitespace.
But when I try to access this cookie using javascript I get nothing. I
got some code from the internet and it looks OK to me; I am putting
this in the head section of my webpage.

<script type="text/javascript">
<!--

<!-- is purposeless the last 10 or more years, leave it out

var x = readCookie('allow');
if (x)
{
window.location = "http://www.paullee.com/placeholder.html"
}


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


The cleaning of spurious whitespace should not be necessary, leave it out
if (c.indexOf(nameEQ) == 0) return c.substring

All this position counting is not necessary,
split works easier.
(nameEQ.length,c.length);
}
return null;

Why return null?

See above.
</script>

Basically, I want any people who satisfy the "allow" cookie criteria
to be forwarded
to another webpage. I really can't see what is wrong with the code.


seems you want to test if there is a cookie called "allow" that holds the
string containing "allow" too

If so, try:

<script type="text/javascript">

if (readCookie('allow') == 'allow') {
window.location.href = "http://www.paullee.com/placeholder.html";
};

function readCookie(name) {
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca.split('=');
if (c[0] == name) return c[1];
};
};

</script>

However, this should work too:

<script type="text/javascript">

var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
if (ca[0] == 'allow=allow')
window.location.href = "http://www.paullee.com/placeholder.html";
};

</script>

or even:

<script type="text/javascript">

if (/(^|;)allow=allow(;|$)/.test(document.cookie))
window.location.href = "http://www.paullee.com/placeholder.html";

</script>

Sorry not tested!
 
R

Richard Cornford

Hi,
I'm a beginner with javascript and am a bit confused about
how to access cookies.
I have set a cookie in php using setcookie("allow", "allow",
time() +31536000)
and I can see this in my cookie list in Firefox (tools->options),
and the Name, content, expiry date etc. are all OK.
Basically, I want any people who satisfy the "allow" cookie
criteria to be forwarded
to another webpage. I really can't see what is wrong with
the code.

If your code on the server is in a position to judge that this "allow"
criteria is met, in order for it to set the cookie that it will send
back to the client, why not use PHP to send an HTTP redirect response
instead of the (then unneeded) page content and the cookie? That is
going to be considerably more reliable than trying to get client side
scripts to do the redirecting.

Richard.
 
E

Evertjan.

Richard Cornford wrote on 02 nov 2009 in comp.lang.javascript:
If your code on the server is in a position to judge that this "allow"
criteria is met, in order for it to set the cookie that it will send
back to the client, why not use PHP to send an HTTP redirect response
instead of the (then unneeded) page content and the cookie? That is
going to be considerably more reliable than trying to get client side
scripts to do the redirecting.

Good point.
 
E

Evertjan.

SAM wrote on 02 nov 2009 in comp.lang.javascript:
Why not directly :

if (/allow=allow/.test(document.cookie))

Because that would also accept the cookie:

notyetallow=allowTooMuch

which is clearly not what you would want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top