Results 1 to 7 of 7

Thread: PHP Error: Cannot modify header information

  1. #1
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Error: Cannot modify header information

    I am new to PHP.I have a login page having an error "Cannot modify header information" .After successful login,the page will redirect to homepage however myscreen still remain in login page. Thanks...


    login.php
    PHP Code:
    <?php
    include ('dbconn.cfg');
    $connection = @mysql_connect("localhost""root""") or die("Cannot connect to server!");
    if (isset(
    $_POST['submit']))
    {
    //1st if

        
    $tbl_name "member";
        
    $sql "SELECT * FROM $tbl_name WHERE email = \"" $_REQUEST['email'] . "\" ";

        
    //echo $sql;
        
    $result = @mysql_query($sql$connection) or die("Cannot execute query.");
        
    $numrow mysql_num_rows($result);
        if (
    $numrow != 0)
        {
    //2nd if

            
    $sql "SELECT name,telephone,address FROM $tbl_name
                    WHERE email = '" 
    $_REQUEST['email'] . "' AND password = '" .$_REQUEST['password'] . "' ";

            
    $result = @mysql_query($sql$connection) or die("Couldn't execute query.");
            
    $numrow mysql_num_rows($result);
            if (
    $numrow != 0)
            {
    //3rd if

                
    $row_array mysql_fetch_row($result);
                
    $_SESSION['gmembername'] = $row_array[0];
                
    $_SESSION['gmembertel'] = $row_array[1];
                
    $_SESSION['gmemberaddr'] = $row_array[2];
                
    $_SESSION['gmemberid'] = $_REQUEST['email'];

                
    //$relative_url = "index.php";
                
    header("Location: http://localhost/www/index.php?frame=main.htm");
                
    //echo "Welcome $row_array[0] <br>";

            
    }//3rd if
            
    else
            {
                
    $status "Wrong Password!";
            }
        }
    //2nd if
        
    else
        {
            
    $status "Member Id not found!";
        }
    }
    //1st if

    ?>

    <html>
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_validateForm() { //v4.0
      var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
      for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
        if (val) { nm=val.name; if ((val=val.value)!="") {
          if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
            if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
          } else if (test!='R') { num = parseFloat(val);
            if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
            if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
              min=test.substring(8,p); max=test.substring(p+1);
              if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
        } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
      } if (errors) alert('The following error(s) occurred:\n'+errors);
      document.MM_returnValue = (errors == '');
    }
    //-->
    </script>

    <body bgcolor="#6F7A9E"  text="#0000FF" vlink="#FF0000" alink="#FFFF00" >
    <p align="center"> <font size="3" face="Times New Roman, Times, serif"><b>WELCOME 
      TO GOLDEN TIME CINEMA!</b></font></p>
    <p align="center"><b><font size="3" face="Times New Roman, Times, serif">......... 
      THE MOST COMPREHENSIVE ONLINE </font></b></p>
    <p align="center"><b><font size="3" face="Times New Roman, Times, serif">CINEMA 
      TICKET RESERVATION SERVICE.</font></b></p>
    <p>&nbsp;</p>
    <p align="center"><b><font size="3" face="Times New Roman, Times, serif">You are 
      required to login before proceeding, </font></b></p>
    <p align="center"><b><font size="3" face="Times New Roman, Times, serif">if you 
      do not have an account, you can sign up <a href="index.php?frame=signup">HERE</a></font></b></p>
    <? if (isset($status))
    {
        echo 
    "<center><font color=\"red\">$status</font></center>";
    ?>
    <form name="form1" method="post" action="http://localhost/www/index.php?frame=login">
    <table border=0 align=center>
      <tr><td>Email Address: </td>
        <td><input type="text" name="email" value=<? if (isset($status))
    {
        echo 
    $_REQUEST['email'];
    ?>></td></tr>
      <tr><td>Password: </td>
        <td><input type="password" name="password"></td></tr>
      <tr><td colspan="2" align=center><input name="submit" type="submit" onClick="MM_validateForm('email','','R','password','','R');return document.MM_returnValue" value="Submit"></td></tr>
      </table>
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    PHP's header function generates information to include in the header and so it must be called before other information is sent out from your PHP code. This is because PHP assumes as soon as it sees any output via print, echo or of regular HTML that the header information is complete and can be sent.

    Instead of using the PHP header() you can try another method that works in the client-side part using JavaScript

    Code:
    $redirectUrl = "http://localhost/www/index.php?frame=main.htm";
    print "<script type=\"text/javascript\">";
    print "window.location.href = '$redirectUrl'";
    print "</script>";

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's a terrible solution!
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Quote Originally Posted by Twey View Post
    That's a terrible solution!
    That was just an alternative. If a PHP based solution they needed then they have to alter their PHP code according to that.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I suspect it's as simple as whitespace or HTML before the <?php tag.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    PHP's header function generates information to include in the header and so it must be called before other information is sent out from your PHP code. This is because PHP assumes as soon as it sees any output via print, echo or of regular HTML that the header information is complete and can be sent.

    Instead of using the PHP header() you can try another method that works in the client-side part using JavaScript

    Code:
    $redirectUrl = "http://localhost/www/index.php?frame=main.htm";
    print "<script type=\"text/javascript\">";
    print "window.location.href = '$redirectUrl'";
    print "</script>";
    Thanks a lot..this is what i want...

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Don't use that -- it's an ugly solution that uses a hackish Javascript redirect rather than a proper HTTP one. Simply don't output anything before calling header(), and you'll be fine.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •