Jump to content

[SOLVED] PHP Cookies


ToddAtWSU

Recommended Posts

I am trying to set some cookies and display them if they exist but I am having major problems. I don't think I am creating them correctly. Here is what my creation code looks like:

 

<meta http-equiv="refresh" content="5;url=.">

<?php
   if( $_POST["userName"] != "" )
   {
      setcookie( "COCName", $_POST["userName"], time( ) + 60 * 60 * 24 * 30  );
   }
   if( $_POST["userEmail"] != "" )
   {
      setcookie( "COCEmail", $_POST["userEmail"], time( ) + 60 * 60 * 24 * 30  );
   }
?>

<html>

 

Here is what my retrieval code looks like:

 

                     <td><input type="text" name="userName" value="<?php 
                     if( $_POST["userName"] != "" )
                     {
                        echo $_POST["userName"]; 
                     }
                     else if( isset( $_COOKIE["COCName"] ) )
                     {
                        echo $_COOKIE["COCName"];
                     }
                  ?>"></td>
                  <td align="left">
                     <font color="#FF0000" face="Arial">
                        *
                     </font>
                  </td>
               </tr>
               <tr>
                  <td align="right">Your E-mail</td>
                  <td><input type="text" name="userEmail" value="<?php 
                     if( $_POST["userEmail"] != "" )
                     {
                        echo $_POST["userEmail"]; 
                     }
                     else if( isset( $_COOKIE["COCEmail"] ) )
                     {
                        echo $_COOKIE["COCEmail"];
                     }
                  ?>"></td>

What does it appear I am doing incorrectly? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/52163-solved-php-cookies/
Share on other sites

<?php
   if( $_POST["userName"] != "" )
   {
      setcookie( "COCName", $_POST["userName"], time( ) + 60 * 60 * 24 * 30  );
   }
   if( $_POST["userEmail"] != "" )
   {
      setcookie( "COCEmail", $_POST["userEmail"], time( ) + 60 * 60 * 24 * 30  );
   }
?>
<meta http-equiv="refresh" content="5;url=.">
<html>

 

setcookie() need to be on top of the page. The same with Header.

Link to comment
https://forums.phpfreaks.com/topic/52163-solved-php-cookies/#findComment-257271
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.