PHP标头不会重定向到home.php

时间:2015-06-19 04:20:55

标签: php twitter-bootstrap

我在自己设计的网站上关注this video tutorial。我遇到了两个主要问题:

第一个是当代码进入header("location: home.php");命令时,脚本会擦除整个屏幕而回显传递到空白屏幕上。表示登录凭据已通过。由于某种原因,header()函数没有重定向到home.php页面。我的网站托管在bluehost.com上。

我的第二个问题是我注释掉的while循环不允许加载页面,这是另一个困扰我的问题。

代码:

    <header id="header">
    <?php 
            include ( "./inc/connect.inc.php" );
            session_start();
            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>
    </header>
    <!-- END HEADER -->

8 个答案:

答案 0 :(得分:3)

这里有多个问题:

  1. 您不应使用mysql_个功能。他们被弃用并且很危险。请改用mysqli_函数。

  2. 您不应在header()功能之前输出任何内容。注释掉echo $pass<header id="header">

  3. 您在此行中遗漏了$$id = row["id"];。它应该是$id = $row["id"];

  4. 您的header功能不正确。该位置必须至少在开头有一个斜杠:header("Location: /home.php");

答案 1 :(得分:2)

您的问题是您正在执行以下操作:

echo("pass");

你也有:

<header id="header">

在重定向用户之前。这会导致“Headers Already Sent”错误。阻止您运行header()来电,因为......您的标题已经发送。

删除该行,它应该有效。

注释

如果您启用了错误报告,您会看到错误。您可以通过将以下内容放在PHP脚本的顶部来打开它:

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

答案 2 :(得分:2)

问题是您首先要将内容输出到客户的浏览器(<header id="header">echo $pass)。

我敢打赌,如果你检查了你的日志,你会在那里找到一条声明“无法发送标题;标题已经发送到......”。一旦完成任何输出,标题也会被传输,您无法添加新标题。

标题名为Location btw,而不是location

来自RFC 2616 - "Hypertext Transfer Protocol -- HTTP/1.1", Section 4.2, "Message Headers"

  

每个标题字段由一个名称后跟一个冒号(“:”)和   场价值。 字段名称不区分大小写

至于你的循环,它是$row['id'],而不是row['id']

答案 3 :(得分:2)

如果您要发送php标头,则无法输出任何内容。此时,您已经输出了

<header id="header">

这意味着当你输出标题时,为时已晚。

答案 4 :(得分:1)

while循环问题是你在行

之前缺少$符号
while($row = mysql_fetch_array($sql))
                    {
                        $id = row["id"];
                    }

将其更改为

$id = ---->$row["id"];

答案 5 :(得分:1)

删除echo("pass");表单if语句。你在循环row["id"];

时错过了$ sign

答案 6 :(得分:1)

Javascript功能怎么样?

if($userCount == 1){
         echo("pass");
         $_SESSION['user_login'] = $user_login;
         header("location: home.php");
         echo "<script>js:function(){ window.location("url to your home.php"); }</script>";
         exit();
 }

答案 7 :(得分:1)

在代码中调用header()函数之前,你不能有任何输出。

参考PHP手册:header function PHP Manual

  

请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。