我无法获取标题(“位置:http://www.google.com/";)工作:(


I cant get header("Location: http://www.google.com/") to work :(

我有一个指向php文件的表单。php文件所做的只是将用户重定向到特定页面。我似乎无法让它发挥作用,也不确定我做错了什么。

<form method="post" action="processRegister.php">
    User Name: <input type="text" name="userName" maxlength="32"><br>
    Password: <input type="password" name="userPassword" maxlength="32"><br>
    <input type="submit">
</form>

processRegister.php

<?php
// processRegister.php
//////////////////////
header("Location: http://www.google.com/");
/*
// First include the class definition
include('UserClass.php');
// Next, create an instance of the class
$newUser = new User;
// Call the registerUser() method, passing in the required variables
if ($newUser->registerUser($userName, $userPassword)) {
  header('Location: www.google.com');
} 
else {
  header('Location: www.yahoo.com');
}
*/
?> 

在使用header()更改位置后使用exit调用。这不仅可以防止不必要的处理,还可以防止潜在的安全漏洞,就好像你在更改位置后没有退出一样,页面会按照正常方式进行处理并发送到浏览器。例如,在将未经授权的用户重定向到登录页面的情况下,敏感内容仍将发送给他们,他们可以捕获这些内容。

header之后使用exit(),并在页面顶部(php)wright ob_start();最重要的是o ob_start!这表示代码从哪里开始。

<?php
ob_start();
// processRegister.php
//////////////////////
header("Location: http://www.google.com/");
/*
// First include the class definition
include('UserClass.php');
// Next, create an instance of the class
$newUser = new User;
// Call the registerUser() method, passing in the required variables
if ($newUser->registerUser($userName, $userPassword)) {
  header('Location: www.google.com');
} 
else {
  header('Location: www.yahoo.com');
}
*/
exit();
?> 

您的源文件是否有良好的编码?如果只是UTF-8,则需要将其更改为UTF-8 without BOM,因为BOM可能会导致问题。

啊,我读你的帖子太快了,没有意识到它在两个不同的页面上。我的解决方案不会解决你的。

不过,请尝试此操作,但在标头后添加一个标志。所以不仅仅是

header("Location: http://www.google.com/");

使其成为

header("Location: http://www.google.com/");
echo "Flag1"; 

如果它甚至没有打印"Flag1",我们就会更好地理解为什么它不起作用。