Setting Cookies in PHP

Discussion in 'PHP' started by sin0cide, Dec 7, 2007.

  1. #1
    I have two scripts to test a way of implimenting cookies for form data. For some reason when it trys to make the cookies it says no object what does that mean?

    First page just get data from user and posts to processing page with the id's basic and full

    My question is why doesn't the makeCookie() section of my code work? and if it did would the way I am using if elseif work good for returning data to the page or should it be setup differently. I don't want it to be complex just work

    Processing Page
    
    <head>
    <?php
    
    $basic = $_POST["basic"];
    $full = $_POST["full"];
    
    
    function makeCookie() {
       if (isset($basic)) 
    		setcookie("basic","1",time()+3600);
       if (isset($basic))
    		setcookie("full","1",time()+3600);
    }
    
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?php 
      if (isset($_POST["basic"])) 
         echo "Basic Form from Post <br />";  
      elseif (isset($_COOKIE["basic"]))
         echo "Basic Form from COOKIE <br />";
      else
      	echo "No Basic Form <br />";
    ?>
    <?php
      if (isset($_POST["full"]))
         echo "Full Form from Post <br />";  
      elseif (isset($_COOKIE["full"]))
         echo "Full Form from COOKIE <br />";
      else
      	 echo "No Full Form <br />";
    ?>
    
    <input value="Make Cookies" type="button" onClick="makeCookie();" />
    
    </html>
    Code (markup):
     
    sin0cide, Dec 7, 2007 IP
  2. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The issue here is that you are trying to call makeCookie() as a javascript function but you have written it as a php function.

    Javascript works on the client side, PHP on the server side. View your page source before you click the submit button and you wont even see the function.

    For more information on setting cookies with javascript check out this link.

    http://www.quirksmode.org/js/cookies.html

    Otherwise you will need to submit the form and save the cookie using PHP.

    Hope this helps :)
     
    tonybogs, Dec 7, 2007 IP
  3. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Tonyblogs is right. You can not call PHP functions with JS.
    Also this cookie function may not work because you have already sent the headers to the client.
     
    buldozerceto, Dec 7, 2007 IP
  4. sharry

    sharry Peon

    Messages:
    319
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if you want to call this php function what you need to is, call the function before hand if the $basic or $full is set [note: im assuming here the name of the php file which you are using is index.php] and you need to put the first php block before <html> tag

    
    <head>
    <?php
    
    $basic = $_POST["basic"];
    $full = $_POST["full"];
    
     if (isset($basic) || isset($full)) {
           makeCookie();
    }
       
    
    
    function makeCookie() {
       if (isset($basic)) 
    		setcookie("basic","1",time()+3600);
       if (isset($basic))
    		setcookie("full","1",time()+3600);
    }
    
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <?php 
      if (isset($_POST["basic"])) 
         echo "Basic Form from Post <br />";  
      elseif (isset($_COOKIE["basic"]))
         echo "Basic Form from COOKIE <br />";
      else
      	echo "No Basic Form <br />";
    ?>
    <?php
      if (isset($_POST["full"]))
         echo "Full Form from Post <br />";  
      elseif (isset($_COOKIE["full"]))
         echo "Full Form from COOKIE <br />";
      else
      	 echo "No Full Form <br />";
    ?>
    <form action="index.php" method="post">
    <input value="Make Cookies" type="submit"  />
    </form>
    
    PHP:
     
    sharry, Dec 7, 2007 IP
  5. sin0cide

    sin0cide Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yah I started codeing javascript and php but they are simular code styles so I sometimes put them together... I also read something about the headers too... thanks for all the feedback
     
    sin0cide, Dec 8, 2007 IP