Simple Cookie

Hi,

How do I create a simple cookie that would contain for example: FullName, Email, Username, Password, Domain, Plan, CheckID ?

The CheckID would be a MD5 codeword that I will hardcode in and will be used later to see if the cookie is authentic.

So, how do I create and write this ? and then later read it back ? also how do I destroy it ?

Thanx
MirageSFAsked:
Who is Participating?
 
Diablo84Commented:
you must use setcookie BEFORE any output (php echo/print, html, new lines outside of <?php tags) otherwise it cant send the cookie data in the http header and you will see that error.
0
 
ZylochCommented:
Hi MirageSF,

To set, use setcookie
http://us2.php.net/setcookie

To get, use
$_COOKIE['cookiename'];

Regards,
Zyloch
0
 
SkonenCommented:
You could use individual cookies like:

setcookie("Fullname", "whoever",  time() + 3600);  // Expire in one hour

And retrieve it with $_COOKIE:

$myvar = $_COOKIE['Fullname'];


Or you can pack the values into one cookie using explode (http://us2.php.net/explode) and implode (http://us2.php.net/implode). Which means less cookies and expire times to keep track of.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Rajkumar_GCommented:
Here is a simple example for Cookies creation, retrieveing data from cookies and deleting cookies.
I think this will be useful for u,U an customise it according to your requirements.

cookie_ex.html
--------------
<html>
<head>
</head>
<body>
<form name="cookie_example" method="post" action="cookie_ex.php">
  Fullname  :<input type="text" name="fullname"><br>
  Email     :<input type="text" name="email"><br>
  Username  :<input type="text" name="username"><br>
  Password  :<input type="text" name="password"><br>
  Domain    :<input type="text" name="domain"><br>
  Plan      :<input type="text" name="plan"><br>
  checkid   :<input type="text" name="checkid"><br>
  <input type="submit" name="Submit">
</form>
</body>
</html>



cookie_ex.php
-------------
<?
      setcookie ("cookie[full]", $_POST['fullname'], time()+604800);
      setcookie ("cookie[email]", $_POST['email'], time()+604800);
      setcookie ("cookie[user]", $_POST['username'], time()+604800);
      setcookie ("cookie[pass]", $_POST['password'], time()+604800);
      setcookie ("cookie[domain]", $_POST['domain'], time()+604800);
      setcookie ("cookie[plan]", $_POST['plan'], time()+604800);
      setcookie ("cookie[checkid]", $_POST['checkid'], time()+604800);

      if(setcookie("cookie","", time()+604800))
         {
           echo "<br>Cookie Deleted<br>";
         }
      else
         {
           echo "<br>Cookie Exists<br>";
         }


      if (isset($_COOKIE['cookie']))
       {
         foreach ($_COOKIE['cookie'] as $name => $value)
           {
             echo "$name : $value <br />\n";
           }
       }

?>


Run this code, so that u can understand it well. Then it will be easy for u to customise it.
0
 
Diablo84Commented:
Using one cookie for all of the information....

(sensible if you are accessing all of the stored information on the same page, if you need to access different data on different pages then it may make sense to break them up into seperate cookies.

For the sake of example i am going to assume you have the values stored in local variables. and you have MD5'd the CheckID (using for example $CheckID = md5($CheckID);

Setting the cookie:

<?php
setcookie("mycookie","$FullName|$Email|$Username|$Password|$Domain|$Plan|$CheckID",time()+31556926);
?>

3 of the setcookie parameters have been used above, the name of your cookie, the data to store in it and a time stamp (here this is the current time plus one year - so it is valid until a year from now).

Reading the cookie:

<?php
$mycookie = $_COOKIE['mycookie'];
$elements = explode("|",$mycookie); Returns an array of the values stored in the cookie
echo $elements[0]; //FullName
echo $elements[1]; //Email
echo $elements[2]; //Username
echo $elements[3]; //Password
echo $elements[4]; //Domain
echo $elements[5]; //Plan
echo $elements[6]; //CheckID
?>

For observation purposes you can view the array contents just using:

<pre>
<?php
$mycookie = $_COOKIE['mycookie'];
$elements = explode("|",$mycookie); Returns an array of the values stored in the cookie
print_r($elements);
?>
</pre>

Deleting the cookie:

<?php
setcookie("mycookie","",time() - 3600); //sets the cookie with an expiry time in the past
?>

You may handle deleting the cookie like this (supposing it was handled on index.php) have a link with a query string...

<a href="index.php?delete">Link</a>

Then in your php:

<?php
if (isset($_GET['delete'])) setcookie("mycookie","",time() - 3600);
?>

Best Wishes

|)iablo
0
 
MirageSFAuthor Commented:
Array ( [0] => [1] => [2] => [3] => [4] => )
Warning: Cannot modify header information - headers already sent by (output started at /home/storm/domains/storm.com/public_html/complete.php:4) in /home/storm/domains/storm.com/public_html/complete.php on line 11


When I go to collect the cookie information after a third party website has been loaded, I get an empty cookie, and the warning messsage ?

Line 11 is setcookie("mycookie","",time()-3600);

0
 
prsupriyaCommented:
Hi!

How about this?

<?
$arraynew=array("FullName"=>setckval("FullName", "FullNameval"),"Email" =>setckval("Email", "Emailval"),
"Username" =>setckval("Username", "Usernameval"),"Password" =>setckval("Password", "Passwordval"),
"Domain" =>setckval("Domain", "Domainval"),"Plan" =>setckval("Plan", "Planval"),
"CheckID" =>setckval("CheckID", "CheckIDval"));
?>
<?
function setckval($ckname,$ckval){
      setcookie($ckname, $ckval);
      return $_COOKIE["$ckname"];
      }
print_r(array_values($arraynew));
?>

Good Luck,
S:
0
 
SkonenCommented:
Mirage, you can use an output buffer to get rid of that warning. Use

ob_start();

Right after your first <?php tag or before any output, then after you set your cookie, put the following line:

ob_end_flush();

So in your case it's probably safe to use ob_end_flush() following line 11.
0
 
Diablo84Commented:
Really just better to just correct the code flow in the script.

Its only line 11 so it is most likely a new line outside of the <?php tag, a 2 second reshuffle of the code flow will fix this rather then turning to output buffering.
0
 
MirageSFAuthor Commented:
// Code below creates the cookie and then loads another site.

<?php
session_start();
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$domain = $_POST['domain'];
$username = $_POST['username'];
$password = $_POST['password'];
echo $fullname;
setcookie("mycookie","$fullname|$email|$username|$password|$domain",time()+31556926);
Header("Location: https://www2.2checkout.com/2co/buyer/purchase?sid=540281&quantity=1&product_id=$pid&demo=Y");
?>

// Code below is loaded after the 2checkout.com site loads it., but fails at line 11 with the error as above.

<?php
$mycookie = $_COOKIE['mycookie'];
$elements = explode("|",$mycookie);
print_r($elements);

echo $elements[0]; //FullName
echo $elements[1]; //Email
echo $elements[2]; //Username
echo $elements[3]; //Password
echo $elements[4]; //Domain
setcookie("mycookie","",time() - 3600);
?>

// btw the array seems to be empty anyway for some reason !  The variables being past are correct.

0
 
ZylochCommented:
In the first case, have this:

<?php
session_start();
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$domain = $_POST['domain'];
$username = $_POST['username'];
$password = $_POST['password'];
setcookie("mycookie","$fullname|$email|$username|$password|$domain",time()+31556926);
Header("Location: https://www2.2checkout.com/2co/buyer/purchase?sid=540281&quantity=1&product_id=$pid&demo=Y");
?>

The echo is pointless because you're redirecting anyways.


For the second, move your setcookie function to between your

$element = explode line and your print_r line.
0
 
Diablo84Commented:
yes quite, you don't need echo $fullname;

As i said above anything in the form of new lines echo/print or html tags will case the same problem as both header and setcookie require data to be sent via the http headers.

In the case of the second example you need to control the code flow

<?php
$mycookie = $_COOKIE['mycookie'];
$elements = explode("|",$mycookie);
print_r($elements);

echo $elements[0]; //FullName
echo $elements[1]; //Email
echo $elements[2]; //Username
echo $elements[3]; //Password
echo $elements[4]; //Domain
?>

You cannot put it prior to the above code as it will delete the cookie before reading the information, at the same time you can put it after because you have not yet read the information from it, so, if you do need to delete it at this point then you can do as Zyloch pointed out which is to read the information from it, delete it then output the information you read. The alternative is putting it in a conditional statement to control its execution, it depends when and why you delete it when you do.

The bottom line is anything that sends data via the headers (header,setcookie etc) cannot go after you have outputted data unless you have turned output buffering on (not needed in this case) so make sure your code flow is correct with this in mind.
0
 
Diablo84Commented:
I am fine with a split but i would like to propose a majority split - to Diablo84 -  as it is my solution that is being used. A partial split to Zyloch for the support pointing out the issue with MirageSF's implementation of the code.
0
 
Diablo84Commented:
A 1 point majority was not quite what i had in mind :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.