PHP, MySQL, HTML, CSS, Javascript / jQuery, tips and tricks, and more. Follow to receive updates for free
Tuesday, May 27, 2014
Detect Mobile Browser Using PHP
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
Here's how:
Monday, September 30, 2013
Json_decode Error: Cannot use object of type stdClass as array
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
/([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
- myemail.@example.com
- .myemail@example.com