Php stop script execution. Background script execution in PHP without crontab. #one. Demonstration of use
Stops execution PHP script and outputs a formatted HTML message.
Analogue of the base PHP functions die(). The difference is that wp_die() outputs a formatted HTML page errors. It is recommended to use this function when you need to completely stop PHP job. It is not recommended to use this function often - try not to show errors to users.
returns
Returns nothing, but terminates PHP.
Usage
wp_die($message, $title, $args); $message (mixed) Error message or full WP_Error class object.Default: ""$title (string/array/number/object/boolean) Error title. If you use the WP_Error object, then the title will be taken from $data["title"], this parameter can be changed in advance for yourself.
Default: ""$args (string/array)
Various arguments controlling behavior.
Default: no
- text_direction (line)
Text direction: ltr (left to right) or rtl (right to left).
Default: "ltr"
response (number)
HTTP status code. 500 - internal server error. The whole list .
Default: 500
back_link (logical)
Whether or not to display a backlink to the previous page.
Default: false
Examples
#one. Demonstration of use
Using the wp_die() function, let's see what is currently in the $post global variable:
Add_filter("body_class", "add_body_class_cb"); function add_body_class_cb($classes) ( global $post; wp_die("
" var_export($post, true) . ""); }
#2. Styling the wp_die block
If the site needs to change the design of this block, then you can use the wp_die_handler hook:
500); $r = wp_parse_args($args, $defaults); $have_gettext = function_exists("__"); if (function_exists("is_wp_error") && is_wp_error($message)) ( if (empty($title)) ( $error_data = $message->get_error_data(); if (is_array($error_data) && isset($error_data[" title"])) $title = $error_data["title"]; ) $errors = $message->get_error_messages(); switch (count($errors)) ( case 0: $message = ""; break; case 1 : $message = ""; break; default: $message = "
- \n\t\t
- ".join(" \n\t\t
- ", $errors). " \n\t
$back_text
"; ) if (! did_action("admin_head")) : if (!headers_sent()) ( status_header($r["response"]); nocache_headers(); header("Content-Type: text/html; charset= utf-8"); ) if (empty($title)) $title = $have_gettext ? __("WordPress › Error") : "WordPress › Error"; $text_direction = "ltr"; if (isset($r[ "text_direction"]) && "rtl" == $r["text_direction"]) $text_direction = "rtl"; elseif (function_exists("is_rtl") && is_rtl()) $text_direction = "rtl"; ?> >The code wp die : wp-includes/functions.php WP 5.2.2
$args); ) elseif (is_int($title)) ( $args = array("response" => $title); $title = ""; ) if (wp_doing_ajax()) ( /** * Filters the callback for killing WordPress execution for Ajax requests.* * @since 3.4.0 * * @param callable $function Callback function name.*/ $function = apply_filters("wp_die_ajax_handler", "_ajax_wp_die_handler"); ) elseif (wp_is_json_request()) ( /** * Filters the callback for killing WordPress execution for JSON requests. * * @since 5.1.0 * * @param callable $function Callback function name. */ $function = apply_filters("wp_die_json_handler", "_json_wp_die_handler"); ) elseif (wp_is_jsonp_request() ) ( /** * Filters the callback for killing WordPress execution for JSONP requests. * * @since 5.2.0 * * @param callable $function Callback function name. */ $function = apply_filters("wp_die_jsonp_handler", "_jsonp_wp_die_handler") ; ) elseif (defined("XMLRPC_REQUEST") && XMLRPC_REQUEST) ( /** * Filters the callback for killing WordPress executio n for XML-RPC requests. * * @since 3.4.0 * * @param callable $function Callback function name. */ $function = apply_filters("wp_die_xmlrpc_handler", "_xmlrpc_wp_die_handler"); ) elseif (wp_is_xml_request() || isset($wp_query) && (function_exists("is_feed") && is_feed() || function_exists("is_comment_feed") && is_comment_feed() || function_exists("is_trackback") && is_trackback()) ) ( /** * Filters the callback for killing WordPress execution for XML requests. * * @since 5.2.0 * * @param callable $function Callback function name. */ $function = apply_filters("wp_die_xml_handler", "_xml_wp_die_handler") ; ) else ( /** * Filters the callback for killing WordPress execution for all non-Ajax, non-JSON, non-XML requests. * * @since 3.0.0 * * @param callable $function Callback function name. */ $function = apply_filters("wp_die_handler", "_default_wp_die_handler"); ) call_user_func($function, $message, $title, $args); )8 years ago
If you want to avoid calling exit() in FastCGI as per the comments below, but really, positively want to exit cleanly from nested function call or include, consider doing it the Python way:
define an exception named `SystemExit", throw it instead of calling exit() and catch it in index.php with an empty handler to finish script execution cleanly.
// file: index.php
class SystemExit extends Exception()
try(
/* code code */
}
catch (SystemExit $e ) ( /* do nothing */ )
// end of file: index.php
// some deeply nested function or .php file
If(SOME_EXIT_CONDITION)
throw new SystemExit(); // instead of exit()
?>
10 years ago
Jbezorg at gmail proposed the following:
header("Location: /");
?>
After sending the `Location:" PHP header _will_ continue parsing, and all code below the header() call will still be executed. So instead use:
If($_SERVER [ "SCRIPT_FILENAME" ] == __FILE__ )
{
header("Location: /");
exit;
}
?>
10 years ago
To rich dot lovely at klikzltd dot co dot uk:
Using a "@" before header() to suppress its error, and relying on the "headers already sent" error seems to me a very bad idea while building any serious website.
This is *not* a clean way to prevent a file from being called directly. At least this is not a secure method, as you rely on the presence of an exception sent by the parser at runtime.
I recommend using a more common way as defining a constant or assigning a variable with any value, and checking for its presence in the included script, like:
in index.php:
define("INDEX" , true );
?>
in your included file:
if (! defined("INDEX"))(
die( "You cannot call this script directly!");
}
?>
B.R.
4 years ago
A side-note for the use of exit with finally: if you exit somewhere in a try block, the finally won't be executed. Could not sound obvious: for instance in Java you never issue an exit, at least a return in your controller; in PHP instead you could find yourself exiting from a controller method (e.g. in case you issue a redirect).
Here follows the POC:
echo "testing finally wit exit\n" ;
try(
echo "In try, exiting\n" ;
exit;
) catch(Exception $e ) (
echo "catch\n" ;
) finally (
echo "finally\n" ;
}
echo "In the end\n" ;
?>
This will print:
testing finally wit exit
In try, exiting
1 year ago
>> Shutdown functions and object destructors will always be executed even if exit is called.
It is false if you call exit into desctructor.
normal exit:
class A
{
{
echo "bye A\n" ;
}
}
class B
{
public function __destruct()
{
echo "bye B\n" ;
}
}
$a = new A ;
$b = new B ;
exit;
//Output:
// bye B
// bye A
?>
// Exit into destructor:
class A
{
public function __destruct()
{
echo "bye A\n" ;
}
}
class B
{
public function __destruct()
{
echo "bye B\n" ;
exit;
}
}
$a = new A ;
$b = new B ;
//Output:
// bye B
?>
17 years ago
If you are using templates with numerous includes then exit() will end you script and your template will not complete (no,