Snippets

PHP:
/*
    * @ AUTHOR : DasGuru
    * @ COPY  : gecko-dev.de 2011
    * @ PARAM : $orginallose -> der umzuwandelnde Losebetrag
    * @ PARAM : Anzahl der Dezimalstellen für den Output
    * @ RETURN : formatierter string wie zb. "12 Mio" oder "Mrd" oder "1 k"
*/
function loseinflation($orginallose, $dezimalstsellen = 2) {
    
    if (!is_int($orginallose)) settype($orginallose, 'int'); 
    
    if ($orginallose >= 1000000000) { // 1 Mrd
        $anzahl = bcdiv($orginallose,1000000000, $dezimalstsellen);
        $output = number_format($anzahl, $dezimalstsellen, ',','.').' Mrd';
        return $output;
    }else
    if ($orginallose >= 1000000) { // 1 Mio
        $anzahl = bcdiv($orginallose,1000000, $dezimalstsellen);
        $output = number_format($anzahl, $dezimalstsellen, ',','.').' Mio';
        return $output;
    }else
    if ($orginallose >= 1000) { // 1 K
        $anzahl = bcdiv($orginallose,1000, $dezimalstsellen); 
        $output = number_format($anzahl, $dezimalstsellen, ',','.').' k';
        return $output;
    }else {
        return number_format($orginallose, $dezimalstsellen, ',', '.');
    }
}
 
Zuletzt bearbeitet:
[PHP] Ein simples Templatesystem

PHP:
<?php
/**
 * Renders a template from the templates-subfolders with an additional layout
 * 
 * Templates have to be stored in a subdir called "templates".
 * Absolutely nothing is validated so you better do that first.
 *
 * Have in mind that variables are passed from template to layout and the
 * variables from the function's signature cannot be used in a template
 * because they would break the function by overwriting vital information.
 *
 * Example usage:
 * <samp>
 *   $section = render('section', $variables = array(
 *     'title'      => 'Section #1',
 *     'content'    => 'Sample content',
 *     'page_title' => 'Example page',
 *   ));
 *   $html = render('section', $variables, 'layout');
 * </samp>
 * with "templates/section.php":
 * <code>
 *   <h1><?= $section ?></h1>
 *   <p><?= $content ?></p>
 * </code>
 * and "templates/layout.php":
 * <code>
 *   <html>
 *     <head><title><?= $title ?></title></head>
 *     <body><?= $CONTENT ?></body>
 *   </html>
 * </code>
 *
 * @param string $_template  the template to render (note: no extension)
 * @param array  $_variables the variables to be substituted
 * @param mixed  $_layout    an additional layout template
 * @param string $_content   name of content variable used in layout
 *
 * @return string the rendered template
 */
function render($_template, $_variables = array(), $_layout = false, $_content = 'CONTENT') {
	ob_start();
	extract($_variables);
	include 'templates/'.$_template.'.php';
	$$_content = ob_get_clean();

	if ($_layout) {
		ob_start();
		include 'templates/'.$_layout.'.php';
		$$_content = ob_get_clean();
	}

	return $$_content;
}
 
Zuletzt bearbeitet:
[PHP] Simpelstes Proxyerkennen

PHP:
function requested_via_proxy() {
    $proxy_headers = array(
        'HTTP_VIA', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR',
        'HTTP_V_FORWARDED', 'HTTP_FORWARDED', 'HTTP_CLIENT_IP',
        'HTPP_FORWARDED_FOR_IP', 'VIA', 'X_FORWARDED_FOR',
        'FORWARDED_FOR', 'X_FORWARDED', 'FORWARDED', 'CLIENT_IP',
        'FORWARDED_FOR_IP', 'HTTP_PROXY_CONNECTION',
    );
    return (bool) count(array_intersect($proxy_headers, array_keys($_SERVER)));
}