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 13 of 13
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cookies, aren't working

    First I delete and remake the cookie, cause it is a form submission

    if (isset($_POST['passer']) && $_POST['var'] != null){
    $string = $_POST['var'];
    $overtime = time()-3600;
    $time = $expire=time()+60*60*24*30;
    setcookie("resign","", $overtime);
    setcookie("resign",$string,$time );
    }

    if (isset($_COOKIE["resign"])){
    print_r($_COOKIE);
    }

    <form method="POST">
    <input type="text" name="var" autofocus>
    <input type="submit" name="passer">
    </form>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,480
    Thanks
    0
    Thanked 635 Times in 625 Posts
    Cookies are not accessible after they are created until after you reload the page.
    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.

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts
    awwww so I need to do a refresh

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Yes you need to refresh the page.

    When you request a page, your browser sends lines of text called request headers (you can't see these, it's done transparently). The webserver then grabs the page (using php in the process) and then generates reply headers. Your cookies are set in the reply headers. After the reply headers comes the html.

    What this means is that your cookies are only set on the client computer AFTER your page has been sent to their browser.

    By default a browser will return cookies to a site if they came from that domain. That means when you make another page request, the browser will automatically send the cookies and their values back.

    You can see more about this in my signature link below about headers already sent.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Still not working, I did a print_r($_cookie); and it doesn't exist, any other ideas?

    Array ( [PHPSESSID] => 714ac07fbcd1c12e237e50b79165010c) is all there is

    Do I need to set a session_start();

    ?
    Last edited by dawg; 07-09-2013 at 12:30 AM.

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it seems the problem is a headers problem, I cannot set the cookie because headers have already been sent

    Figured it out, had one sspace before the ?php, dumbest thing ever
    Last edited by dawg; 07-09-2013 at 04:10 AM.

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Use a javascript function to set the cookie, after the cookie is set use javascript to submit the form, with a little setTimeout to make sure it gets done before the page reloads. Works for me.
    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
    <?php
    if(isset($_GET['mytext']))
    {
    echo 
    $_COOKIE['mycookie'];
    }
    ?>
    <script type="text/javascript" src="http://www.myphotowizard.net/Files/miniAPI.js"></script>
    </head>
    <body>
    <form id="myform" name="myform" action="./cookie.php" method="get">
    <input type="text" id="mytext" name="mytext" />
    </form>
    <button onclick="setCookie();">Set Cookie</button>
    <script type="text/javascript" >
     function setCookie()
        {
        a="mycookie";
        var b=byId("mytext").value;
        c=1;
        var exdate=new Date()
        exdate.setDate(exdate.getDate()+c)
        document.cookie=a+ "=" +escape(b)+
        ((c==null) ? "" : ";expires="+exdate.toGMTString())
        setTimeout( 'document.forms[0].submit()',100 );
        } 
    </script>
    </body>
    </html>
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by DrDOS View Post
    Use a javascript function to set the cookie
    You're making another assumption that everyone has javascript enabled..
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by dawg View Post
    Still not working, I did a print_r($_cookie); and it doesn't exist, any other ideas?
    Thats because it's $_COOKIE not $_cookie. The super global arrays are all UPPERCASE.

    Quote Originally Posted by dawg View Post
    Array ( [PHPSESSID] => 714ac07fbcd1c12e237e50b79165010c) is all there is
    Thats a php session cookie being sent back by your browser. This was automatically set by php at some point when calling session_start()

    Quote Originally Posted by dawg View Post
    Do I need to set a session_start();
    If you're using sessions, yes.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #10
    New Coder
    Join Date
    Nov 2011
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Solved

    had a white space in front of the <?Php starting tag, which was making headers already sent.

    You need to set cookies before you start any html, so white space destroyed it.

  • #11
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Quote Originally Posted by tangoforce View Post
    You're making another assumption that everyone has javascript enabled..
    Not really. A lot of sites need JS to work. Someone has no business being on them without JS. What percentage of people go without JS? You can't go to many sites with Windows 95 and IE 5.0.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,093
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by DrDOS View Post
    Someone has no business being on them without JS. What percentage of people go without JS?
    No business? Sheesh, if you have that attitude as a webmaster then good luck!

    As for the second part, it's quite common for companies to disable JS on their browsers to stop their company networks being hit with drive by downloads.

    But hey, they've got no business doing that right?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #13
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Quote Originally Posted by tangoforce View Post
    No business? Sheesh, if you have that attitude as a webmaster then good luck!

    As for the second part, it's quite common for companies to disable JS on their browsers to stop their company networks being hit with drive by downloads.

    But hey, they've got no business doing that right?
    That's not my attitude, it's the people who run the website's attitude. Their site won't work without it, what are you doing on a site that isn't working. And a company can require that someone not mess with the JS settings on their company issued machines. Me, I'm just throwing out another way that the guy can solve his problem, one known to work.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Posting Permissions

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