Results 1 to 9 of 9

Thread: [RESOLVED] Redirection Blues

  1. #1

    Thread Starter
    Addicted Member kzatu's Avatar
    Join Date
    Aug 2003
    Location
    Nevada
    Posts
    148

    Resolved [RESOLVED] Redirection Blues

    Hi,

    I'm new at PHP and I'm having to learn it through the crash course method. I'm having difficulties with redirecting to another page.

    I've read that you have to use the
    PHP Code:
    header('Location:http://website/page'); 
    But everytime I do I get this error message
    Warning: Cannot modfy header information - headers already sent by (output started at c:\Inetpub\wwwroot\test1.php:2) in c:\Inetpub\wwwroot\test1.php on line 4
    I read about this error and it seems that therer are a dozen PHP codes and HTML tags that you cannot have before the header function. This seems awefully restrictive. Is there another better way to redirect?
    Changes are not permanent, but change is. {Neil Peart}

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Redirection Blues

    You cannot have any thing printed out before you call the header function, if you do the header function will throw the warning that you get. Go though your code and make sure that the re-direct is the first thing that is printed out.

    If you want to display a message before you redirect the user then you will have to use a cominable of meta tags and/or javascript.

  3. #3

    Thread Starter
    Addicted Member kzatu's Avatar
    Join Date
    Aug 2003
    Location
    Nevada
    Posts
    148

    Re: Redirection Blues

    Wow, that sounds like a huge undertaking. As my first web development project, I always assumed that my overall structure would be shoddy and that I'd get slammed somewhere. I think it can be done though with some medium overhauling. I'll give it a try.
    Changes are not permanent, but change is. {Neil Peart}

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Redirection Blues

    Quote Originally Posted by kzatu
    Wow, that sounds like a huge undertaking. As my first web development project, I always assumed that my overall structure would be shoddy and that I'd get slammed somewhere. I think it can be done though with some medium overhauling. I'll give it a try.

    Whilst your reworking your code try to keep in mind

    1. Keep your Logic sperate from your display
    2. Avoid using print or echo to display chunks of html, search the forum for better methods
    3. check any values that are being passed via from using isset and check for unwanted HTML characters

  5. #5

    Thread Starter
    Addicted Member kzatu's Avatar
    Join Date
    Aug 2003
    Location
    Nevada
    Posts
    148

    Re: Redirection Blues

    Well I tried a little test before I started changing my structure. The first four lines in my index.php are this:
    PHP Code:
    <?
    session_start();
    require('functions.php');
    dbconnect();
    So I started testing by inserting header('Location:http://website/test.php'); after session_start and before require('functions.php');, and that worked. Next I placed it after require and before dbconnect and the redirect failed.

    Every line of code in the functions.php is inside of a function. So simply requiring functions.php does not execute any code in that script. So next I began moving my header redirect line throughout the functions.php all the way down to the very last PHP closing tag ?>. And the redirect still worked.

    So it seems that requiring (or including) another PHP script, that has no prints, still disables the header redirect.

    Any ideas?
    Changes are not permanent, but change is. {Neil Peart}

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Redirection Blues

    Make shure there are no spaces or newlines after the closing ?> in functions.php. Any characters outside <? and ?> will be outputted.

  7. #7

    Thread Starter
    Addicted Member kzatu's Avatar
    Join Date
    Aug 2003
    Location
    Nevada
    Posts
    148

    Re: Redirection Blues

    Wow. Thanks for the lightning fast response. Also you were correct. I had a CRLF after the ?>. This is going to make me paranoid now.
    Changes are not permanent, but change is. {Neil Peart}

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Redirection Blues

    Heheh

    After a while you get used to just putting in <?php and ?> when you make a new backend file and working between those.

  9. #9
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Redirection Blues

    In your include file make sure you dont have any white space before the opening <? tag, it can be simple a space or a newline, but check through your include to make sure. 9/10 times its something simple.

    Read this post it will help you get your head aroudn debugging PHP and just good practice.
    http://www.vbforums.com/showpost.php...74&postcount=6

Posting Permissions

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



Click Here to Expand Forum to Full Width