Jump to content

Cookies crumbling


salawinder

Recommended Posts

Hi all, new to the forum - and begging for enlightenment already!

 

I am working on a page and am trying to use cookies for the first time, but they just are not working. I must be doing something wrong!!

 

the code below is a stripped down version of the page. I am using java to reload the page, as I want to reload as soon as the user makes a selection from the drop-down.

 

<?php
$bld = $_cookie['bld'];
if(!isset($bld)) {
$bld=$_GET['bld'];
setcookie("bld",$bld);
}
?>

<html>
<head>
<title>Title Here</title>

<SCRIPT language="JavaScript">
function rld_bld(form){var val=form.fld_bld.options[form.fld_bld.options.selectedIndex].value; self.location='index.php?bld=' + val ;}
</script>
</head>

<body>

<? echo "The Cookie says your name is :".$_cookie['bld']; ?>
<br>
<? echo "The URL says your name is :".$bld; ?>
<br>

<table border="0" width="660" style="border-collapse: collapse">
<tr>
	<td height="66">
		<form method="POST" name=tbl_bld action=index.php style="margin-bottom:0" >
		<select size="1" style="width: 200px;" name="fld_bld" onchange=rld_bld(this.form)>
		<option value=none>Select your Name...</option>
		<option value=John>John</option>
		<option value=Paul>Paul</option>
		<option value=George>George</option>
		<option value=Ringo>Ringo</option>
		</select></form></p>
	<p></td>
</tr>
<tr>
<td>
</body>
</html>

 

After making a selection the page reloads, but the cookie value is empty, the $bld value is populated, so the code at the top is working (to a degree), but I just can't get it to set the cookie.

 

Any help would be greatly appreciated, I've been banging my head long enough!

Link to comment
Share on other sites

I considered using a session, but thought at one point that I would also be setting cookies with some of the java code. I gave up on that, so I suppose I could look at sessions again.

 

I spent so long trying to figure out how to get this working I'd like to figure it out though, I am certain I will need it at some point, plus I hate to be defeated!

Link to comment
Share on other sites

If there is no cookie set or there is no ?bld= in the URL, nothing will get set, but try this.  Also, this should be in the top of your webroot tree, and the page that's calling needs to be the same domain name.  Note, www.domain.com is not the same as domain.com.

 

<?php
if(!isset($_COOKIE['bld'])) {
$bld=$_GET['bld'];
}
else {
$bld = $_COOKIE['bld'];
}

setcookie ('bld', $bld, false,'/');

?>

Link to comment
Share on other sites

hmm,

 

Looking at the code I think it should work, but now neither the cookie nor the $_GET are returning a value!

 

The java reloads this page with the ?bld= and the name chosen, so on the first load there will be nothing set, but on the 2nd load it should get the variable from the query string and put it into a cookie. It really seems so simple, and I have looked at a load of examples etc. I just can't see why it isn't working.

 

I ahve considered that there is something in the php.ini that might affect it, but didn't get anywhere.

Link to comment
Share on other sites

If you look at the path where your cookies are stored on your PC, do you see it get set there?

 

Thats pretty much the code I use to set my cookies and mine works, so the setcookie should work as long as the domain conditions are right.

Link to comment
Share on other sites

just to throw in a curve ball, as you say 'if the domain conditions are right'.

 

I am using a subdomain, i dont believe that will make a difference, but it might?

 

if it is an issue it would actually be a fairly major stumbling block, but if it is I'll have to look at a way around it.

 

To confirm, I am only using one page (at the moment) and it is on aaa.bbbbbb.com.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.