Question:
PHP Problem!!! NEED HELP!!!?
Nick Howard
2009-12-27 21:57:46 UTC
I'm getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/37/4830137/html/viewPic.php:5) in /home/content/37/4830137/html/buy3.php on line 14

HERES THE CODE -->



include('connect.php');
if($_POST['coup'])
{
$r = mysql_query("SELECT * FROM COUPONS WHERE CODE='$_POST[coup]'");
while($row = mysql_fetch_array($r))
{
if($row['CODE'])


//1s 1mn 1hr 1day 2weeks
if($row['DATE'] + (100 * 60 * 60 * 24 * 14) < (time()) || $row['DATE'] == 0)
{
setcookie("coup", '', time()+(100*60*60*24));
echo"

CODE ACCEPTED

";
$flag = 1;
$butID = $row['BUTTONID'];
$coup = $row['DISCOUNT'];

}
else
{
echo"

CODE HAS EXPIRED

";
$flag = -1;
$butID = '7917031';
$coup = 1;
}
Five answers:
2009-12-27 22:10:38 UTC
That error is usually associated with sending headers after output has been sent to the browser.



Headers must be sent before such output, meaning that they should be placed way before the document declaration, this often includes other code. But as a rule of thumb, before any output is sent. Setcookie is likely the source of the problem in this case, since its send with the rest of the headers, before any other output. Make sure that you ain't accidentally outputting something. Even something as simple as a space or newline character can be enough to break things, and those are normally invisible to the eye, unless you mark them with your cursor or enable editor specific features to show them.



Note line 14 dose give a good indication as of what may be the exact source, in this case likely setcookie. Always remember to look up the mentioned line.



You also appear to be missing the brackets for the if($row['CODE']) if statement, try to count the number of rows returned. I.e. http://php.net/manual/en/function.mysql-num-rows.php, that may be a better way to check if anything was returned from the DB.



Finally your query could be considered unsafe, the rule of thumb about always quoting array names also holds true when used in queries. I know it doesn't seam obvious, but quotes can be retained by enclosing the array variable in brackets. I.e.



-----StartCode-----

mysql_query("SELECT * FROM COUPONS WHERE CODE='{$_POST['coup']}'");

-----CodeEnd-----

There is no need to use String Concatenation, like another poster suggested. Variables are automatically phrased by PHP when using double quotes.



See also:

http://php.net/manual/en/function.setcookie.php - PHP: setcookie - Manual
just "JR"
2009-12-28 00:46:15 UTC
1. Check that "connect.php" does NOT contain ANY blank line.

2. Syntax of your query is improper. edit: WHERE CODE='$_POST[coup]'"); replace with ='".$_POST['coup']."' limit 1");

=> EXPANDED: = ' " . $_POST [ ' coup ' ] . " ' limit 1" ) ;

=> You need only ONE row (since you only set ONE cookie), so "limit 1"

3. change while($row = mysql_fetch_array($r))

into $row = mysql_fetch_array($r); // You only fetch one row

4. Remove the lines from if($row['code']) to if($row['DATE']



$row = mysql_fetch_array($r);

if ( $row['DATE'] + (100 * 60 * 60 * 24 * 14) < (time()) || $row['DATE'] == 0)

{

setcookie("coup", '', time()+(100*60*60*24));

echo ...

}

Don't you need to set a "value" for the cookie? (secong parameter of setcookie).

Providing not one blank line or space has been sent, the cookie will work once. It can't work twice, since just after, you issue and "echo" (which sends some output).
MyKidsFan
2009-12-27 22:05:46 UTC
You have to do a setcookie before you put out anything. It's not obvious to me from your code if you are outputting anything but make sure you are not. This even means blank lines. It has gotten me when I did something like this:



(blank line)


php code



setcookie(...)

?>



The blank line (obviously without the actual words "blank line') was output and therefore headers were considered already sent and the setcookie didn't work. So make sure you are not putting out anything.
costarakis
2016-11-01 17:19:34 UTC
the image appears like the image shown whilst residing house windows does not comprehend what application created the document or which application is linked with the document form. Are you easily writing Hypertext Preprocessor documents or undeniable HTML documents with a Hypertext Preprocessor extension? Hypertext Preprocessor documents are processed on a Hypertext Preprocessor server earlier being shown interior the browser, so once you're writing them on your guy or woman pc and not using a Hypertext Preprocessor server working then they could't be processed. on the different hand you're attempting to function the documents to a server that doesn't run Hypertext Preprocessor.
Elvin85
2009-12-27 23:11:18 UTC
Just delete this comment line: //1s 1mn 1hr 1day 2weeks . everything wil be ok immeditially.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...