Category Archives: PHP

Solve PHP imap_open Invalid Certificate Failure

Lets say, I have a following configuration:

Incoming Server:
IMAP Port: 143
Password: userpass

But having error while trying to connect inbox, like following code:

$inbox = imap_open('{}INBOX', '', 'userpass');

Can't open mailbox {}INBOX: invalid remote specification

The solution is to use a flag “/novalidate-cert”. Its purpose is “do not validate certificates from TLS/SSL server, needed if server uses self-signed certificates”.

So this will be right call:

$inbox = imap_open('{}INBOX', '', 'userpass');

Get url parameters from url-string

Lets say, we have an url as variable. We want to parse its each parameters.
Here is a solution:

$url = '';

$urlParam = parse_url($url);
    ['scheme'] => https
    ['host']   =>
    ['path']   => /search
    ['query']  => hl=en&tbo=d&site=&source=hp&q=skin+smoothing+cream&hl=en&ei=YqVnUu2BCYfZrQfRwYFw&start=10&sa=N

parse_str($urlParam['query'], $queryParam);
    ['hl']      => en
    ['tbo']     => d
    ['site']    => 
    ['source']  => hp
    ['q']       => skin smoothing cream
    ['ei']      => YqVnUu2BCYfZrQfRwYFw
    ['start']   => 10
    ['sa']      => N

Access private Variable and private Method from outside of Class in PHP

Today I have learnt how to access private variable and private method from outside of a php class. It is by using a built-in class “ReflectionClass”.

Here is the code-snippet:

Class Demo
    private $myPrivateVariable = 'I am Private Variable';
	public function myPublicMethod()
        return 'I am Public Method';

    private function myPrivateMethod()
        return 'I am Private Method';

$demo = new Demo();

//$reflection_class = new ReflectionClass('Demo');
$reflection_class = new ReflectionClass($demo);

$private_method = $reflection_class->getMethod('myPrivateMethod');
echo $private_method->invoke($demo);
// I am Private Method

$private_variable = $reflection_class->getProperty('myPrivateVariable');
echo $private_variable->getValue($demo);
// I am Private Variable

USE keyword in php anonymous function

I have found a nice use of USE keyword in php anonymous function from Erik’s blog

According to his article:

The use keyword allows you to introduce local variables into the local scope of an anonymous function. This is useful in the case where you pass the anonymous function to some other function which you have no control over.

function remove_lowest_average($arr) 
	$average = array_sum($arr) / count($arr);

	return array_filter($arr, function($v) use ($average) {
		return ($v > $average);

following is an array with 9 elements which sum up to 45. 
Average value is 5. 
So value <= 5 will be omitted.
$input = array(1,2,3,4,5,6,7,8,9);
// Output: array(6,7,8,9);

Using Zend Log in Codeigniter

If you are developing a complex script then LOG helps you to keep trace the function execution flow and thus helps fixing out bug.

Here is an implementation of using Zend Log in Codeigniter framework:
1. Copy Zend Log Library:
i. Download ZendFramework-1.11 from here:
ii. Extract it. Inside it there is a “Log” folder inside “library/Zend” directory.
iii. Create a folder inside “application/libraries” of your codeigniter and copy “Log” folder (from ii.) into it.
iv. Copy “Exception.php” and “Log.php” from extracted “library/Zend”  directory to Codeigniter’s “application/libraries/Zend” directory.

2. Create Library:
Create Zend.php inside application/libraries and paste following codes:

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

class CI_Zend {
	private $writer;
	private $logger;

	public function __construct() {
		ini_set('include_path',ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');

	public function log_init($logfile) {
		require_once 'Zend/Log.php';
		require_once 'Zend/Log/Writer/Stream.php';

		$this->writer = new Zend_Log_Writer_Stream($logfile);
		$this->logger = new Zend_Log($this->writer);

	public function info_log($msg) {
		$this->logger->log($msg, Zend_Log::INFO);

	public function err_log($msg) {
		$this->logger->log($msg, Zend_Log::ERR);

3. Create controller myzend.php:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Myzend extends CI_Controller {

    public function __construct() {

    public function index() {

       $this->zend->info_log('This is Info message', Zend_Log::INFO);
       $this->zend->err_log('This is Error message', Zend_Log::ERR);

Thats it.

Log Your PHP Application With Zend_Log (Part 2)
Integrate Zend library in code Igniter

Get multiple checkbox values selected in PHP

$test_chk = array();

if(isset($_POST) && !empty($_POST)) {

	$test_chk = $_POST['test_chk'];

		$test_chk = array();

<form method="post" action="">
	<strong>Multiple Check Test:</strong><br />
	<input type="checkbox" name="test_chk[]" value="value A" <?php if(in_array("value A", $test_chk)) echo ' checked="checked"';?> /> value A<br />
	<input type="checkbox" name="test_chk[]" value="value B" <?php if(in_array("value B", $test_chk)) echo ' checked="checked"';?> /> value B<br />
	<input type="checkbox" name="test_chk[]" value="value C" <?php if(in_array("value C", $test_chk)) echo ' checked="checked"';?> /> value C<br />
	<input type="checkbox" name="test_chk[]" value="value D" <?php if(in_array("value D", $test_chk)) echo ' checked="checked"';?> /> value D<br />
	<input type="submit" name="test_sbt" value="Submit" />

Send an array from a form then parse it back with PHP

Lets say we want to send an array within a form and also retrieve this array by php.

Here is a solution:

	$arrayVal = unserialize(base64_decode($_POST['myary']));
	echo '<pre>'; print_r($arrayVal); echo '</pre>';

//$a = array('blah' ,'blah1' ,'blah2') ;
$a = array('a'=>'blah', 'b'=>'blah1', 'c'=>'blah2') ;
$ary = base64_encode(serialize($a));

<form name="form1" id="form1" action="" method="post">
	<input type='hidden' name='myary' value="<?php echo $ary ;?>" >
	<input type="submit" name="press" />

I found this solution from here.