简体   繁体   中英

Cannot modify header information error when trying to redirect page

I'm following this video tutorial and have become stumped on the part where the login page redirects the user to home.php from index.php . When the user clicks login the credentials are checked and when the head function is called this error pops up:

"Warning: Cannot modify header information - headers already sent by (output started at /home/urbanas8/public_html/register/index.php:19299) in /home/urbanas8/public_html/register/inc/header.inc.php on line 22"

Line 19299 is just

<?php require_once("./inc/header.inc.php");?>

and line 22 is

header("location: home.php");

here is the header.inc.php file:

<?php 
            session_start();
            ini_set('display_errors', 1);
            error_reporting(E_ALL);
            require_once( "./inc/connect.inc.php" );


            if(isset($_POST['login'])){                 
                $user_login = strip_tags(@$_POST['user_login']);
                $password_login = strip_tags(@$_POST['login_password']);

                $password_login = md5($password_login);

                $sql = mysql_query("SELECT id FROM users WHERE email='$user_login' AND password='$password_login' LIMIT 1");
                $userCount = mysql_num_rows($sql);
                if($userCount == 1){
                    while($row = mysql_fetch_array($sql)){
                        $id = $row["id"];
                    }
                    //echo("pass");
                    //$_SESSION['user_login'] = $user_login;
                    header("location: home.php");
                    exit();
                }else {
                    echo("/invalid username/password");
                    exit();
                }
            }
    ?>
        <div id="top-bar">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <ul id="top-info">
                            <li>Phone: 703-518-4325</li>
                            <li>Email: <a href="mailto:info@urbanare.com">info@urbanare.com</a></li>
                        </ul>
                        <ul class="nav pull-right" id="top-buttons" style="line-height:10px !important;">
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-sign-in"></i>Log in<strong class="caret"></strong></a></a>
                                <ul id="login-dp" class="dropdown-menu" style="padding: 15px;">
                                    <form action="" method="POST" accept-charset="UTF-8">
                                        email: <input id="user_username" style="margin-bottom: 15px;" type="text" name="user_login" size="30" />
                                        password: <input id="user_password" style="margin-bottom: 15px;" type="password" name="login_password" size="30" />
                                        <input id="user_remember_me" style="float: left; margin-right: 10px;" type="checkbox" name="remember" value="1" />
                                        <label class="string optional" for="user_remember_me"> Remember me</label>
                                        <input class="btn btn-primary" style="clear: left; width: 100%; height: 32px; font-size: 13px;" type="submit" name="login" value="Sign In" />
                                    </form>
                                </ul>
                            </li>

                            <li class="divider"></li>

                            <li><a href="../register"><i class="fa fa-pencil-square-o"></i> Register</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div class="sticky-wrapper"><div id="nav-section">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <a href="http://comingsoon.safepropserv.com/landing/index.html" class="nav-logo"><img src="./files/logo_transparent.png" alt="UrbanaRE Logo"></a>

                        <!-- BEGIN SEARCH -->
                        <div id="sb-search" class="sb-search">
                            <form>
                                <input class="sb-search-input" placeholder="Search..." type="text" value="" name="search" id="search">
                                <input class="sb-search-submit" type="submit" value="">
                                <i class="fa fa-search sb-icon-search"></i>
                            </form>
                        </div>
                        <!-- END SEARCH -->

                        <!-- BEGIN MAIN MENU -->
                        <nav class="navbar">
                            <button id="nav-mobile-btn"><i class="fa fa-bars"></i></button>

                            <ul class="nav navbar-nav">
                                <li class="dropdown">
                                    <a href="./files/UrbanaRE -- Property Info.html" data-toggle="dropdown" data-hover="dropdown">Home<b class="caret"></b></a>
                                    <ul class="dropdown-menu">
                                        <li><a href="http://comingsoon.safepropserv.com/landing/index.html">Home Search</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/index-slider.html">Home Slider</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/index-grid.html">Home Grid</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/index-map.html">Home Map</a></li>
                                    </ul>
                                </li>

                                <li class="dropdown">
                                    <a href="./files/UrbanaRE -- Property Info.html" data-toggle="dropdown" data-hover="dropdown">Properties<b class="caret"></b></a>
                                    <ul class="dropdown-menu">
                                        <li><a href="http://comingsoon.safepropserv.com/landing/properties-detail.html">Properties Detail</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/properties-list.html">Properties List</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/properties-grid.html">Properties Grid</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/properties-grid2.html">Properties Grid 2</a></li>
                                    </ul>
                                </li>

                                <li class="dropdown">
                                    <a class="dropdown" href="./files/UrbanaRE -- Property Info.html" data-toggle="dropdown" data-hover="dropdown">Pages<b class="caret"></b></a>
                                    <ul class="dropdown-menu">
                                        <li><a href="http://comingsoon.safepropserv.com/landing/about.html">About Us</a></li>
                                        <li class="dropdown-submenu">
                                            <a href="./filse/UrbanaRE -- Property Info.html">Agency</a>
                                            <ul class="dropdown-menu">
                                                <li><a href="http://comingsoon.safepropserv.com/landing/agency-detail.html">Agency Detail</a></li>
                                                <li><a href="http://comingsoon.safepropserv.com/landing/agency-listing.html">Agency Listing</a></li>
                                            </ul>
                                        </li>
                                        <li class="dropdown-submenu">
                                            <a href="./files/UrbanaRE -- Property Info.html">Agent</a>
                                            <ul class="dropdown-menu">
                                                <li><a href="http://comingsoon.safepropserv.com/landing/agent-detail.html">Agent Detail</a></li>
                                                <li><a href="http://comingsoon.safepropserv.com/landing/agent-listing.html">Agent Listing</a></li>
                                            </ul>
                                        </li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/pricing-tables.html">Pricing Tables</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/login.html">Login</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/register.html">Register</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/faq.html">FAQ</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/404.html">404</a></li>

                                        <li class="divider"></li>
                                        <li><a tabindex="-1" href="./files/UrbanaRE -- Property Info.html"> Separated link </a></li>
                                    </ul>
                                </li>

                                <li class="dropdown">
                                    <a href="./files/UrbanaRE -- Property Info.html" data-toggle="dropdown" data-hover="dropdown">Blog<b class="caret"></b></a>
                                    <ul class="dropdown-menu">
                                        <li><a href="http://comingsoon.safepropserv.com/landing/blog-detail.html">Blog Detail</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/blog-listing1.html">Blog Listing 1</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/blog-listing2.html">Blog Listing 2</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/blog-listing3.html">Blog Listing 3</a></li>
                                        <li><a href="http://comingsoon.safepropserv.com/landing/blog-listing4.html">Blog Listing 4</a></li>
                                    </ul>
                                </li>

                                <li><a href="http://comingsoon.safepropserv.com/landing/contacts.html">Contacts</a></li>
                            </ul>

                        </nav>
                        <!-- END MAIN MENU -->

                    </div>
                </div>
            </div>
        </div></div>

    <!-- END HEADER -->

According to the error message you shared.

The output is started at "index.php:19299" (before including header.inc.php)

if you dont have any space (before ob_start(); at first line of your index.php file.

<?php ob_start();

You should try comment below lines:

  //       ini_set('display_errors', 1);
     //       error_reporting(E_ALL);

Or you should check there should nothing echo in header. because this error mostly occurs when something is echoing before the header redirect.

Removed session_start(); from the header and put it at the top of the index file... 3 hours later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM