Category Archives: PHP

tips, tools, functions and general ideas relating to PHP

Shorthand AJAX examples


$.get("test.php", function(data){
  $('#myFormField').val(data);
});



$.load('myScript.php', function(data) {
    //fill a hidden textbox with your script output and reveal it
    $('#textBox').val(data).show();
)};


PHP Error handing.. ..your way

   

error_reporting(0);
   $old_error_handler = set_error_handler("userErrorHandler");

   function userErrorHandler ($errno, $errmsg, $filename, $linenum,  $vars)
   {
     $time=date("d M Y H:i:s");
     // Get the error type from the error number
     $errortype = array (1    => "Error",
                         2    => "Warning",
                         4    => "Parsing Error",
                         8    => "Notice",
                         16   => "Core Error",
                         32   => "Core Warning",
                         64   => "Compile Error",
                         128  => "Compile Warning",
                         256  => "User Error",
                         512  => "User Warning",
                         1024 => "User Notice");
      $errlevel=$errortype[$errno];

      //Write error to log file (CSV format)
      $errfile=fopen("errors.csv","a");
      fputs($errfile,"\"$time\",\"$filename:
      $linenum\",\"($errlevel) $errmsg\"\r\n");
      fclose($errfile);

      if($errno!=2 && $errno!=8) {
         //Terminate script if fatal error
         die("A fatal error has occurred. Script execution has been aborted");
      }
   }

Recursive Iterating..


function directoryScan($dir, $onlyfiles = false, $fullpath = false) {
     if (isset($dir) && is_readable($dir)) {
          $dlist = Array();
          $dir = realpath($dir);
          if ($onlyfiles) {
               $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
          } else {
               $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
          }
         
          foreach($objects as $entry => $object){    
               if (!$fullpath) {
                    $entry = str_replace($dir, '', $entry);
               }
              
               $dlist[] = $entry;
          }
         
          return $dlist;
     }
    }

   
  //Return contents of ‘Folder’ – Relative Paths and Files & Folders.
$result = directoryScan('folder', false, false);

//Return contents of ‘Folder’ – Full Paths and Files & Folders
$result = directoryScan('folder', false, true);

//Return contents of ‘Folder’ – Relative Paths and Only Files.
$result = directoryScan('folder', true, false);

//Return contents of ‘Folder’ – Full Paths and Only Files
$result = directoryScan('folder', true, true);

AJaX request

Simple AJAX request

 

        $('#loginSubmit').on('click', function(e){
            e.preventDefault();
            checkLogin();
        })
        
        function checkLogin()
        {
             $.ajax({
                url: "ajax/login.php",
                type: "POST",
                data: {
                    username: $("#form_signin #email").val(),
                    password: $("#form_signin #password").val()
                },
                success: function(response)
                {
                    if(response == 'true'){
                        window.location.replace("/saved-route.php");
                    } else {
                        $("#errorMessage").replaceWith('

'+response+'

'); } } }); }

Display tweets from RSS with twitter style links

Based on an original post here:
http://www.farinspace.com/twitter-feed-website-integration/
but modified lisghtly to mimic Twitter.


\1@\2', $v);
	$v = preg_replace('/(^|\s)#(\w+)/', '\1#\2', $v);

	$v = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1\\2'", $v);
	$v = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1\\2'", $v);
	$v = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2@\\3", $v);

	return trim($v);
}

?>


prevent caching of files

Version them in their with their timestamp. This way, when they change, the link will change and the browser will reload the sitemap… Otherwise it will be fine to be cached as it wont have changed.