Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP and Cookies

Featured Replies

Hi Im learning about PHP and Im teaching myself from a book.

Im on to the section about cookies.

This is the code I copied from the book:

 

 

<?php

$user = $_POST['user'];

$color = $_POST['color'];

$self = $_SERVER['PHP_SELF'];

if (($user != null) and ($color != null))

{

setcookie( "firstname", $user, time()+36000);

setcookie( "fontcolor", $color, time()+36000);

header( "Location:getcookie.php");

exit();

}

?>

<html><head><title>Set Cookie Data</title></head>

<body>

<form action="<?php echo($self);?>" method="post">

Please enter your first name:

<input type="text" name="user"><br><br>

Please type in your favourite font color:

<input type="radio" name="color" value="#FF0000">Red

<input type="radio" name="color" value="#00FF00">Green

<input type="radio" name="color" value="#0000FF">Blue

<br><br><input type="submit" value="Submit">

</form>

</body>

</html>

 

 

 

This is the code for the second part:

<?php

$user = $_COOKIE['firstname'];

$color =$_COOKIE['fontcolor'];

?>

<html><head><title>Get Cookie Data</title>

 

<style type="text/css">

body{color: <?php echo( $color );?>}

</style>

</head>

<body>

<h1>HELLO<?php echo ($user);?>!</h1>

</body>

</html>

 

The first page works but when input the data and press "Submit"

I get this:(Please see attachment for screen shot)post-11185-029806800 1280074213_thumb.jpg

can anyone see where I am going wrong??

Thanking you in advance

ShedHead.

Simple ur sending output before u set the cookie no output can be sent before setting a cookie not even html.

 

EDIT:

 

actually i think ur cookies are fine but ur trying to redirect after u set the cookies, the cookies are considered output so ur redirect is gonna flag that error might also be because ur setting one cookie after the other, so both of those suggestions are prob causing the prob

actually cookies are a part of the HTTP header and if you dont use output buffering the user has been redirected when you send a new Location so you should set cookies first.

 

as the warnings suggest there are errors on each of the 3 Header calls meaning each of them fail, that would suggest that the script starts with a newline prior to the starting PHP tag. (which hasn't been transferred in the example)

 

to explain a HTTP response is split into 2 sections, the HEAD and the BODY the way that you tell the browser that the header has now stopped and the following is the body of the response is by a new line, after that new line the HEAD is closed and you are no longer able to add to the HEAD thus giving you those errors when you try and manipulate the HEAD

 

normally you get issues like this when including some library's due to a newline char at the end php tag, ?>\r\n to solve this PHP allows you to not use the ending php tag so you dont get effected by silly FTP clients (like dreamweaver) adding newlines when they get confused

 

Hope that explains why your getting the error

Thats fine, but how do I adjust the code in order to make it work?

 

Mmm i've tested ur code and it does work but if its not for u what u could do is use ob_start(); at the top of the page then ob_flush() something like this

 


$user = $_POST['user'];
$color = $_POST['color'];
$self = $_SERVER['PHP_SELF'];
if (($user != null) and ($color != null))
{
ob_start();
setcookie( "firstname", $user, time()+36000);
setcookie( "fontcolor", $color, time()+36000);
header( "Location:getcookie.php");
ob_flush();
exit();
}
?>
<html><head><title>Set Cookie Data</title></head>
<body>
<form action="<?php echo($self);?>" method="post">
Please enter your first name:
<input type="text" name="user"><br><br>
Please type in your favourite font color:
<input type="radio" name="color" value="#FF0000">Red
<input type="radio" name="color" value="#00FF00">Green
<input type="radio" name="color" value="#0000FF">Blue
<br><br><input type="submit" value="Submit">
</form>
</body>
</html>



This is the code for the second part:
<?php
$user = $_COOKIE['firstname'];
$color =$_COOKIE['fontcolor'];
?>
<html><head><title>Get Cookie Data</title>

<style type="text/css">
body{color: <?php echo( $color );?>}
</style>
</head>
<body>
<h1>HELLO<?php echo ($user);?>!</h1>
</body>
</html>


hunt for the random newline char at the at the beginning or end of the php tags

 

 

FILE_START>>>>>


<?php


/* or */

?>

FILE_END >>>

 

would need to be

 

FILE_START>>>>><?php


/* or */

?>FILE_END >>>

 

output buffering works to if you start it before the new line char in question... you can see there's a newline actually, just noticed it the first error isn't on the first line :)

  • 2 weeks later...
  • Author

Mmm i've tested ur code and it does work but if its not for u what u could do is use ob_start(); at the top of the page then ob_flush() something like this

 


$user = $_POST['user'];
$color = $_POST['color'];
$self = $_SERVER['PHP_SELF'];
if (($user != null) and ($color != null))
{
ob_start();
setcookie( "firstname", $user, time()+36000);
setcookie( "fontcolor", $color, time()+36000);
header( "Location:getcookie.php");
ob_flush();
exit();
}
?>
<html><head><title>Set Cookie Data</title></head>
<body>
<form action="<?php echo($self);?>" method="post">
Please enter your first name:
<input type="text" name="user"><br><br>
Please type in your favourite font color:
<input type="radio" name="color" value="#FF0000">Red
<input type="radio" name="color" value="#00FF00">Green
<input type="radio" name="color" value="#0000FF">Blue
<br><br><input type="submit" value="Submit">
</form>
</body>
</html>



This is the code for the second part:
<?php
$user = $_COOKIE['firstname'];
$color =$_COOKIE['fontcolor'];
?>
<html><head><title>Get Cookie Data</title>

<style type="text/css">
body{color: <?php echo( $color );?>}
</style>
</head>
<body>
<h1>HELLO<?php echo ($user);?>!</h1>
</body>
</html>


  • Author

Thanks for that webdesigner93.

Ive tried it with the additional code and it works fine.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.