Warning: Cannot modify header information [error]

Discussion in 'PHP' started by whateveritis, Oct 18, 2010.

  1. #1
    Hi

    I'm doing a PHP for log in function and I got this error

    I have tried every possible ways to fix this problem from Googling the solutions but I still can't fix the problem :(, anyone can help me? I'm using PHP 5

    <?php
    if(!isset($empID) || !isset($empPass))
    {
    header("Location: empLogin.php");
    }
    elseif(empty($empID) || empty($empPass))
    {
    header("Location: empLogin.php");
    }
    else
    {
    $user = assslashes($_POST['empID']);
    $pass = md5($_POST['empPass']);
    										   
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
     die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("emplogin", $con);
    $result=mysql_query("SELECT * from emplogin WHERE empID='$empID' AND empPass='$empPass'", $con);
    $rowCheck = mysql_num-rows($result);
    if($rowCheck > 0)
    {
    while($row = mysql_fetch_array($result))
    {
    session_start();
    session_register('empID');
    echo "Welcome!";
    header("Location: empHome.php");
    }
    }
    else
    {
    echo "Please try again";
    }
    }
    ?>
    PHP:
     
    whateveritis, Oct 18, 2010 IP
  2. Zeh.

    Zeh. Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    echo "Welcome!";
    header("Location: empHome.php");
    PHP:
    You can't echo something and use header() after. header() must be called only if no content was sent to the browser.
     
    Zeh., Oct 18, 2010 IP
  3. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i have removed echo but it still returns the same warning, i wonder if this is because i include the codings within html tag?
     
    whateveritis, Oct 18, 2010 IP
  4. Zeh.

    Zeh. Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Of course. The php code including headers must be in the first line -- no html before.
     
    Zeh., Oct 18, 2010 IP
  5. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    so i tried to put all of the php code on top of the html tag but it returns nothing, it went back to the login page
     
    whateveritis, Oct 18, 2010 IP
  6. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i got it already, gosh i'm so slow lol the header function is used to redirect user into the given page :)
     
    whateveritis, Oct 18, 2010 IP
  7. ksohail

    ksohail Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    you can use ob_start(); at the top of file I hope it will work
     
    ksohail, Oct 19, 2010 IP
  8. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i tried it too but that was before i put the php codes outside html tag, can ob_start() be used if the php codes within the html tag?
     
    whateveritis, Oct 19, 2010 IP
  9. Zeh.

    Zeh. Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No offense, you must code again, and fine.
     
    Zeh., Oct 19, 2010 IP
  10. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Below code is creating problem for tou.

    echo "Welcome!";
    header("Location: empHome.php");
    PHP:
    You can not use header after anything sent to the user(i.e. browser).
    So header must be the last line of the execution. As better practice user exit after header call. :)
     
    Last edited: Oct 20, 2010
    xpertdev, Oct 20, 2010 IP
  11. djarotstudio

    djarotstudio Member

    Messages:
    200
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #11
    Almost the same as all the above,
    1. Header can only be sent once.
    so it's not about the echo, it's not about anything elses. It's about you're sending header call after it actually being sent before, php wont send this second header call. so it warns you.
    in plain english php would probably says sumthin like this :
    So, all you need to figure is, which was your first header call? and which is your next one you've trying to send after. From xpertdev pointers, I assume the 2nd one would be the
    header("Location: empHome.php");
    PHP:
    ..where in this line, I also assume you're trying to redirect a user to empHome.php, when something/some process is done.

    While actually, you've already sent a header call before.

    I'm not quite sure which one is the first which one is the next - you should know which one which, coz you're the one who wrote the piece.

    But here's the next pointer, you can either :
    - rewrite your code to redirect the user using syntax other than header - location.
    - rewrite the first header call into sumthing else, can't suggest you what,as i 'm not sure of which

    As confusing as those above writing - Hope it gives you another point of view. ;)
     
    djarotstudio, Oct 20, 2010 IP
  12. silviuks

    silviuks Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Offtopic question:

    why don't you combine if and elseif?
     
    silviuks, Oct 20, 2010 IP