#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Philippines
    Posts
    71
    Rep Power
    3

    Cannot modify header information


    Hello guys I'm receiving this problem on my code.

    Code:
    Warning: Cannot modify header information - headers already sent by (output started at /portal/index.php:16) in /portal/index.php on line 23
    My code is :

    PHP Code:
    <?php
        
    require_once '../_core/db.inc.php';
        if(
    $user->is_loggedin()!="") {
            
    $user->redirect('home.php');
        }
    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>Portal</title>
        <link href="../_css/portal.css" rel="stylesheet" />
    </head>
    <body>
        <div class="login-container">
            <?php 
                
    if(isset($_POST['emailRequest'])) {

                    
    $emailRequest $_POST['email'];
                    if(
    $user->emailCheck($emailRequest)) {
                        if(
    $user->checkValidation($emailRequest)) {
                            if(
    $user->emailRequest($emailRequest)) {
                                
    header('Location: phase2.php?emailRequest=' $emailRequest);
                            } else {
                                echo 
    '<font color="#F53939">Error</font><br /><br />';
                            }
                        } else {
                            echo 
    '<font color="#F53939">We already sent you the access code. Please check your email.</font><br />Redirecting...<br />';
                            
    header('Location: phase2.php?emailRequest=' $emailRequest);
                        }
                    } else {
                        echo 
    '<font color="#F53939">Wrong email...</font><br /><br />';
                    }
                }
            
    ?>

    Line 23:
    PHP Code:
    header('Location: phase2.php?emailRequest=' $emailRequest); 
    I don't understand why it suddenly came up. On my XAMPP its working but after I uploaded it on server it goes like this.
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,123
    Rep Power
    2010
    This error occurs when output has already been sent. (e.g. html code).
    Looks like you are missing a call too die() or exit() after each header(..).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Location
    Philippines
    Posts
    71
    Rep Power
    3
    I see, I also tried to insert ob_start() at the top and ob_flush() at the end and it works.
    Also, I inserted exit() on every if statement I made for redirecting headers I implemented.
    Thank you!
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,760
    Rep Power
    4536
    ob_start()/flush() is a hack to get sloppy code to work. Make your logic choices before you start any HTML output and you won't have this issue.
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo