Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts

    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.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i wonder what would be the point of being able to do that? why do you
    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

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,077
    Thanks
    2
    Thanked 320 Times in 312 Posts
    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.

  • #5
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •