Page Redirecting Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • klaydze
    New Member
    • Mar 2007
    • 30

    Page Redirecting Problem

    hi guyz, i have a difficulty in understanding the header function. they said that you must first call the header function before any output on the page will make?is that right?

    because i create a simple login which when the user click the button submit the query for searching of registered user will execute and will redirect to index.php.after i click the submit here is the error that give's me Warning: Cannot modify header information - headers already sent by (output started at C:\kamote\Hello Database2.php:6 ) in C:\kamote\Hello Database2.php on line 65

    HERE IS MY CODE

    [PHP]<HTML>
    <HEAD>
    <TITLE>New Document</TITLE>
    </HEAD>
    <BODY>
    <?
    if (!isset($_POST['submit'])) {
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table border=0 cellspacing=10 align=center width=100%>
    <tr>
    <td align=center style="font-size:50px; background-color:#284775; color:#eeeeee"> Login</td>
    </tr>
    <tr>
    <td>
    <table border=0 width=50% align=center>
    <tr>
    <td colspan=2>&nbsp ;</td>
    </tr>
    <tr>
    <td colspan=2 style="backgrou nd-color:#5d7b9d;c olor:#ffffff;fo nt-weight:bold">Lo gin status:</td>
    </tr>
    <tr>
    <td align="right">U sername:</td>
    <td><input type="text" name="tusername " size="40">
    </tr>
    <tr>
    <td align="right">P assword:</td>
    <td><input type="password" name="tpassword " size="40">
    </tr>
    <tr>
    <td></td>
    <td><a href="#">Forgot your password?</a> <a href="#">Regist er?</a></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Login"></td>
    </td>
    </table>
    </td>
    </tr>
    </table>
    <p align=center style="font-size:9pt">Copyr ight Microkups Co, Inc. © 2007</p>
    </form>
    <?
    }

    else {

    $server = "localhost" ;
    $user = "root";
    $pass = "";
    $db = "mydbase";

    $uname = $_POST['tusername'];
    $upass = $_POST['tpassword'];

    $connection = mysql_connect($ server,$user,$p ass) or die("Unable to connect");
    mysql_select_db ($db);
    $query = "Select * from tblUsers Where username = '$uname' and password = '$upass'";
    $result = mysql_query($qu ery) or die("Error in query");

    if (mysql_num_rows ($result) > 0) {
    echo "you successfully logged as $uname";
    header ('Location: index.php');
    }

    else {
    echo "invalid username or password";
    }

    }
    ?>
    </BODY>
    </HTML>[/PHP]
    Last edited by pbmods; Oct 16 '07, 12:33 AM. Reason: Removed unparsed formatting.
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by klaydze
    hi guyz, i have a difficulty in understanding the header function. they said that you must first call the header function before any output on the page will make?is that right?
    Hi Klaydze,

    Anything that writes to the browser, such as HTML tags, echo, printf, print_r etc MUST go AFTER the header() function. The manual is correct, header() HAS TO BE the first thing output.

    This also includes spaces, see the PHP Manual for headers.

    Cheers
    nathj

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      It would be the simplest way to leave the echo out:
      [php]
      if (mysql_num_rows ($result) > 0) {
      header ('Location: index.php');
      }
      [/php]
      or pass the message to index.php, so you can display it there:
      [php]
      if (mysql_num_rows ($result) > 0) {
      header ('Location: index.php?msg=s uccessful');
      }
      [/php]

      Ronald

      Comment

      • klaydze
        New Member
        • Mar 2007
        • 30

        #4
        Originally posted by ronverdonk
        It would be the simplest way to leave the echo out:
        [php]
        if (mysql_num_rows ($result) > 0) {
        header ('Location: index.php');
        }
        [/php]
        or pass the message to index.php, so you can display it there:
        [php]
        if (mysql_num_rows ($result) > 0) {
        header ('Location: index.php?msg=s uccessful');
        }
        [/php]

        Ronald
        still it gives me this error Warning: Cannot modify header information - headers already sent by (output started at C:\kamote\Hello Database2.php:6 ) in C:\kamote\Hello Database2.php on line 65

        other way to fixed this? thank you

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by klaydze
          still it gives me this error Warning: Cannot modify header information - headers already sent by (output started at C:\kamote\Hello Database2.php:6 ) in C:\kamote\Hello Database2.php on line 65

          other way to fixed this? thank you
          Instead of using like this
          [code=php]<HTML>
          <HEAD>
          <TITLE>New Document</TITLE>
          </HEAD>
          <BODY>
          <?
          php codes
          ?>
          [/code]
          try to put, php opening tags with out any white space on the top of the page
          [code=php]
          <?
          php codes
          ?>
          <HTML>
          <HEAD>
          <TITLE>New Document</TITLE>
          </HEAD>
          <BODY>
          [/code]

          and never use short tags <?......?> instead use of use <?php........ ?>

          Comment

          Working...