PHP snippets

From Wowww mediawiki

Jump to: navigation, search

Get execution trace

With the PHP command debug_backtrace you can generate the backtrace.
This can be handy when solving problems.
The function debug_backtrace generates an array with all traces to the current function.

$trace = debug_backtrace();
//Prints the last executed trace:
print_r($trace[0],true);
//Prints the trace before last executed trace:
print_r($trace[1],true);
..
.

See also http://php.net/manual/en/function.debug-backtrace.php

Go-back button

Creates a URL that directs the user back to the previous page, if that page is in the same domain. If it isn't, a fall-back URL is provided.

// form the URL of the 'go back' link on top of the page
$referrer = $_SERVER['HTTP_REFERER'];
// check if the user was directed here from within the same domain
if (preg_match("/{$_SERVER['HTTP_HOST']}/", $referrer)) {
$go_back_url = $referrer;
} else {
// fallback
$go_back_url = "some path";
};
$go_back_link = l(t('go back'), $go_back_url, array('attributes' => array('class' => array('go-back'), 'title' => t('Return to previous page'))));
unset($referrer, $go_back_url);
 
...
 
print $go_back_link;
unset($go_back_link);
Personal tools