【问题标题】:Redirect works offline but not online?重定向可以离线工作但不能在线?
【发布时间】:2013-08-01 23:39:48
【问题描述】:

在我的离线开发服务器上,我有以下代码:

<?php //submit_build.php

include_once 'header.php';
require_once 'login_users.php';
include_once 'functions.php';

if(empty($_SESSION['username']))
die ("You must be logged in to use this page");

$choice = $_GET['choice'];

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

mysql_select_db($db_database)
    or die("Unable to select database: " . mysql_error());


if (isset($_POST['buildname']) &&
    isset($_POST['weapon']) &&
    isset($_POST['mod1']) &&
    isset($_POST['description']) &&
    isset($_POST['category']) &&
    isset($_POST['hidden']) &&
    isset($_POST['password']))
{
$buildname = clean(sanitizeString($_POST['buildname']));
$buildurl = urlencode($buildname);
$section = $choice;
$weapon =   sanitizeString($_POST['weapon']);
$modcap = sanitizeString($_POST['modcap']);
$mod1 =     sanitizeString($_POST['mod1']);
$mod2 =     sanitizeString($_POST['mod2']);
$mod3 =     sanitizeString($_POST['mod3']);
$mod4 =     sanitizeString($_POST['mod4']);
$mod5 =     sanitizeString($_POST['mod5']);
$mod6 =     sanitizeString($_POST['mod6']);
$mod7 =     sanitizeString($_POST['mod7']);
$mod8 =     sanitizeString($_POST['mod8']);
$polarity1 =    sanitizeString($_POST['polarity1']);
$polarity2 =    sanitizeString($_POST['polarity2']);
$polarity3 =    sanitizeString($_POST['polarity3']);
$polarity4 =    sanitizeString($_POST['polarity4']);
$polarity5 =    sanitizeString($_POST['polarity5']);
$polarity6 =    sanitizeString($_POST['polarity6']);
$polarity7 =    sanitizeString($_POST['polarity7']);
$polarity8 =    sanitizeString($_POST['polarity8']);
$description =  sanitizeString($_POST['description']);
$category =     sanitizeString($_POST['category']);
$hidden =       sanitizeString($_POST['hidden']);
$pw_check =     sanitizeString($_POST['password']);

$pw_check = md5($pw_check);
if ($pw_check == ($_SESSION['password']))
{
$add_build = "INSERT INTO weapons VALUES(NULL,'$username', '$buildname', '$section', '$weapon', '$modcap', '$mod1', '$mod2', '$mod3', '$mod4', '$mod5', '$mod6', '$mod7', '$mod8', '$polarity1', '$polarity2', '$polarity3', '$polarity4', '$polarity5', '$polarity6', '$polarity7', '$polarity8', '$category', '$hidden', '$description', NULL, '{$_SESSION['ipaddress']}', '$buildurl')";
mysql_query($add_build);
header("Location: account.php");
}
else{
die("Incorrect password.");


}
}

随后是更多的 PHP 和 HTML 稍后在文档中。

注意文件 header.php 包含 HTML。

我的代码可以在离线状态下完美运行。我可以点击提交,我会被重定向到account.php。

但是,只要我将文件上传到远程服务器,代码仍然可以正常工作,但重定向却不行。而不是重定向,它只是让我回到同一页面。然而,输入的数据确实会提交给 MySQL,所以只是重定向不起作用。

谁能告诉我如何得到这个

header(位置:account.php);

上班?我应该把它放在哪里?或者我应该搬到哪里去

include_once 'header.php';

让它工作?

非常感谢!

编辑:

这是我的 authenticate.php 文件.. 直到 html 开始的位置。也许你可以在这里看到一个问题。

<?php // authenticate.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 (!empty($_POST['username']) &&
    (!empty($_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_start();
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $pw_temp;
            $_SESSION['ipaddress'] = $_SERVER['REMOTE_ADDR'];
            header('Location: index.php');
            exit;
     }      
}else{
include_once 'header.php';


echo <<<_END

<html xmlns="http://www.w3.org/1999/xhtml">

【问题讨论】:

  • 据我记得,如果数据已经发送,header(location:) 将不起作用。 read the manual 总是好的。
  • 附带说明,这不会改变您的问题,但 mysql_* 已被弃用。出于安全和性能原因,强烈建议您尽快切换:Why shouldn't I use mysql_* functions in PHP?
  • 您总是可以将 header.php 移动到页面上其他 html 之前,至少在调用 header() 之后。
  • 我尝试在我的 HTML 之前移动它,但如果我这样做,出于某种原因,它不会保存我的任何会话数据。例如,该页面要求您输入密码,并从 $_SESSION['password'] 和 $_SERVER['REMOTE_ADDR'] 向您显示您的 IP 地址 .... 如果我移动我的头文件,这些将变为空。奇怪的是我的头文件不包含任何这些信息。
  • Headers already sent by PHP 的可能副本。无论是否显示 - 您无法在服务器上重定向的原因是由于这个非常常见的错误。

标签: php


【解决方案1】:

header() 之后exit; 是个好主意,否则您的代码将在当前脚本中继续。这可能意味着它将再次发送自己。所以你很可能正在运行account.php,但是这个页面随后被发送并覆盖account.php,使它看起来像account.php没有运行。

【讨论】:

  • 我试过了——而不是刷新页面,它只是给出了一个空白的白色页面,顶部是我的标题:(
  • @SteelyDan 您是否还阅读了Daedalus 评论。如果您发送了任何 html,则标头将不起作用。
  • 确实,我已经将我的标题移到了我的 PHP 的最后,就在我的 HTML 开始之前,它导致了一个错误,我的会话数据没有被保存。
  • @SteelyDan 你确定没有空格吗?实际上,如果它在测试服务器上工作,那么这可能不相关。
猜你喜欢
  • 1970-01-01
  • 2012-04-26
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2012-01-14
  • 2013-10-18
相关资源
最近更新 更多