problem with cookies

Discussion in 'PHP' started by levani, Jan 1, 2010.

  1. #1
    I have a small problem with php cookies, this is the script I use to set cookies on my website visitors computer:

    <?php
    	
        if ( $_POST["cookie"] != '' ) {
    	   
    	   $inTwoMonths = 60 * 60 * 24 * 60 + time();
    	   setcookie('name', date("G:i - m/d/y"), $inTwoMonths);
    	   
    	} ?>
        
    	<?php
    	if(!isset($_COOKIE['name'])) {
    	  echo '<form action="" method="post">
    	<input type="hidden" value="1" name="cookie" id="cookie" />
    	<input type="submit" value="save cookie" />
    	</form>';
    	} else { 
               echo "Welcome";
               }
    ?>
    PHP:
    The real code is rather complicated but it's not important. Here is what I'm trying to do... When user click the "save cookie" button I should echo the "Welcome" text, shouldn't it? But it's displayed only after the next page load! I mean when the clicks the save cookies button than he should refresh the page again to see the Welcome text. How can I solve this problem?

    I placed the setcookie function above the code where the cookie is checked but it doesn't work.

    Any ideas?

    Thanks in advance
     
    levani, Jan 1, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Works fine
    
    <?php
    
    if(!isset($_POST["cookie"])) 
    {
     $inTwoMonths = 60 * 60 * 24 * 60 + time();
     setcookie('name', date("G:i - m/d/y"), $inTwoMonths);
    } 
    
    if(!isset($_COOKIE['name'])) 
    {
    print <<<HERE
    <form action="" method="post">
    <input type="hidden" value="1" name="cookie" id="cookie" />
    <input type="submit" value="save cookie" />
    </form>
    HERE;
    } 
    else 
    { 
     echo "Welcome";
    }
    ?>
    
    PHP:
     
    Kaimi, Jan 1, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    A quick work around until you get something better :)

    
    <?php
       
        if ( $_POST["cookie"] != '' ) {
           
           $inTwoMonths = 60 * 60 * 24 * 60 + time();
           setcookie('name', date("G:i - m/d/y"), $inTwoMonths);
           
        } ?>
       
        <?php
        if(!isset($_COOKIE['name'])) {
          echo '<form action="" method="post">
        <input type="hidden" value="1" name="cookie" id="cookie" />
        <input type="submit" value="save cookie" />
        </form>';
        } else {
               error_reporting(OFF);
               header( "Location: /" ) ;
               echo "Welcome";
               }
    ?>
    
    PHP:
     
    MyVodaFone, Jan 1, 2010 IP
  4. levani

    levani Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    None of the codes work for me :(
     
    levani, Jan 1, 2010 IP
  5. CoreyPeerFly

    CoreyPeerFly Notable Member Affiliate Manager

    Messages:
    394
    Likes Received:
    24
    Best Answers:
    5
    Trophy Points:
    240
    #5
    What does the code MyVodaFone posted do for you?
     
    CoreyPeerFly, Jan 1, 2010 IP