0

When a certain condition is met in my PHP, I want something above it to be executed (a header). I've read all about ob_start but through testing it out many, many (I mean days) times, I honestly don't see any difference. So now I'm wondering whether I can leave the header at the top and activate it later on?

if($x == 1)
    header("Location: something"); exit;

... // echo's, HTML, etc
$x = 1;
restart_php();

Probably not, but worth a try. Maybe someone out there does know a technique.

  • You have to use `exit` after `header('location'.$url);`. – Leri Sep 13 '12 at 13:11
  • added to the question. I've already tested this before –  Sep 13 '12 at 13:12
  • 1
    You can't "restart PHP", but you can create logical structures using loops, functions etc. to let your logic flow in any way you want. Use them. – deceze Sep 13 '12 at 13:14
  • I see. this has been so frustrating, I really don't understand why in the world PHP won't let headers after output is sent –  Sep 13 '12 at 13:16
  • @Adam184 Let's begin with that $x will always be equal to 1. So your script will never stop reloading. to prevent this in your `if clause` you have to change value of your variable. To access it in another php session you need to use sessions. And if you won't add brackets, only first statement after condition will be executed. – Leri Sep 13 '12 at 13:17
  • http://stackoverflow.com/a/8028987/476 – deceze Sep 13 '12 at 13:17
  • @PLB I don't understand what you are saying. I think you are missing a lot of words in each sentence. –  Sep 13 '12 at 13:19
  • 1
    same reason the post office doesn't allow you sending the letter ahead of the envelope. – Dave Sep 13 '12 at 13:22
  • perhaps provide some context on what you are trying to accomplish withe the page itself will result in more specific suggestions. – Dave Sep 13 '12 at 13:23
  • because the letter wouldn't be paid for? LOL. I think I'm going to check out the two answers. –  Sep 13 '12 at 13:25
  • because they don't know what to do with it, that information is in the envelope(header). – Dave Sep 13 '12 at 13:38
  • @Dave ah that makes sense. I guess I just don't get how they can't send it after I do some errands, then put it in the envelope later. –  Sep 13 '12 at 13:43

2 Answers2

1

if you really want to specify the header later (and it be variable) to provide a bailout-redirect of sorts... try this on for size;

<?php
function restart_php($x){
    if($x == 1)  
        ob_end_clean();   # throw everything away
        header("Location: something"); 
        exit;  
    }else{
        $page = ob_get_contents();
        ob_end_clean();
        echo $page;
    }
}
ob_start();  # everything after here goes into buffer so header is clean

... // echo's, HTML, etc  
$x = 1;  # to previous note on this, X is always equal to 1?
restart_php($x);  # which clears the buffer and either shows header or echo's the contents
?> 
Dave
  • 991
  • 1
  • 7
  • 15
  • I will try this out, thanks. Yes, they are bailout redirects needed to be done in PHP. –  Sep 13 '12 at 13:37
0

Don't know, if i understand your question right .... in php (actually) there is no way to use a GO TO (like in basic) ....

Update: since php 5.3.0 a goto function is implemented

what you can do eg. write your condition at the top in a function

<?php
 function shutdown() {
   global $x;
   if($x==1) {
      header("location ...");
   } 
   //.....
 }
 // and then call the register_shutdown_function
 register_shutdown_function('shutdown');
 ?>

More infos about the this function register_shutdown_function()

donald123
  • 5,638
  • 3
  • 26
  • 23
  • 4
    For the information: PHP does have a [goto](http://php.net/manual/en/control-structures.goto.php) since 5.3.0. – TaZ Sep 13 '12 at 13:25
  • "You are also allowed to jump backwards with a goto statement." –  Sep 13 '12 at 13:31
  • @TaZ maybe you should post an answer with the goto? I will accept that :) –  Sep 13 '12 at 13:31
  • @TaZ sorry didn't fetch the changelog of 5.3 .... Thanks for this new Information – donald123 Sep 13 '12 at 13:33
  • 1
    Hmm, well the (XKCD) image at the bottom of that page is quite accurate. So I don't really recommend it. If you have a problem with output messing up your headers you really should look more into `ob_start` even though you haven't had luck with it yet, or restructure your program. – TaZ Sep 13 '12 at 13:33
  • Don't know why they implemented goto... In PHP 2.1 I would say ok, but in PHP 5.3... – Alain Tiemblo Sep 13 '12 at 13:33