我在php中见过使用header来重定向到一个页面。
我已经使用了下面代码中的方法和下面代码中给出的a重定向错误。
我需要知道代码出了什么问题。
下面是代码(php):<?php
/**
* Created by PhpStorm.
* User: Priyabrata
* Date: 3/18/14
* Time: 8:58 PM
*/
function connect_to_db($uid, $pass){
$con=mysqli_connect("localhost", "root", "12345", "MyDB");
if (mysqli_connect_errno()){
echo "Failed to establish link!!";
}else{
echo mysqli_connect_error();
}
$result=mysqli_query($con, "SELECT `pwd` from `login_table` where `uid` = "."'".$uid."'");
if (!$result){
echo "Error!!";
exit();
}
while($xx = mysqli_fetch_array($result)){
if ($xx['pwd'] == hash("MD5", $pass)){
header("location:../Html/Login-Existing-Users1_success.htm");
exit();
}else{
echo "Invalid Login Credentials!!";
exit();
}
}
mysqli_close($con);
}
function validate_credentials(){
if ((isset($_POST["username"]))&&(isset($_POST["password"]))){
$uid = $_POST["username"];
$pwd = $_POST["password"];
if (($uid == "")||($pwd == "")){
echo "Please enter User name and password";
}else{
//Check user name and password
connect_to_db($uid, $pwd);
}
}else{
echo "Please enter User name and password";
}
}
validate_credentials();
这是我得到的错误,而不是重定向时,上传到服务器:
Warning: Cannot modify header information - headers already sent by (output started at /home/opticfhb/public_html/helpvssupport.net/login.php:19) in /home/opticfhb/public_html/helpvssupport.net/login.php on line 27
附加信息:这在我的本地机器上工作得很好,在服务器上产生了问题。
在您已经回显任何输出之后,您不能调用header()
。HTTP请求包含报头信息和数据。任何输出都是数据的一部分,数据在所有报头都已经发送之后才会出现。
PHP有一个叫做headers_sent的函数来检查消息头是否已经发送。您可以考虑编写一个函数,发出位置标头,如果他们还没有,或者回显一些简单的重定向HTML,如果他们有-也许通过在<head>
:
<meta http-equiv="refresh" content="0;http://www.website.com/redirect/url" />