HTTP重定向/标头..什么时候叫它


HTTP Redirect / Header... When to call it

在我的网站上,在每个php页面的顶部,我都有一个

include_one'header.php';

此文件包含HTML。

在我的文件"authenticate.php"中,我希望在登录回索引后进行重定向。

我的代码如下:页眉('位置:http://www.URLHERE.com/index.php');

但是,提交后,页面会刷新。它不会重定向。重定向在我的localhost dev服务器上正常工作,但我一上传到网上,它就停止了工作。

这是因为我的头包含HTML,它在header()函数之前被调用吗?请注意,"header.php"文件中的所有HTML都在HEREDOC标记中。

这是我的代码:

<?php // login.php
include_once 'header.php';
include_once 'functions.php';
require_once 'login_users.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to database:" . mysql_error());
mysql_select_db($db_database)
    or die("Unable to find database:" . mysql_error());
if (isset($_POST['username']) &&
    isset($_POST['pw_temp']))
{
    $username = sanitizeString($_POST['username']);
    $pw_temp = sanitizeString($_POST['pw_temp']);
    $pw_temp = md5($pw_temp);
    $query = "SELECT username,password FROM users WHERE username='$username' AND password='$pw_temp'";
    if (mysql_num_rows(mysql_query($query)) == 0)
    {
    die("Wrong info");
    }
    else
    {
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $pw_temp;
            $_SESSION['ipaddress'] = $_SERVER['REMOTE_ADDR'];
            header('Location: http://www.URLHERE.com/index.php');
        }       
}
...more code down here

在我的网站上,在每个php页面的顶部,我都有一个include_once 'header.php';

这就是你做错的地方。

它必须是

<?php // login.php
include_once 'functions.php';
require_once 'login_users.php';
// some code
include 'output.php'; // ONLY HERE output starts.

在这里,你可以看到一个简洁但完整的例子,其中有一些解释和推理。但是,摆脱header.php并开始使用模板的主要原因正是您提出的问题。

您可以在else中执行include,从顶部文件行中删除include_once 'header.php';,然后这样做:

if (isset($_POST['username']) &&
    isset($_POST['pw_temp']))
{
    $username = sanitizeString($_POST['username']);
    $pw_temp = sanitizeString($_POST['pw_temp']);
    $pw_temp = md5($pw_temp);
    $query = "SELECT username,password FROM users WHERE username='$username' AND password='$pw_temp'";
    if (mysql_num_rows(mysql_query($query)) == 0)
    {
    die("Wrong info");
    }
    else
    {
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $pw_temp;
            $_SESSION['ipaddress'] = $_SERVER['REMOTE_ADDR'];
            header('Location: http://www.URLHERE.com/index.php');
        }       
}
else
{
include_once 'header.php';
//..... now your code!!!!!
}

也许是因为设置了头之后执行的代码?在该行之后添加dieheader不会停止执行!)。