My darn cookies

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

My darn cookies

Postby Xerpher » Tue Oct 01, 2002 5:58 pm

Well for some reason my cookies never get set... I'm on IE 6.0 ona UNIX server and here's the code I've tried.

Code: Select all

setcookie ("CPOST[username]", $POST_username);

setcookie ("CPOST[username]", $POST_username, time() + 60*60*24*365);

setcookie ("CPOST[username]", $POST_username, time() + 60*60*24*365, '', '');

setcookie ("CPOST['username']", $POST_username);

setcookie ("CPOST['username']", $POST_username, time() + 60*60*24*365);

setcookie ("CPOST['username']", $POST_username, time() + 60*60*24*365, '', '');


Anyone got any ideas?

DoppyNL

Postby DoppyNL » Tue Oct 01, 2002 10:39 pm

If you're setting the cookie with a username you posted from a form you will have to use:

Code: Select all

$_POST['username']


Also, make sure that the browser accepts the cookie!

Greetz Daan

Jay

Postby Jay » Tue Oct 01, 2002 10:44 pm

Another suggestion. Perform the 60*60*24*365 calculation ONCE, and insert that value into the cookie function. It'll save time so PHP doesn't have to keep working out the same thing again and again. The time saved is negligable, but it's also good coding practise and provides you with one place to change the value should you want to.

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Tue Oct 01, 2002 11:37 pm

Well I can't use $_POST because my host disabled it, and I'm only setting one cookie, I just showed all those lines because those are all the different ways I tried to get it to work, but it never did... When I tell the page to print the cookie ( which I think should be $CPOST['username'] ) it returns an empty variable.

DoppyNL

Postby DoppyNL » Wed Oct 02, 2002 12:15 am

I think you cannot use $_POST because you're host doesn't allow the post-method?!?

anyway, setting a cookie works as follows:

Code: Select all

setcookie('cookiename','cookievalue');

and ofcourse you can replace those strings with a variable.
see the manual for a full discription of setcookie.

also, cookies will turn up in $_COOKIE (in PHP4.1.0 and earlier $HTTP_COOKIE_VARS)

Greetz Daan

Jay

Postby Jay » Wed Oct 02, 2002 3:34 am

How did your host manage to disable, and why?

Anyway, you can use the direct reference ($var) or $HTT_POST_VARS['var']! Try those to see what works

DoppyNL

Postby DoppyNL » Wed Oct 02, 2002 4:03 am

Jay wrote:How did your host manage to disable, and why?

You can configure Apache to not accept posted data. PHP can do nothing about that.
Also, My host didn't disable that, possibly Xerpher's host did.
Why they do it? I don't know, I can't think of a good reason.

Greetz Daan

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Oct 02, 2002 6:21 am

Ok, well I've been trying a bunch of stuff and this is the line I'm using to set the cookie:

Code: Select all

setcookie ("CPOST[username]", $POST_username, time() + 60*60*24*365);


Now I know its setting a cookie because my browser asks me if I will allow it. And the variable $POST_username isn't empty, I'm getting it to show itself right after the cookie is set so I know that variable isn't empty. I even tried this code:

Code: Select all

setcookie ("CPOST", $POST_username, time() + 60*60*24*365);


But still, $CPOST is empty... :cry:

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Oct 02, 2002 7:54 am

Something is definitely messed up here. I took the example from php.net

Code: Select all

$value = "WOOHOO";
setcookie ("TestCookie", $value, time()+3600);
echo "->".$TestCookie;
echo "->".$_COOKIE["TestCookie"];
echo "->".$HTTP_POST_VARS['TestCookie'];


and all I got on the page was

Code: Select all

->->


I know for sure the cookie is being set. So either the cookie is being set with an empty value or I just can't retrieve cookies. :(

DoppyNL

Postby DoppyNL » Wed Oct 02, 2002 7:59 am

Cookies that you set on page 1 will not appear on page 1.
they will appear for the first time on page 2!!!!

from php.net:
Common Pitfalls:

Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter.


Greetz Daan

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Oct 02, 2002 8:08 am

Wow, that was weird, I had setup a whole isset thing to watch out for that "pitfall" but I guess I still fell in :oops:

THANK YOU!

Jay

Postby Jay » Wed Oct 02, 2002 8:36 am

DoppyNL wrote:Also, My host didn't disable that, possibly Xerpher's host did.

I was replying to Xerpher ;). If the host won't accept POST data, change the form to $_GET. They MUST accept that if they're using PHP (otherwise why bother having a dynamic setup?).
Also, add the fourth element as '/', so your cookie code looks like setcookie ("TestCookie", $value, time()+3600,'/'); When I was having problems with my site I did that and it worked.

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Oct 02, 2002 8:58 am

ACK! X_X I SPOKE TOO SOON!

Tell me whats wrong with this...

Code: Select all

    function login()
   {
      global $POST_username, $POST_password, $UserInfo, $DB;

      if (isset($POST_username) && empty($cookie))
      {
         $DB->query("SELECT * FROM users WHERE username='".$POST_username."'", "CurrentUser");
         $UserInfo = $DB->fetch_row("CurrentUser");
   
         setcookie ("cookie[username]", "X".$POST_username, time()+3600, '/');
         setcookie ("cookie[password]", "Y".$POST_password, time()+3600, '/');
         
         print "<meta http-equiv=refresh content=\"0;url=index.php?page=two\">";
      }
      elseif (isset($cookie))
      {
         echo "Success";
      }
      else
      {
         $UserInfo['username'] = "Guest";
         $UserInfo['password'] = "NULL";
      }
      if (isset ($cookie)) {
         while (list ($name, $value) = each ($cookie)) {
            echo "$name == $value<br>\n";
         }
      } else { echo "dangit"; }
   }



I get cookie alerts so the first if definitely works, but at the end, I always get "dangit" no matter what. So it seems my cookie is empty.... (Which is weird because I put a letter in front of the variable to make sure it isn't empty :? )

DoppyNL

Postby DoppyNL » Wed Oct 02, 2002 9:09 am

once again:

you are trying to set a cookie and read it in the same script
wich is not possible!!!
the cookie will appear in the next script you call!!!

same pittfall again... read my previous post.

as an extra note:
You login a user by setting a cookie and checking that cookie everytime.
Might be more easy by using sessions and remembering a session-variable. That way users that refuse the cookie can login normally.

Greetz Daan

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Oct 02, 2002 9:37 am

...I'm speachless... I put this outside the function, and now its all working:

Code: Select all

   if (isset ($cookie)) {
      while (list ($name, $value) = each ($cookie)) {
         echo "$name == $value<br>\n";
      }
   } else { echo "dangit"; }


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot], krotbacca and 3 guests