+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Problem using php header() function

  1. #1
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Problem using php header() function

    Hi this is day1 of my php education, and I'm running into problems early. I'm trying a simple redirect based on correct entry of a password. I checked a few sources and my code seems ok, but...

    first my html form...

    Code:
    <form action="success.php" method="post">
    Password: <input type="password" name="password"> <input type="submit" />
    </form>
    and here is my success.php

    Code:
    <?php
    
    $pwd = $_POST["password"];
    
    if( $pwd == "123" ) {
    	header( "Location: success.htm" );
    } else {
    	echo "nyeh";
    }
    
    ?>
    when I enter the correct password, I get...

    Warning: Cannot modify header information - headers already sent by (output started at /.../.../success.php: in /.../.../.../success.php on line 13
    Last edited by fguy64; 05-17-2009 at 12:28 AM.

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Problem using php header() function

    The problem may be with success.htm. Your code as posted works fine.

    If success.htm =

    HTML Code:
    <html>
      <body>
      SUCCESS!  
      </body>
    </html>
    ... it will work.

  3. #3
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: Problem using php header() function

    nah, I double checked and tried again, same problem. The only thing I can think of is the first time I executed the script, I had forgotton to upload my success.htm, but it's there now.

    When I checked the php reference for this error, it made reference to some kind of flush() command, but it didn't look like something a newbie had access to, and I didn't try it.

    you can find the error here

    http://ca.php.net/manual/en/function.flush.php

    I wasn't sure if I should try it without a second opinion, maybe it affects the servers?

    edit: I should mention that my php file starts an ends with the usual html tags, but I don't think that should be a problem, here is teh entire file

    Code:
    <html>
    <head>
    <title></title>
    </head>
    
    <body>
    
    <?php
    
    $pwd = $_POST["password"];
    
    if( $pwd == "123" ) {
    	header( "Location: success.htm" );
    } else {
    	echo "nyeh";
    }
    
    ?>
    
    </body>
    </html>
    Edit:
    ok, I took all the html tags out of my php file and it worked. hmm I thought you could embed php within html tags, but maybe I misread. It's getting late...
    Last edited by fguy64; 05-17-2009 at 01:34 AM. Reason: Automerged Doublepost

  4. #4
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Problem using php header() function

    Yes, it does make a difference! You can embed php within html tags, but the header() statement must go before any output, in this case <head><title></title></head>.

  5. #5
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: Problem using php header() function

    Quote Originally Posted by gomarc View Post
    Yes, it does make a difference! You can embed php within html tags, but the header() statement must go before any output, in this case <head><title></title></head>.
    I'm not sure I get what you are saying. the tags you mention constitute output? So I tried as below and the problem returned. I made sure there was no white space before or after my php tags.


    <html>
    <body>
    <?php

    $pwd = $_POST["password"];

    if( $pwd == "123" ) {
    header( "Location: success.htm" );
    } else {
    echo "nyeh";
    }

    ?>
    </body>
    </html>

  6. #6
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: Problem using php header() function

    Quote Originally Posted by fguy64 View Post
    I'm not sure I get what you are saying. the tags you mention constitute output? So I tried as below and the problem returned. I made sure there was no white space before or after my php tags.


    <html>
    <body>
    <?php

    $pwd = $_POST["password"];

    if( $pwd == "123" ) {
    header( "Location: success.htm" );
    } else {
    echo "nyeh";
    }

    ?>
    </body>
    </html>
    As soon as any text (even a blank space) is returned, PHP must send headers that indicate a standard HTML webpage.

    So, in short, to use header, the very first characters in your document MUST be <?php - this particular string isn't sent to the user, and it just sets it up to process data; unless you echo/print anything to the screen (or a function throws an error and echo/prints it), you can use header to send different headers other than a standard webpage


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  7. #7
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: Problem using php header() function

    ok, so php with header() function is like an exception to the rule.

    thanks to both for the help.

  8. #8
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Problem using php header() function

    Since you are just starting, this can help you point out that the best practice in coding is to separate code from layout/content. Have a code file that sends headers, then load content, then display it.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  9. #9
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: Problem using php header() function

    Quote Originally Posted by xav0989 View Post
    Since you are just starting, this can help you point out that the best practice in coding is to separate code from layout/content. Have a code file that sends headers, then load content, then display it.
    yeah, now that you mention it, I do recall reading something like that somewhere. Makes sense and duly noted, thanks.

  10. #10
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Problem using php header() function

    that's all the MVC (Model->content, View->layout, Controller->code) thing that developers are praising
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 04-17-2008, 07:39 PM
  2. Replies: 5
    Last Post: 04-06-2008, 12:47 PM
  3. PHP Problem/Ad Problem
    By jasgor9 in forum Programming Help
    Replies: 1
    Last Post: 04-01-2008, 08:45 PM
  4. Como colocar ads en header de php nuke??
    By pes5champion in forum Soporte
    Replies: 8
    Last Post: 10-10-2006, 11:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers