How to create Cookies by using PHP???

tlife143

New Member
Messages
2
Reaction score
0
Points
0
I want a help on creation of cookies....

How to create a cookies and how to access that cookie information???

Plz help me!!!

Wen i use setcookie(name,value,expiry);

It's giving an error tht "Cannot modify header information - headers already sent by"
 
Last edited:

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
simply put, setcookie() must be used before ANYTHING is output to the page.
for example, the following will not work because there is a newline before the PHP engine is engaged:
PHP:
<?php
setcookie(name,1,time()+3600);
?>
The following will work however, despite it not being the first bit of code (because nothing else was output before it):
PHP:
<?php

if (isset($_GET['name']))
$name = $_GET['name'];
else $name=0;

if (isset($_GET['age']))
$age = $_GET['age'];

$calcage = $age+5;

if ($name)
$output = "$name, in 5 years you will be $calcage years old!";
else
$output = 'please submit name & age';

setcookie(name,1,time()+3600);
echo $output;
?>
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Modifying headers, such as locations and cookies, must be done before any output because the headers are sent as soon as you write to the page, as already said. That is why I find it helpful to put the body of php before everything, and use output control functions to write what has been written to after the body tag, shown below.
PHP:
<?php

function ErrorHandler() {
     $flags = "There has been an error!";
}

set_error_handler("ErrorHandler", E_ALL);

ob_start();

//PHP Code writes body.

$html = ob_get_clean();
ob_end_clean();

?>
<html><head><title>Bla</title></head><body>
<?php

if (isset($flags)) {
     echo "<div style=\"color: red; height: 400px;\">" . $flags . "</div>";
}

echo $html;

?>
</body>
</html>
That way if there is an error anywhere on the page, you could better handle it. You could even redirect a user to an error page. Otherwise, you would have to be worried about where the error occurred.
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
tlife143 said:
and how to access that cookie information?
$_COOKIE["name"] contains the data in a cookie with the name "name". This information is not available when the cookie has just been set. In other words, the page has to be reloaded for $_COOKIE to be updated.
 

drf1229

New Member
Messages
71
Reaction score
1
Points
0
simply put, setcookie() must be used before ANYTHING is output to the page.
for example, the following will not work because there is a newline before the PHP engine is engaged:
PHP:
<?php
setcookie(name,1,time()+3600);
?>
The following will work however, despite it not being the first bit of code (because nothing else was output before it):
PHP:
<?php

if (isset($_GET['name']))
$name = $_GET['name'];
else $name=0;

if (isset($_GET['age']))
$age = $_GET['age'];

$calcage = $age+5;

if ($name)
$output = "$name, in 5 years you will be $calcage years old!";
else
$output = 'please submit name & age';

setcookie(name,1,time()+3600);
echo $output;
?>
What he (or she) said :)
 

daman371

New Member
Messages
130
Reaction score
0
Points
0
Not true. setcookie usually has to be used elsewhere because. You do have to call ob_start(); before everything else on the page.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
Not true. setcookie usually has to be used elsewhere because. You do have to call ob_start(); before everything else on the page.
Completely not true. ob_start() will simply prevent anything being output, and instead saves everything until it is flushed.
In my example I saved a string to a variable and then output that, basically performing the same function.

You can get away with NEVER using ob_start, and still create cookies.
 
Top