SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    header() function not working

    Hey

    I've got a problem with a header function using Location.
    I tracked it down to, that echoing content before header doesn't work properly.
    Does the order of code causes this problem, or the order of execution?

    I have seen ob_start() suggested
    but I got this understanding that it is bad form.

    Is it or is it not?

    And how to use it properly?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,061
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You can only use the header function when no content has been sent to the browser yet. That includes an enter (or a space, or a tab) at the start of the file, before the PHP open tag!
    And, just throwing ob_start() in your code would work, but it's kind of a quick fix, not really a nice solution.
    If you can't get it to work you can your post here and take it from there.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it is really a hurry then you are free to use ob_start(). But because it seems that you already know that you have thrown some output before the use of header function, I would also recommend to change the coding flow in that way in which you don't output anything before using the header() function.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, Here's the flow of my script:
    Generate product list,
    Add items to cart , generate list of products in cart, with remove link.
    PHP Code:
    <a href=shop.php?cart=remove&amp;index='.$i.'">Remove</a> 
    So I don't see how could I change the flow.

    This is the if statement what uses the header function.
    PHP Code:
    if($_GET['cart']=='remove'){
    $key = (int)$_GET['index'];
    form_display();
    unset(
    $_SESSION['cart'][$key]);
    unset(
    $_GET['cart'], $_GET['index']);
    array_filter($_SESSION['cart']);
    header('Location: shop.php'); } 
    Also what are the reasons not to use ob_start() as a permanent fix?
    It interests me where is this kind of buffer function used?

    Thanks

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm why not you put that piece of code on top of the shop.php file? Isn't that possible?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,061
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Koora View Post
    Also what are the reasons not to use ob_start() as a permanent fix?
    It makes the PHP process take up extra memory as it has to put every output you generate in memory, and only then flush it when the script is done (or when you call one of the ob_end functions). Without ob_start PHP flushes only 4KB at a time, which is then the maximum amount of memory PHP will use at any given time to buffer output. With ob start the maximum is the total output of your script, which might be a lot more than 4KB.

    Quote Originally Posted by Koora View Post
    It interests me where is this kind of buffer function used?
    Well, I hardly ever use it. There are some cases with functions that have no return possibility, like print_r, which you might wrap with buffering like so:

    PHP Code:
    $a = array(1,2,3,4); // some test array
    ob_start();
    print_r($a);
    $result ob_get_clean(); 
    $result now contains the output of the buffer, so instead of actually printing the result of print_r has been saved in the buffer and then requested using ob_get_clean().
    Of course print_r is a silly example, but I couldn't think of a better one
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    There are some cases with functions that have no return possibility, like print_r, which you might wrap with buffering like so:
    print_r CAN return the string - there's a second parameter that was added in 4.3.0 for this exact reason.

    PHP Code:
    $a = array(1,2,3,4); // some test array
    $result print_r($atrue); 
    Of course your reasoning for using ob_start still stands; it just so happens that that particular example is incorrect.

  8. #8
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    before "if" stmt, check whether the session is set or not.
    If session is not set,
    {
    put the "header" function
    }
    if(...)
    {
    ..
    }


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
  •