salawinder Posted January 8, 2008 Share Posted January 8, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/ Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 If you want it to work after you close the browser, you need to set a duration with it. Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-433993 Share on other sites More sharing options...
salawinder Posted January 8, 2008 Author Share Posted January 8, 2008 Actually one of the main requirements is that the cookies only last as lon as the browser is open, but I have messed with putting a time into see if it made a difference but nothing changed. Thanks for the feedback though! Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-433997 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 If that's the case, just use a session instead. Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434003 Share on other sites More sharing options...
salawinder Posted January 8, 2008 Author Share Posted January 8, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434010 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 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,'/'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434016 Share on other sites More sharing options...
salawinder Posted January 8, 2008 Author Share Posted January 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434025 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 I goofed, try again with the new code. Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434028 Share on other sites More sharing options...
salawinder Posted January 8, 2008 Author Share Posted January 8, 2008 Yep, the GET is now working, but the cookie still isn't. (there was a ] missing on line 2 but I put that in, still not getting anything) Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434033 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434037 Share on other sites More sharing options...
salawinder Posted January 8, 2008 Author Share Posted January 8, 2008 Now that's interesting.... there is no cookie from the domain I am using at all, but there are lots of other cookies there, so I know it's not my browser settings. odder and odder. Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434043 Share on other sites More sharing options...
salawinder Posted January 8, 2008 Author Share Posted January 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85092-cookies-crumbling/#findComment-434058 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.