-1

I'm trying to redirect the page after the update the DB but i got error. Please Help.

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\lucent\updatedb.php:1) in C:\xampp\htdocs\lucent\updatedb.php on line 18

Code

<?php
$con = mysql_connect("localhost","root","") ;
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("pdk company", $con);
mysql_query("UPDATE servicetbl SET date_expiry='2012-06-13'
WHERE id =1");
mysql_close($con);
$URL="login_success.php";
header ("Location: $URL");
?>

3 Answers3

2

There's content already sent to the client. Your code doesn't seem to output anything, though, so I think there may be some whitespace at the beginning of your file, or you've accidentally saved your PHP file as UTF8 with BOM (Byte Order Mark). The BOM (3 bytes) will be outputted to the client, forcing the headers to be sent as well. At that point you cannot send other headers anymore.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • +1 for the information about `BOM` btw not an early php programmer understand this complex terms – xkeshav Apr 13 '12 at 06:44
  • http://www.php.net/manual/en/function.header.php#107930 http://www.joelonsoftware.com/articles/Unicode.html – GolezTrol Apr 13 '12 at 07:36
-1

two tips

  • use ob_start() at the top of page just after <?php
  • write exit(); just after header();

    ob_start() : This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • You're fast. I'm still typing. :) Putting `exit()` at the end of the source doesn't seem to make sense. Also, this is just a patch for a problem that shouldn't exist in the first place. – GolezTrol Apr 13 '12 at 06:44
  • that's why i have use the word `TIPS` on the top of answer. this is best practice to use `exit()`. isn't it? – xkeshav Apr 13 '12 at 06:45
  • No it isn't. And even if it is, it is not an answer to the question, so it should be a comment at most. – GolezTrol Apr 13 '12 at 06:46
  • This one can solve the problem. You should call `exit()` because a `header()` won't automatically stop the script from executing. – xkeshav Apr 13 '12 at 06:48
  • You've seen the code in the question? There is no script after the header. And if there were, that wouldn't cause this problem either. – GolezTrol Apr 13 '12 at 06:49
-2

As alternative I can suggest the HTML Meta-Refresh. You can use this within your code: you do not have to make sure that no other code or white spaces are before your php-tag

<?php
    echo '<meta http-equiv="refresh" content="0; url='.$URL.'">';
    exit;
?>
WolvDev
  • 3,182
  • 1
  • 17
  • 32