Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2007, 07:55 AM   PM User | #1
student
Regular Coder

 
student's Avatar
 
Join Date: Jan 2007
Posts: 129
Thanks: 13
Thanked 0 Times in 0 Posts
student is an unknown quantity at this point
how to overcome the pitfalls of php cookies?

hello,
i need to set a cookie and work with that cookie on the same page.

this seem to be not possible with php cookies:

"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."

how to overcome this?
is this possible using other programming languages such as cgi, perl so that we can set cookies and read them and process them on the same page?

please point me to such tutorials.

thank you very much.
student is offline   Reply With Quote
Old 01-30-2007, 08:12 AM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
i wonder what would be the point of being able to do that? why do you
Quote:
need to set a cookie and work with that cookie on the same page.
?
You just do all your processing first to determine the value that your cookie needs to have, and then you set it.

the cookie-collection is part of the http-header, so if you set a cookie then it first needs to be sent to the browser, and on any consecutive pagerequest for that domain, that cookie will be added to the http-header (until it expires).
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-30-2007, 09:02 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Since the whole point of cookies is to pass info between pages I can't imagine any language implementing a means of accessing the cookie again on the same page unless it was just a side effect of the way that they implemented cookies in the language in which case they will probably fix it in the next version so that it no longer has that side effect.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-30-2007, 04:26 PM   PM User | #4
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,714
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
The value/variable that you used to set the cookie is available anywhere on that page, just use that value.

PHP Code:
if(!isset($_COOKIE['TestCookie'])){
$cookie_value 'something from somewhere';
setcookie("TestCookie"$cookie_valuetime()+3600);

// more code ....

if($cookie_value == 'something from somewhere'){
...
}
// end of !isset cookie... 
If you have a single web page that has logic to both set a cookie and logic that makes use of that cookie, you can "fake" setting the cookie by adding something like the following after the actual setcookie(...) statement -
PHP Code:
$_COOKIE['TestCookie'] = $cookie_value
The key for proper logic when a web page both sets and uses a cookie is that it should have conditional logic that determines what to do when the cookie is set and when it is not set.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 01-30-2007, 05:47 PM   PM User | #5
student
Regular Coder

 
student's Avatar
 
Join Date: Jan 2007
Posts: 129
Thanks: 13
Thanked 0 Times in 0 Posts
student is an unknown quantity at this point
Hello raf, felgall, CFMaBiSmAd
thank you very much for your replies.
i have got the code working.
and implemented what i wanted to.
thank you.
student is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:47 AM.


Advertisement
Log in to turn off these ads.