PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a log in form which posts the information to itself ($_SERVER['PHP_SELF']) and at the top of the page it does the login and sets cookies but in my output the cookie values don't show until I refresh the page? Is it possible to do this?
user_login()
|
|
--------------------------------------------------> if login is sucessful, set cookies
otherwise do nothing
|
|
Login is sucessful, setcookie()
echo cookies <------- blank until I refresh
I assume that cookies must be set before the page even is read (i.e the script) but wasn't sure.
The hack way: when you use setcookie, also set the cookie to the $_COOKIE array.
Better way: create a cookie wrapper class instead of interfacing with $_COOKIE. When a cookie is set (using the wrapper), the change is immediately effected.
I've recearched for about 40 minutes but I'm not any closer. Thanks for the reply but I'd rather not go near javascript just yet :P
Could you enlighten me on my other options, particularly a 'cookie wrapper class'? I've researched for a long time but I don't understand this. If you could provide an exmaple, I'm a very fast learner.
javascript set cookie is actually very simple, and you can echo it with php, but you shouldn't have to. I don't see why you can't set the cookie before the headers are finished being sent.
Because the cookies ARE working (and not throwing the header error), I assume that he has output buffering enabled by default, and whether or not the cookies are being set is not the problem.