简体   繁体   中英

header location information cannot modify

<?php
 echo $_REQUEST['uname'];
 echo $_REQUEST['pass'];

$res= $mysql_query("select * from `sec` where uname='$_REQUEST['uname']' AND    `pass`='$_REQUEST['pass']'");

if(mysql_num_rows($res)>0)
{
 header('location:home.php')
}
else
{
 header('location:index.php')
}

?>

What's this error: header information cannot modify... even the username and passwords are correct it is not redirecting to another page and giving this warning.. why this error is coming??

You cannot make a PHP header redirection after echoing data to the page.

Take the echoes out, and it should work. Also, put semicolons at the end of those header functions.

You can't send headers after sending any output to the browser. You do that on your first two lines. You can use output buffering to work around this.

从代码的第一和第二行中删除echo ,不要在header('location:.....');之前放置echoprint_r语句header('location:.....');

All the php work should be done before sending any data to the browser. If You are using your own code you should write code in this way

<?php 
include "action/home.php"; //don't echo anything here
include "view/home.php"; 
?>

OR

If you have to redirect after the header information have already been set, you should use javascript function to redirect as

if(mysql_num_rows($res)>0)
{ ?>
  <script type="text/javascript">
  window.location="home.php";
  <script>
<?
}

This error is mostly come when we use many time header location, To avoid this error, you have to use another function instead of header('location:home.php') Here is different function.

<meta http-equiv='Refresh' content='0; url=home.php'>

Same for header('location:index.php') Replaced by <meta http-equiv='Refresh' content='0; url=index.php'> <meta http-equiv='Refresh' content='0; url=index.php'>

You cannot use header() once something is outputted to the browser, you have echoed $_REQUEST['uname']; and $_REQUEST['pass']; , that's the reason why your code broke out.

You have two possible solution: either remove those echoes or use ob_start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM