Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Errors With Php Setcookie() And Headers! Please Help!, Having header errors when I try to set a cookie with PHP function setc
runeco
post Nov 28 2006, 01:11 AM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 13
Joined: 22-November 06
Member No.: 33,855



Hey guys, I'm having a problem setting cookies with PHP. I am trying to implement a script that will allow someone to log in and access members only pages on my website. One of the aspects is that I have to place a cookie on that persons computer. I know that you can use the PHP function setcookie() to place it on your computer. The problem is that wherever i have setcookie() in my PHP code I get an error:

"Warning: Cannot modify header information - headers already sent by (output started at /home/runeco/public_html/cookie.php:6) in /home/runeco/public_html/cookie.php on line 8"

i looked up some information about it and everyone says that it is problem that I have may have blank spaces after the closing php tag "?>". I througly checked my editor and could not find any blank spaces. So i decided to test something. I opened a new PHP document and just told it to set a cookie, here is the code:

CODE
<HTML>
<HEAD>
<TITLE>Cookie Tester</TITLE>
</HEAD>
<BODY>
<?
$message = 'hello';
setcookie("IM_A_COOKIE", $message, time() + 3600);
?>
</BODY>
</HTML>


I ran this code and I got the exact error i already stated above. This proves that the problem has to do with the setcookie() function. Is anybody else using setcookie()? If so, how do you get it to work, or are you experiencing the same problems? If there is no solution, I'm going to need to find a different way to make a login system for my site users. In that case, does anyone know how to make a login script with PHP that gets information from a MySQL database and supports members only pages. I would really much rather have a solution for my setcookie() problem, as I may need it for other parts of my website. Thanks in advance, and props to anyone that can solve my problem!
Go to the top of the page
 
+Quote Post
electron
post Nov 28 2006, 04:01 AM
Post #2


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



In headers and COOKIE functions you cannot output anything before the header has been sent. Here your HTML tags are outputted before the header. Hence you are getting this error.
If you want to solve it try the following Code:

CODE
<?
$message = 'hello';
setcookie("IM_A_COOKIE", $message, time() + 3600);
?>
<HTML>
<HEAD>
<TITLE>Cookie Tester</TITLE>
</HEAD>
<BODY>

</BODY>
</HTML>


Note the change of the PHP header or Setcookie() function is being called before the output has been started.
This will not gvew any error.

Hope this helps ad have a good day.
Go to the top of the page
 
+Quote Post
farsiscript
post Nov 30 2006, 09:36 AM
Post #3


Super Member
*********

Group: Members
Posts: 357
Joined: 8-April 06
Member No.: 21,487



dear runeco I agree electron when you use Setcookie() function Your all codes must goes at top of page and you can include file form another space , you must compile the file and Setcookie() form this file
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Delphi(1)
  2. Getting List Of Directories And Files Using Php(6)
  3. Converting To Unix Timestamp(1)
  4. Call Cookie Functions In Iframe(2)
  5. How To Use A Link To Call Function In Php?(8)
  6. Include File.php?id=something(13)
  7. Php Header Problem(11)
  8. Genuine Help With Smtp And Php Mail() Function(3)
  9. How Good Is This Data Cleaning Function?(2)
  10. Google China(13)
  11. Mail() Clone(5)
  12. The Best Zip Function(1)
  13. New Found Php Coding Errors(5)
  14. How To Check If Fsockopen Function Is Enabled?(2)
  15. Aef Forum Modifications [resolved](3)
  1. Problem With Where Clause And Max Function(2)
  2. Arrays Outside A Function(3)
  3. Inkscape, A Open Source Vector App That Support Lot Function.(0)
  4. Help: Disable All Buttons Inside A Div Element(8)
  5. User Permission Function [php](3)
  6. Php - Forms, Date And Include(0)
  7. Endif function?(6)
  8. Fopen Errors :((6)
  9. Question Regarding Php Function(12)
  10. Is The Database System Gone Again? [resolved](12)
  11. PHP Function To Add Previous and Next Page Feature(5)
  12. Flash Header Edit?(2)
  13. Disabling Right Click Function(9)


 



- Lo-Fi Version Time is now: 7th October 2008 - 09:24 PM