Monday, 23 June 2014

Email error logs to yourself

<?php

// Our custom error handler
function nettuts_error_handler($number, $message, $file, $line, $vars){
    $email = "
        <p>An error ($number) occurred on line
        <strong>$line</strong> and in the <strong>file: $file.</strong>
        <p> $message </p>";
       
    $email .= "<pre>" . print_r($vars, 1) . "</pre>";
   
    $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
   
    // Email the error to someone...
    error_log($email, 1, 'you@youremail.com', $headers);

    // Make sure that you decide how to respond to errors (on the user's side)
    // Either echo an error message, or kill the entire project. Up to you...
    // The code below ensures that we only "die" if the error was more than
    // just a NOTICE.
    if ( ($number !== E_NOTICE) && ($number < 2048) ) {
        die("There was an error. Please try again later.");
    }
}

// We should use our custom function to handle errors.
set_error_handler('nettuts_error_handler');

// Trigger an error... (var doesn't exist)
echo $somevarthatdoesnotexist;

PHP Cleane Input

function cleanInput($input) {

  $search = array(
    '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
    '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
    '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
  );

    $output = preg_replace($search, '', $input);
    return $output;
  }
?>
<?php
function sanitize($input) {
    if (is_array($input)) {
        foreach($input as $var=>$val) {
            $output[$var] = sanitize($val);
        }
    }
    else {
        if (get_magic_quotes_gpc()) {
            $input = stripslashes($input);
        }
        $input  = cleanInput($input);
        $output = mysql_real_escape_string($input);
    }
    return $output;
}
<?php
  $bad_string = "Hi! <script src='http://www.evilsite.com/bad_script.js'></script> It's a good day!";
  $good_string = sanitize($bad_string);
  // $good_string returns "Hi! It\'s a good day!"

  // Also use for getting POST/GET variables
  $_POST = sanitize($_POST);
  $_GET  = sanitize($_GET);
?> 

Sunday, 22 June 2014

Getting Keys/Values Separately From Associative Array

If you somehow need to get either only the keys or values of an associative array, you can do so with help of “array_keys” or “array_values” functions. See the example below:
1
2
3
4
5
$my_array = array("key1"=>"value 1","key2"=>"value 2","key3"=>"value 3");
 
print_r(array_keys($my_array));
echo "<br>";
print_r(array_values($my_array));

Difference Between Two PHP Arrays

If you are trying to exclude few elements from an array which are common to another array’s elements, then “array_diff” is the perfect function for you. Use as below:
1
2
3
4
$myarray1 = array(1,2,3,4,-1,-2);
$myarray2 = array(-1,-2);
$filtered_array = array_diff($myarray1,$myarray2);
print_r($filtered_array);

Sorting Arrays In PHP

there are two ways to sort elements of array. If you want a simple default ascending order sorting with non-object array elements, then you can go for “sort” function. You just pass the array as parameter and it will sort the array and returns true if succeed.
1
2
3
4
$simple_array = array("dbc","bcd,"abc");
if(sort($simple_array)){
print_r($simpl_array);
}

If you want to define your own logic for sort condition and/or you are using objects as element of array, go for “usort” function. Example:
01
02
03
04
05
06
07
08
09
10
11
12
13
$elements = array();
 
$elements[0] = (object)NULL;
$elements[0]->post_time = new DateTime();
 
$elements[1] = (object)NULL;
$elements[1]->post_time = new DateTime();
 
usort($elements,"sort_function");
 
function sort_function( $a, $b ) {
   return $b->post_time->diff($a->post_time)->invert;
}