tony
tony

Reputation: 71

redirect to another page after logging in

I am working on how to redirect to another page by suing header() function, but it's not working, and give me an error message saying hat:Cannot modify header information - headers already sent by...... Anyone could help, thanks a lot. here is my php code

 if(isset($_POST['email'])){
    $email = $_POST['email'];
    $pass = $_POST['pass'];
     $query = "select email, password from member where email = '$email' and   password='$pass'";
     if(mysql_num_rows(queryMysql($query)) == 0)
{
   echo "Email/Password invalid<br/>";
}
else
{
    $query1 ="select uniid from member where email = '$email' and password ='$pass'";
     $result1 = queryMysql($query1);
      $row1 = mysql_fetch_object($result1);
      $uniid = $row1->uniid; 
       $_SESSION['uniid'] = $uniid;

       header("Location:View.php?view=$uniid");
   }
     }

Upvotes: 1

Views: 635

Answers (6)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Consider starting your code with ob_start(). This turns off output buffering, which in short allows you to use header() and setcookie() anywhere at all.

Upvotes: 1

Chris C
Chris C

Reputation: 2013

Make sure that there is no whitespace in your PHP file at all, in particular before the opening PHP tag. This counts as text output and it can't occur before sending headers.

Upvotes: 0

dj_segfault
dj_segfault

Reputation: 12429

The most common cause for this problem is that something somewhere is printing something to stdout before you send your header. You can either use ob_start as Kolink said, or you can turn on a global output buffer in php.ini, which will allow PHP to automagically send the header output first.

Upvotes: 0

IslandCow
IslandCow

Reputation: 3532

A header must precede any output from the program. So, if you echo anything before the header modification, you will get that error. If you don't want to rearrange your code, you can use output buffering to delay other output until the header is sent.

See:
https://www.php.net/manual/en/function.header.php
https://www.php.net/manual/en/function.ob-start.php
https://www.php.net/manual/en/function.ob-end-flush.php

Upvotes: 0

timdev
timdev

Reputation: 62894

You can't have any output (not even whitespace), before your call to header().

You can hunt down any opportunity for output, or you can start output buffering on the first line of your script. That will cause output to be buffered until either the end of the script, or an explicit call to ob_flush() (or similar functions). See ob_start()

Upvotes: 0

Eamorr
Eamorr

Reputation: 10012

THe headers must be sent before you write anything else. Put the header re-direct at the top of your code and it should work.

Upvotes: 3

Related Questions