surya
surya

Reputation: 109

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??

Upvotes: 0

Views: 148

Answers (6)

Prem
Prem

Reputation: 707

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'>

Upvotes: 1

Ravi
Ravi

Reputation: 2086

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()

Upvotes: 0

vusan
vusan

Reputation: 5331

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>
<?
}

Upvotes: 1

user1684833
user1684833

Reputation:

remove echo from first and second line of your code, don't put echo or print_r statement before header('location:.....');

Upvotes: 1

John Conde
John Conde

Reputation: 219824

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.

Upvotes: 1

Liftoff
Liftoff

Reputation: 25392

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.

Upvotes: 1

Related Questions