Archive

Archive for the ‘Web Development’ Category

Adding a Self Signed Certificate to Trusted Certificate on Linux

January 14, 2016 Leave a comment

Some times, when we generate self signed certificates, some libraries need it to be a part of the operating systems trusted certificates

I am doing this on CentOS 7 and for openSSL

This will only work for apps/libraries that use OpenSSLs trusted certificate list

I already have my self signed certificate in /etc/pki/tls/certs/my-self-signed-cert.crt

cp /etc/pki/tls/certs/devinviteright.crt /etc/pki/ca-trust/source/anchors

sudo update-ca-trust

PHP 5.6 and Self Signed Certificates for Soap, file_get_contents

November 18, 2015 Leave a comment

Operating System: CentOS 7
PHP: 5.6

This is only for local machine and local development – use at your own risk

I a using a self signed SSL certificate on my local development machine and many functions in PHP refuse to work with self signed certificates.
Some of the functions are

  • file_get_contents()
  • fopen()
  • SoapClient()

my domain name for local machine: localhost
my Self Signed SSL certificate in PEM format: localhost.crt

If i use any of these functions to access a HTTPS resource, the calls fail with invalid certificate
eg file_get_contentc(“https://localhost/test.csv”); will fail

the easiest method is to force add your self signed certificate to the trusted ca-authority in the operating system and openssl

# this should already be installed if you have openssl installed
yum -y install ca-certificates

# copy your self signed cert to the /etc/pki/ca-trust/source/anchors - change certificate path as necessary
cp /etc/pki/tls/certs/localhost.crt /etc/pki/ca-trust/source/anchors

# update
sudo update-ca-trust

now SoapClient, file_get_contents and fopen should work without problems

Other way to do it is to manually send in the stream_context to each of the calls like here

http://stackoverflow.com/questions/18465567/php-soapclient-verify-peer-true-fails-with-ca-signed-certificate

Ajax File Upload without iframe

February 10, 2015 Leave a comment

The Invisible I-frame File Upload method works in almost all browsers.

This post explains using FormData to submit forms that is supported by most new browsers.

<?
if( isset($_POST) && ! empty($_POST) ) {
 
     $file = $_FILES['myfile'];
 
     $output ="Response From the Server\n";
     $output .= "myInput = ".$_POST['myInput'];
     $output .= "\n".'File Name: '.$file['name'];
     $output .= "\n".'File Type: '.$file['type'];
     $output .= "\n".'File Size: '.$file['size'];
     $output .= "\n".'File Tmp Name: '.$file['tmp_name'];
 
     echo $output;
     exit;
}
?>

<html>
<head>
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
     <script type="text/javascript">
         jQuery(document).ready(function($){

         /**
          * when the file is selected 
          */ 
          $("#myfile").change(function() {

               var file = $('#myfile')[0].files[0];

               // new form data 
               var formData = new FormData();
               formData.append('myfile', file); // add the file details
               formData.append('myInput', "Hello World"); // add other data as required
 
               // make an ajax post request 
               $.ajax({
                   url: '/test/file.php',
                   type: 'POST',
                   data: formData,
                   processData: false, // this has to be set to false
                   contentType: false, // this has to be set to false
                   error: function(jqXHR, textStatus, errorThrown){
                       alert("Failed"); 
                   },
                   success: function(data) {
                       alert(data);
                   }
              });
         });
     });
 
 </script>
</head>
<body>
    <input type="file" name="myfile" id="myfile">
</body>
</html>
Categories: Jquery, LAMP, Web Development

Zend Framework 1 – Simple Caching

You can cache data in Zend using the Zend Data Cache.

In the Boorstrap.php file add an _init method to initialize the zend cache

Zend will automatically call all methods in bootstrap that start with _init

protected function _initCache () {
 
   // check and create zend cache directory if it does not exist
   $zendCacheDir = '/tmp/zend-cache';
   if( ! file_exists($zendCacheDir) ) mkdir($zendCacheDir);
 
   $frontendOptions = array(
     'lifetime' => 24 * 60 * 60, // cache lifetime of 24 hours
     'automatic_serialization' => true // for objects and arrays 
   );
 
   $backendOptions = array(
     'cache_dir' => $zendCacheDir // Directory where to put the cache files
   );
 
   // getting a Zend_Cache_Core object
   // we are doing File caching here. Other options are SQLite DB, Memcached, APC
   $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
 
   // cache database table schemata metadata for faster SQL queries
   Zend_Db_Table_Abstract::setDefaultMetadataCache($cache); 
 
   // register the cache to zend registry so that we can use it anywhere in the application
   Zend_Registry::set('Cache', $cache);
 }

From your controller, you can then use this

public function getuserAction($userid) {
 
   // get the cache from the registry 
   $cache = Zend_Registry::get('Cache');
 
   // check if a cached user object exists 
   $cacheKey = "userid_$userid";
   $user = $cache->load($cacheKey);
   
   if( $user === false ) { // cache miss
    // get user data from the database 
    $user = $this->getUserFromDatabase($userid); // your custom function to get data from the database 
 
    // save user to cache 
    $cache->save($user, $cacheKey);
  }
 
  return $user;
}

Other things you can do with the cache

// remove a cache 
$cache->remove($cacheKey);

// clear the entire cache
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);

// clean only outdated
$cache->clean(Zend_Cache::CLEANING_MODE_OLD);

 

Categories: LAMP, Web Development Tags: ,

var_dump() output truncated ?

July 31, 2013 1 comment

if you are using xdebug for PHP debugging and your var_dump() output is being truncated, add the below lines to your php.ini or xdebug.ini

restart apache

xdebug.var_display_max_data = -1
xdebug.var_display_max_children = -1
xdebug.var_display_max_depth = -1 
each of these settings are explained here – http://xdebug.org/docs/all_settings
Categories: LAMP, Web Development

Installing Memcached on Ubuntu

July 24, 2013 4 comments

There seems to be confusion between memcache and memcached and there are tutorials all over the internet telling you steps to install memcache/memcached without actually telling what they are.

We need to install 2 things to get memcached working ( I am  on Ubuntu )

  1. The Operating System Library – this is called memcached – there is no choice here, you have to install memcached
  2. Client libraries to access memcached, here you can choose between php-memcache or php-memcached

So PHP has 2 different client libraries ( poorly named as they are confusing ).

Experts on the net say:

  • php-memcache is older and more stable
  • php-memcached is newer but has some additional goodies

Choose what php client to install based on what your application supports. The client libraries are different

I am on 13.04, Raring Ringtail  and  have a running instance of LAMP set up

Installing memcached ( OS library )

sudo apt-get install memcached

memcached details

  • memcached by default runs on port 11211 ( make sure that is open if you have firewall rules )
  • config file is at /etc/memcached.conf

Installing php-memcache (client )

sudo apt-get install php5-memcache

Installing php-memcached ( client )

sudo apt-get install php5-memcached

Restart the apache server

sudo service apache2 restart

To check if memcached is runnig

ps aux | grep memcached

You will see the output similar to this

memcache 17189 0.0 0.0 323228 1172 ? Sl 09:16 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
root 17219 0.0 0.0 9392 896 pts/1 S+ 09:21 0:00 grep --color=auto memcached

To Increase memory for memcached, open the /etc/memcached.conf and increase the -m parameter
The default is 64M

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 64

Dont forget to restart memcached after config changes
Some basic commands

sudo service memcached restart
sudo service memcached start
sudo service memcached stop

A good Front End for Memcached is phpMemcachedAdmin

Note: This post describes a very basic install and does not take security into consideration. Please secure your memcached on production systems. Some of the techniques can be found on the web like Firewalls, using non-standard ports etc.

Categories: LAMP, Web Development

DataTables with Twitter Bootstrap FixedColumns not working

DataTables integrates very well with Twitter Bootstrap and the results look amazing.

I was tinkering with the two and ran into a problem when using the FixedColumns plugin.

Whenever I used bootstrap.css, the horizontal scroll bar would disappear.

After searching around for a long time, found that bootstrap table property of max-width is the culprit.

So just disable that property in bootstrap.css and the horizontal scroll should start working

table {
 /* max-width: 100%; */
 background-color: transparent;
 border-collapse: collapse;
 border-spacing: 0;
}