Thursday, October 24, 2013

Create Dynamic Multidimensional File Tree Array

This is another my PHP snippet to create file tree array dynamically.

<?php
function wps_glob($dir) {
  foreach (glob($dir . '/*') as $f) {
    if(is_dir($f)) {
      $r[] = array(basename($f) => wps_glob($f));
    }
    else {
      $r[] = basename($f);
    }
  }
  return $r;
}

function wps_files($path) {
  $wpsdir = Array(
     'root' => $path,
     'struktur' =>  wps_glob($path)
  );
  return $wpsdir;
}
?>

Usage

<?php

echo '<pre>';
print_r( wps_files( PATH_TO_YOUR_DIRECTORY ) );
echo '</pre>';

?>

PHP Function to send post using CURL

This is one of my PHP snippet collection. This function can performs post request with parameters set. I have used this function for sending request to several API servers and so far it works as well as I need. May be you need to add curl_setopt based on your needs.

The Snippet:
<?php 
function curl_post($url,$params) {
  $parse_url = parse_url($url);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_TIMEOUT,60);
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  if($parse_url['scheme'] == 'https') {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  }
  $result = curl_exec($ch);
  curl_close ($ch);
  return $result;
}
?>

Usage

Below is just example usage to work with API service.

<?php
$url = 'https://api.server.example.com/post.php';
 
// you need to set post parameters
$params = Array(
    'app_id'       => $app_id,
    'app_secret'   => $app_secret,
    'message'      => $message
);

$result = curl_post($url,$params);

?>

Monday, October 21, 2013

How to Include All PHP Files in Directory - The Easy Way

Ever wonder how to include all php files in directory? I just created PHP snippets that may be useful for you. Here is the code:

The contents of include.php:
<?php
define('MYBASEPATH' , 'inc/');
foreach (glob(MYBASEPATH . '*.php') as $file) {
    if($file != 'index.php') {
        include($file);
    }
}
?>

The code above will include all files in directory inc except index.php. To include all files, simply include include.php file on your script.

<?php
include( 'include.php' );
?>

Enjoy :-)

How to create mysql partitions

The website become very slow because there are too much data on mysql tables. This is the common problem for sites which save historical data. I just found this issue on my website, then googling to find solutions for this. And then, yap, there's simple solution for handling large table issues.

Sunday, October 13, 2013

Creating Simple jQuery QR Code Generator

I've created simple qr code generator using jquery and google infographics (deprecated) chart API. This time I'd like to share this simple and easy trick.

The Demo

Image Result

Download Source Code


HTML Elements

The element to show Qr Code image after jquery event. Below is the example:
<div id="outwrap">

      <div class="left" style="width:490px">
          <textarea id="qrc" style="width:490px;height:220px"></textarea>
          <button id="buildqr">Create</button>
          <span id="qrerror" class="error"></span>
      </div>
      <div class="right" class="tbl" style="width:250px"><div class="tbl-cell" id="qresult">Image Result</div></div>
      <div class="clear"></div>

</div>

Sunday, October 6, 2013

Block temporary emails using PHP

It's really sucks when someone registers on site using temporary email address. This is not good for business. Especially if you pay online services to handle your email marketing campaign. That's why you need securing your registration form not just for email validation, but also prevent receiving contacts that you will never be able to follow up them. I did this by filtering inputs and detect email domain to make sure it isn't in temporary email service list.

Here's how:

Monday, September 30, 2013

Json_decode Error: Cannot use object of type stdClass as array

Oops.. I found this Fatal error: Cannot use object of type stdClass as array in /home/username/html/index.php on line 13 when using json_decode() function. How to fix this?

Simply add true parameter. This will solve the problem:

$row = json_decode( $json_data, true );

// now you can use $row as array

echo $row['name'];

Thursday, September 26, 2013

Perfect PHP Email Regex

This is a perfect regular expression for validating email address from text inputs form. It matches common email address format. Here is the email regex I often use on PHP or Javascipt.

/([a-z0-9_]+|[a-z0-9_]+\.[a-z0-9_]+)@(([a-z0-9]|[a-z0-9]+\.[a-z0-9]+)+\.([a-z]{2,4}))/i

The regular expression above matches email addresses like:
  • myemail@example.com
  • my_email@example.com
  • my.email@example.com
But not for:
  • myemail.@example.com
  • .myemail@example.com

Thursday, September 19, 2013

How to display QR Code dynamically on blogspot using Google Chart Tools

Hi, did you see QR Code on my widget? You should be able to see there, on the right sidebar. Yes, that's dynamic qr code I created using Google Chart Tools and jQuery. It creates QR Code contains current URL of the page. If you like too, I'll show you how I did this little trick. Now you can simply do this on your blog, on any blog. Here's how:

Example jQuery slideToggle

The simple example playing with jquery slideToggle().

<script>
$(function(){
   $('.toggle').click(function(){
       var el = $(this).attr('rel');
       $(el).slideToggle();
   }); 
});
</script>

<button class="stoggle" rel="#hideMe">Click me</button>
<br />
<div id="hideMe" style="background: #ffffd0; display: none; height: 100px; text-align: center; width: 200px;">
I'm the hidden element
</div>

Demo: or toggle the element above:

By adding "rel" attribute, we can toggle specific elements.

Remove blogthis button on blogspot

Using blogspot to blog everything is fun, easy and absolutely free. But how if you don't want someone stole your valuable contents simply by pressing Blogthis button on your blog posts?

Event this feature includes backlink to your post, people can remove it easily from rich textarea. So I decided to remove this button by editing HTML template. Checkout this: