Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.