6

Sorry for the dramatic sounding title, just wanted to know if there is a way to prevent all types of PHP commands from executing EXCEPT one.

For example, now when I kill a script using die() my pages look half broken because the bottom part of the page's html failed to load since it was being brought in using the include() function.

So is there a way to tell PHP "don't allow any more commands to be executed except the include function" ?

TJDeatwiler
  • 63
  • 1
  • 1
  • 3
  • 1
    Then maybe `die()` is not right command to use. What is the context? Why do you have to kill the script at all? – Felix Kling Jan 15 '11 at 02:52
  • No, if it's your time, you'll simply have to `die`… Sorry. :) Seriously though, seems you're going about this the wrong way. Just simply don't kill your page while it's still doing something. Use better error handling! – deceze Jan 15 '11 at 02:54
  • @Felix Kling I am killing the script if the GET parameter's value is anything other than 3 predetermine acceptable values for security reasons. – TJDeatwiler Jan 15 '11 at 02:56
  • 1
    Jump off of the top of a building, shoot yourself, drink oil, jump of a plane except without the parachute. – Delta Jan 15 '11 at 02:58
  • You should be doing all your processing before you output anything at all. – Casey Chu Jan 15 '11 at 03:06

6 Answers6

12

You can use return to "terminate" an included file, without killing the whole script:

test1.php

<?php
include 'test2.php';
echo 'foo';

test2.php

<?php
echo 'bar';
return;
echo 'baz';

Outputs:

barfoo
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • Useful way to continue the script. Just handle whatever is returned however you like, thanks. – Andrew Apr 20 '15 at 15:30
2

You're going to need to use a custom die() command, such as my_die() to include the needed files. You cannot override die() nor will any other function allow you to do what you seek.

Spechal
  • 2,634
  • 6
  • 32
  • 48
1

exit() will stop execution, and run any registered exit handlers.

Throwing an exception will raise up to wherever the surrounding try is. If there is no try up to the top level, it will terminate all execution of the script.

return at global scope will return out of the current include file, back to whatever did include/require of the file.

Personally, I recommend a well-thought-out approach to exception handing, and using try/throw.

Jon Watte
  • 6,579
  • 4
  • 53
  • 63
0

Any reason you can't just do this?

if(something didn't work) {
  include('footer.php');
  die();
}

If you're using it frequently, turn it into a function.

function finish_and_die($message) {
  print $message;
  include('footer.php');
  exit;
}
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • no because I have more than 1 include file in various levels on the page and the processing part is in the middle, I don't want to have to worry about order if there is another way. – TJDeatwiler Jan 15 '11 at 02:55
0

You could try something like:

if( is_secure($_GET['value']) ) // Whatever you're using to check if the value is allowed
{
    // Page content goes here
}

That way, all your other includes and page processing code will still get called.

Alternatively, if it's not valid, throw an exception, and have an exception handler that knows how to complete page code.

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
-1

If you use an OOP approach combined with output buffering, this is how you can solve the problem:

The Controller class (where your logic goes) has a member which is an instance of the Template class. The Controller class has a __destruct method, which renders the Template

when exit() or die() are called, the Controller object's __destruct method is called, which in turn calls the Template object's render method.

Stephen
  • 18,597
  • 4
  • 32
  • 33
  • 1
    Business Logic belongs in Controllers. Storage/Data Logic belongs in Models. Display logic belongs in Views. – Stephen Jan 15 '11 at 03:09
  • Interestingly this is how I just built my framework to render templates. :) – Spechal Jan 15 '11 at 03:10
  • 2
    I'm sorry but no. Business logic definitely belongs in models. A controller is a bridge between the business logic (model) and the display (view). Search for it, you'll see. [Here's an example](http://stackoverflow.com/questions/235233/asp-net-mvc-should-business-logic-exist-in-controllers). What is Data logic anyway? To me it just sounds like another way of saying business logic. – netcoder Jan 15 '11 at 03:16
  • You're right of course, strictly Business Logic should be in the Model.. In my experience though, it usually ends up that the Model simply defines the data structure, and how it's stored/retrieved, whereas things like Validation, and higher-level logic (i.e. who can access the specified Object) are generally handled in the Controller. That's why I make the distinction between "Business Logic" and "Data Logic" - maybe it's better represented as "Business Logic" goes in the Model and "Application Logic" goes in the Controller. – Stephen Jan 15 '11 at 06:09