Jump to content

Recommended Posts

Hi, i have set a cooke using the following code:

cookies.php


<html>
<head>

<title>Forms</title>
</head>
<body>
    
    <?php
        // time(60 seconds in 1min,60 minutes in 1hr,24hrs in a day, 7days a week), it will expire in 1 week from now
        setcookie('test', 45, time()+(60*60*24*7) ); 
    ?>
</body>
</html>

 

but when i view this on my web browser[iE], i get the error message warning message saying

 

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\php_sandbox\cookies.php:8) in C:\wamp\www\php_sandbox\cookies.php on line 10.

 

Would somebody tell me how to over come this please.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/87294-cookie-problem/
Share on other sites

Hi thanks for your quick response. I have removed everything even the html code to be left with:

 

  <?php setcookie('test', 45, time()+(60*60*24*7));?>

 

but i still get the message of

 

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\php_sandbox\cookies.php: 3) in C:\wamp\www\php_sandbox\cookies.php on line 3.

Even with the HTML code, it should work right??

Link to comment
https://forums.phpfreaks.com/topic/87294-cookie-problem/#findComment-446502
Share on other sites

well... the problem is when you use setcookie() it sends a header, so if you are calling it from another script that writes anything else to the screen then it will give that error unless you use output buffering...

 

That script by itself shouldn't give that error, but if you are calling it from another script that you expect output from you will get that error...

 

output buffering can be turned on in the script you call your setcookie from by putting ob_start(); at the beginning of the script and then an ob_end_flush(); at the end.  But there are some disadvantages to this primarily concerning security...

Link to comment
https://forums.phpfreaks.com/topic/87294-cookie-problem/#findComment-446513
Share on other sites

good PIECE OF ADVICES

as MY FRINED SAID PLACE YOUR COOKIE ON FIRST LINE.

AND ABOUT BUFFER

  YOU CAN SET THE BUFFER IN TWO WAYS

 

FROM PHP.INI FILE OR YOU CAN USE THE START_OB();

OR GO TO PHP.NET

FOR MORE BUFFER INFORMATION

IF STILL YOU HAVE ANY PROBLEM THEN SEND ME YOU ALL CODE

OR ONE THING MORE

PLACE YOUR COOKIE IN YOUR HEADER NOT IN YOUR BODY

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/87294-cookie-problem/#findComment-447639
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.