Archive for the ‘Web Development’ Category

Dynamically loading CSS and JS files using jQuery

June 26, 2013 26 comments

Simple tutorial on how we can use jquery to load CSS and JS files as and when we require them

We will create 4 files ( in the same directory of your web root – mine is /var/www/test )

  • index.html : the main html file
  • main.js : the main JS file which will be included in main.html along with the jquery library
  • style.css : this file will be dynamically loaded using main.js
  • one.js : this file will be dynamically loaded using main.js

the files are here – rest of this is self explanatory 🙂


 <script type="text/javascript" src="//"></script>
 <script type="text/javascript" src="main.js"></script>
 <h3 class="red">This red color is from dynamically loaded css file</h3>


  * function to load a given css file 
 loadCSS = function(href) {
     var cssLink = $("<link rel='stylesheet' type='text/css' href='"+href+"'>");

 * function to load a given js file 
 loadJS = function(src) {
     var jsLink = $("<script type='text/javascript' src='"+src+"'>");
 // load the css file 

 // load the js file 



.red {
    color: red;



    alert("This alert is from dynamically loaded js file");


Simple PHP encrypt and decrypt

March 12, 2013 62 comments

You need to have openssl encrypt installed and running. Check this thread if you dont have it already

 * simple method to encrypt or decrypt a plain text string
 * initialization vector(IV) has to be the same when encrypting and decrypting
 * PHP 5.4.9 ( check your PHP version for function definition changes )
 * this is a beginners template for simple encryption decryption
 * before using this in production environments, please read about encryption
 * use at your own risk
 * @param string $action: can be 'encrypt' or 'decrypt'
 * @param string $string: string to encrypt or decrypt
 * @return string
function encrypt_decrypt($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = 'This is my secret key';
    $secret_iv = 'This is my secret iv';

    // hash
    $key = hash('sha256', $secret_key);
    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    else if( $action == 'decrypt' ){
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);

    return $output;

$plain_txt = "This is my plain text";
echo "Plain Text = $plain_txt\n";

$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "Encrypted Text = $encrypted_txt\n";

$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
echo "Decrypted Text = $decrypted_txt\n";

if( $plain_txt === $decrypted_txt ) echo "SUCCESS";
else echo "FAILED";

echo "\n";

Categories: Web Development

Simple PHP-APC setup on Ubuntu 12.04

March 4, 2013 4 comments

I assume that you already have PHP up and running

Install APC

Method 1: One Step Install ( but you will not get the latest APC )

 sudo apt-get install php-apc
 sudo service apache2 restart

Method 2: Latest Version Install

 sudo apt-get install php-pear php5-dev libpcre3-dev
 sudo pecl install apc

It will ask for some options – just use the default ones ( press enter )
After installation at the end it will display messages like
Build process completed successfully

 Installing '/usr/lib/php5/20100525+lfs/'
 Installing '/usr/include/php5/ext/apc/apc_serializer.h'
 install ok: channel://
 configuration option "php_ini" is not set to php.ini location
 You should add "" to php.ini

make a new file in /etc/php5/conf.d/apc.ini and put a single line in this file

 sudo echo "" > /etc/php5/conf.d/apc.ini
 sudo service apache2 restart

check for apc in phpinfo() – you should be able to see all the apc variables in the output

php -i | grep "apc"


APC Web Administration

Make a directory in the web root and copy the apc.php file to it

 sudo mkdir /var/www/apc
 sudo cp /usr/share/php/apc.php /var/www/apc/

go to http://localhost/apc/apc.php ( this is insecure )


Securing APC Web portal using .htaccess

create the .htpasswd file outside your web root ( preferably in your home directory )
generate the username and password and copy it to the .htpasswd file

 sudo htpasswd -n apc_admin >> ~/.htpasswd

.htaccess rules for the apc directory – change the AuthUserFile and require user name accordingly

echo "AuthUserFile /home/naveen/.htpasswd" >> /var/www/apc/.htaccess
 echo "AuthName \"Please Log In\"" >> /var/www/apc/.htaccess
 echo "AuthType Basic" >> /var/www/apc/.htaccess
 echo "require user apc_admin" >> /var/www/apc/.htaccess

Add the following lines to the apache config file to allow .htaccess override for the apc directory

 <Directory /var/www/apc>
 AllowOverride All

Make sure that your apache conf file has the below line – else add it

 AccessFileName .htaccess

Restart Apache one final time

sudo service apache2 restart

now go to http://localhost/apc/apc.php and it should ask you for a password


APC Internal Authentication

The apc.php file has credentials to access user cache entries ( disable it – make sure that the below line has the value zero – it might already be set to zero)


PHP simple 2 way plain text encryption

February 12, 2013 2 comments

HTMl to PDF using WKHTMLTOPDF on Ubuntu linux

July 26, 2011 30 comments

Some things to understand before we begin

WKHTMLTOPDF is not a php utility

Its a command line C library to convert html files to pdf

I am only using PHP here to generate the HTML files i need to convert

You can use any language of your choice to generate the HTML files

So the steps would be

  • Generate the HTML of the page with all the required CSS in it
  • Save this page with an html extension somewhere in your file system
  • use the wkhtmltopdf command to convert the saved HTML file to PDF

If you are trying to do this on Windows – this might help :

This is for using wkhtmltopdf command line utility in Ubuntu, Apache and PHP

  1. wkhtmltopdf is one of the best html to pdf conversion tools i have come across
  2. It is a command line utility
  3. It is very fast and converts most html pages – impressive  css support
  4. Integration with php is a breeze ( after struggling for 6 hrs 😛 )
Here is how to generate PDF files
  1. Download the latest wkhtmltopdf STATIC file from the site – at present it is wkhtmltopdf-0.10.0_rc2 Linux Static Binary (i368)  for Linux 32 bit
  2. There is also a wkhtmltoimage – dont download that accidentally and try to get pdf’s 🙂
  3. Please read the SYSTEM requirements carefully – wkhtmltopdf requires X11 libraries – which are not present on a typical bare-bone web server – you may need to install them
  4. unzip the file ( and rename it to wkhtmltopdf – optional)
  5. make sure that wkhtmltopdf is in your SYSTEM PATH – my PATH in UBUNTU is /usr/local/bin  – you may have a different PATH – find it out by using the command env  on the terminal
  6. I copied the wkhtmltopdf to this folder – so the file path now is /usr/local/bin/wkhtmltopdf
  7. From  the terminal issue this command /usr/local/bin/wkhtmltopdf /tmp/google.pdf 
  8. Basically we are trying to convert Google’s home page into a pdf file ( change destination folder if you need )
  9. Command syntax is path-to-wkhtmltopdf path-to-html-file path-to-pdf-file
  10. Make sure that the destination directory has write access to apache
  11. If a pdf file is generated, then you have successfully installed wkhtmltopdf
  12. To make it work in a web environment, the apache user (www-data on Ubuntu) needs to have execute permissions to this file /usr/local/bin/wkhtmltopdf 
  13. After you have given the correct permissions, its time to create a small php script to test ( lets say at localhost/test.php )
    ini_set('display_errors', '1');
    $cmd = "/usr/local/bin/wkhtmltopdf /tmp/yahoo.pdf";
    $t = shell_exec($cmd);

Run this php script on a browser and check if the pdf file was created

If you have a local html file and want to convert it then just use

$cmd = "/usr/local/bin/wkhtmltopdf path-to-html-file path-to-pdf-file";
Categories: Web Development

Wierd Exclamation characters in the html email

April 12, 2011 1 comment

In case you are trying to send an html email via the mail function in PHP and you are getting weird formatting and exclamation characters, then you need to insert line breaks (“\n”) into the message of your email

Check out the php documentation @
It says: Each line should be separated with a LF (\n). Lines should not be larger than 70 characters.

So if you are getting badly formatted email, one of the reasons is because of this.

Just before you send your email, you can do this

$email_message = "blah blah blah ..... a long email message.....";
$email_message =  wordwrap($email_message, 70, "\n");
mail($to, $subject, $email_message, $headers);  //send email
Categories: Web Development

Including external Javascript and CSS files in PHP

February 10, 2011 6 comments

Here is how to include external javascript and css files in your php code

   //include CSS Style Sheet
   echo "<link rel='stylesheet' type='text/css' href='path-to-css-file' />";

   //include a javascript file
   echo "<script type='text/javascript' src='path-to-javascript-file'></script>";

If you want to include the javascript and css source code inside the php file itself then you can do it like this

   //include CSS Style Sheet
   echo "<style type='text/css'>
         border: 1px solid;
         color: blue;

      /*heading class*/
      .heading1 {
         font size: 10px;

   //include a javascript file
   echo "<script type='text/javascript'>
      /* All your javascript code goes here*/
Categories: Web Development