This is a discussion on beginner cookie think within the PHP Language forums, part of the PHP Programming Forums category; hi all, Trying to get those cookies to work but they wont... Doing this. Have a page login.php wich ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
hi all,
Trying to get those cookies to work but they wont... Doing this. Have a page login.php wich tests the user and pass. If they are correct a cookie is set with one variable like this: setcookie ("cookieUser", $user); then the browser is sent with header("Location: spelersPage.php"); to another page. I there try to read the cookievariable cookieUser on the spelersPage.php like this. <? $test = $_COOKIE["cookieGebruiker"]; echo "|$test"; ?> without any succes. Someone has an idea. The page is im my Thrusted sites list... kind regards Stijn |
|
|||
|
"Stijn Goris" <mepisto@hotmail.com> wrote in message news:3f622bb2$0$24129$ba620e4c@reader1.news.skynet .be... > hi all, > > Trying to get those cookies to work but they wont... > > Doing this. Have a page login.php wich tests the user and pass. If they are > correct a cookie is set with one variable like this: setcookie > ("cookieUser", $user); > then the browser is sent with header("Location: spelersPage.php"); to > another page. > I there try to read the cookievariable cookieUser on the spelersPage.php > like this. > <? > $test = $_COOKIE["cookieGebruiker"]; > echo "|$test"; > ?> > > without any succes. Someone has an idea. The page is im my Thrusted sites > list... > > kind regards > Stijn > > > > Your problem is likely due to a limitation to cookies, not php. A refresh has to have occured before your cookie becomes available to your environment thus if you were to refresh your browser a couple of times you should get your cookie. If not, as a test, I tend to use phpinfo(); alot when doing various tests on form and cookie variables... Try writing the following few lines to a php script file of its own (call it cookieTest.php for our example). <? // For our test, our cookie will be the words "hello world" followed by the eleven digit timestamp generated by time(); $ourCookie="hello world" . time(); setcookie("ourTestCookie", $ourCookie); print("<br>1 = The cookie we just set is $ourCookie<br>"); print("<br>2 = However the cookie that is already baked is: $_COOKIE[ourTestCookie]"); phpinfo(); ?> View this code in a page of its own... refresh the page a few times and examine the values that are output - and while you're at it, examine the output of phpinfo();. I would expect that you should find something like 1 = The cookie we just set is hello world12345678901 2= However the cookie that is already baked is: The next time your refresh your browser, you should find the output something like the following 1 = The cookie we just set is hello world12345678911 2= However the cookie that is already baked is:hello world12345678901 Note that the cookie the first time was sent to the browser but could not be read yet (until you re-freshed the browser). |
|
|||
|
----- Original Message ----- From: "Randell D." <you.can.email.me.at.randelld@yahoo.com> Newsgroups: comp.lang.php Sent: Friday, September 12, 2003 10:42 PM Subject: Re: beginner cookie think > > "Stijn Goris" <mepisto@hotmail.com> wrote in message > news:3f622bb2$0$24129$ba620e4c@reader1.news.skynet .be... > > hi all, > > > > Trying to get those cookies to work but they wont... > > > > Doing this. Have a page login.php wich tests the user and pass. If they > are > > correct a cookie is set with one variable like this: setcookie > > ("cookieUser", $user); > > then the browser is sent with header("Location: spelersPage.php"); to > > another page. > > I there try to read the cookievariable cookieUser on the spelersPage.php > > like this. > > <? > > $test = $_COOKIE["cookieGebruiker"]; > > echo "|$test"; > > ?> > > > > without any succes. Someone has an idea. The page is im my Thrusted sites > > list... > > > > kind regards > > Stijn > > > > > > > > > > Your problem is likely due to a limitation to cookies, not php. > > A refresh has to have occured before your cookie becomes available to your > environment thus if you were to refresh your browser a couple of times you > should get your cookie. If not, as a test, I tend to use phpinfo(); alot > when doing various tests on form and cookie variables... Try writing the > following few lines to a php script file of its own (call it cookieTest.php > for our example). > > <? > // For our test, our cookie will be the words "hello world" followed by the > eleven digit timestamp generated by time(); > $ourCookie="hello world" . time(); > setcookie("ourTestCookie", $ourCookie); > print("<br>1 = The cookie we just set is $ourCookie<br>"); > print("<br>2 = However the cookie that is already baked is: > $_COOKIE[ourTestCookie]"); > phpinfo(); > ?> > > View this code in a page of its own... refresh the page a few times and > examine the values that are output - and while you're at it, examine the > output of phpinfo();. > > I would expect that you should find something like > > 1 = The cookie we just set is hello world12345678901 > 2= However the cookie that is already baked is: > > The next time your refresh your browser, you should find the output > something like the following > > 1 = The cookie we just set is hello world12345678911 > 2= However the cookie that is already baked is:hello world12345678901 > > Note that the cookie the first time was sent to the browser but could not be > read yet (until you re-freshed the browser). > > Hi, Thanks for your reply. Indeed, when I refresh the page where the cookie is defined, the cookievariable is set. What is the best way to overcome this cookielimitation? Do I have to use some sort of javascript refresh while the setcookie page is accessed or is there an other way (pref. in php) to overcome this problem... I want to use the cookie for an automated login (often used with forum systems where 'Log me on automatically each visit' is used ). How do they deal with this limitation? regards Stijn kind regards Stijn |
|
|||
|
(dolphin talk) eeeeeekkkkkk squueeeekkkkk Stijn Goris eeeeeiiiiiiikkkkkkkk
squeeeeekk Fri, 12 Sep 2003 20:02:59 -0500, eeeeeiiiiiiieeeeeekkkk eeeeekkkkkk eeiiikkkk <3f62691c$0$5561$ba620e4c@reader2.news.skynet.be >: > ----- Original Message ----- > From: "Randell D." <you.can.email.me.at.randelld@yahoo.com> Newsgroups: > comp.lang.php > Sent: Friday, September 12, 2003 10:42 PM Subject: Re: beginner cookie > think > >> "Stijn Goris" <mepisto@hotmail.com> wrote in message >> news:3f622bb2$0$24129$ba620e4c@reader1.news.skynet .be... >> > hi all, >> > >> > Trying to get those cookies to work but they wont... >> > [...] >> > >> Your problem is likely due to a limitation to cookies, not php. >> >> A refresh has to have occured before your cookie becomes available to >> your environment >> [...] >> > Hi, > > Thanks for your reply. Indeed, when I refresh the page where the cookie > is defined, the cookievariable is set. What is the best way to overcome > this cookie limitation? [...] Hi Stijn To overcome the need to refresh, set the cookie's value in *both* the HTTP response header and the $_COOKIE array at the same time: $_COOKIE['mycookie'] = 'chocolate chip'; setcookie('mycookie', $_COOKIE['mycookie']); If you synchronize the HTTP response and the $_COOKIE array on the first page that sets the cookie, you won't have to refresh. HTH This message is under the GPL. |