我看过,我的调整不起作用(菜鸟),但我已经尝试过!
我在安全端口上有一个 cPanel Webmail 的自定义登录,并且希望在自定义登录页面上填充错误消息,而不是重定向到默认端口通用登录页面。
这是我发现的代码,除了我使用 Wordpress 并遇到 header 已发送错误之外,它应该可以完美工作。我尝试包含pluggable.php,因为它与我过去为Wordpress管理区域编写的插件一起使用,但我无法解决公共(public)前端的错误。
Username: <input type="text" name="user" value="" size="20" /><br />
Password: <input type="password" name="pass" value="" size="20" /><br />
<div style="display:none">
<input type="text" name="domain" value="emajenweb.webserversystems.com"/>
<select name="port">
<option selected="selected" value="2096"></option>
</select>
</div>
<?php
if($_POST['domain'] && $_POST['user'] && $_POST['pass'] && !($_GET['failed'] == "1")) {
$port = $_POST['port']; // sets the port number to login to
// Get the protocol to use for this connection
switch($port) {
case '2096': // Secure Webmail
$protocol = 'https://';
break;
}
// Build the URL
$redirectlocation = $protocol.$_POST['domain'].':'.$port.'/login/?user='.$_POST['username'].'&pass='.$_POST['pass'].'&failurl='.$_POST['failurl'];
header ("Location: ".$redirectlocation); // Send URL
} else {
$error = 1;
header ("Location: ".$_POST['failurl']); // Send URL if all neede information is not provided
}
?>
我遇到问题的代码是:
header ("Location: ".$redirectlocation); // Send URL
} else {
$error = 1;
header ("Location: ".$_POST['failurl']); // Send URL if all neede information is not provided
鉴于 header 已发送错误,我希望将 header 位置值更改为 javascript,但我失败了...
我的表单可以正常工作,但无法按照预期在自定义登录页面上传播失败消息。
最佳答案
错误 header 已发送是一个非常常见的错误。这是由于...好吧... header 已发送。当您向服务器发送响应时,会发送 header 。就您而言,它是:
Username: <input type="text" name="user" value="" size="20" /><br />
Password: <input type="password" name="pass" value="" size="20" /><br />
<div style="display:none">
<input type="text" name="domain" value="emajenweb.webserversystems.com"/>
<select name="port">
<option selected="selected" value="2096"></option>
</select>
</div>
那么...如何解决这个问题?很简单。只需在发送响应之前移动您的处理脚本即可:
<?php
if($_POST['domain'] && $_POST['user'] && $_POST['pass'] && !($_GET['failed'] == "1")) {
$port = $_POST['port']; // sets the port number to login to
// Get the protocol to use for this connection
switch($port) {
case '2096': // Secure Webmail
$protocol = 'https://';
break;
}
// Build the URL
$redirectlocation = $protocol.$_POST['domain'].':'.$port.'/login/?user='.$_POST['username'].'&pass='.$_POST['pass'].'&failurl='.$_POST['failurl'];
header ("Location: ".$redirectlocation); // Send URL
} else {
$error = 1;
header ("Location: ".$_POST['failurl']); // Send URL if all neede information is not provided
}
?>
Username: <input type="text" name="user" value="" size="20" /><br />
Password: <input type="password" name="pass" value="" size="20" /><br />
<div style="display:none">
<input type="text" name="domain" value="emajenweb.webserversystems.com"/>
<select name="port">
<option selected="selected" value="2096"></option>
</select>
</div>
您应该引用此 SO 线程以获取更多信息:How to fix "Headers already sent" error in PHP
关于javascript - 在 JS 中使用 PHP 进行 $redirectlocation 和 .$_POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211353/