SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    question on header() function

    Hello

    I have a question regarding the header(location:http:// etc) function. I have a php document that looks similar to the followiing


    <? include("./top.php"); //this file contains loads of html and is resident on all pages in the site IT HAS TO BE HERE

    //now some code specific to this page only
    if blah blah {

    do some stuff

    } else {

    header("location : http://some_page.php");


    }

    include ("./bottom.php"); //this file contains loads of html and is resident on all pages in the site IT HAS TO BE HERE


    -----

    my question is this, how can i redirect to a new page using the header() function in the middle of a file like this ie when i have already passed a load of html and other stuff to the browser. When I run the above i get the following error

    Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/include/top-left.inc.php:129) in /var/www/html/comments.php on line 19

    I am reasonably new to PHP and after investigation found out that to use the header() redirect there can not be any html or white space sent to the browser before the actual header() command

    Im sure there is a really simple solution to this but im a bit stumped and would appreciate some help

  2. #2
    SitePoint Addict Fire59's Avatar
    Join Date
    Aug 2003
    Location
    mn
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's because it's outputting the header and bottom.php stuff.

    so it should be
    Code:
    if(blah) {
    include(header);
    do stuff
    include(footer);
    }else{
    header("location: somewhere.php");
    }

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i understand why im getting the error, but the problem is i need to have the header and footer on the page regardless so i need to find a way of redirecting the page somehow after having passed some html to it

  4. #4
    SitePoint Addict Fire59's Avatar
    Join Date
    Aug 2003
    Location
    mn
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about on the somwhere.php page include and header and footer in the appropriate spot?

    I'm not 100% sure on this but i dont think you can ouput stuff then redirect.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think youre right, ill just have to find a way around it, thanks anyway

  6. #6
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can send all your output to a buffer, so if mid-process the script decides to forward to another page, you can still use the header() functions.

    Look at the ob_start() family of functions.

  7. #7
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use Javascript to redirect.

  8. #8
    SitePoint Addict Fire59's Avatar
    Join Date
    Aug 2003
    Location
    mn
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but i think hcnoodle wants to add header and footer to that redirect page. So I still think that having the include header/footer in that redirect page is probably your best bet.


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
  •