Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, May 27, 2014

Detect Mobile Browser Using PHP

Sometimes we need to serve different looks and functions to users. As we know there so many people around the world using mobile phone to surf the net. I found this useful PHP class to detect user browser. I used this class in several projects.

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 :-)

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