Category Archives: PHP

Ncrypt : A 2-way encryption library for PHP

I have released Ncrypt, a 2-way encryption library for PHP. Using Ncrypt, you can encrypt a string and decrypt an encrypted string.

Steps

1. Include the class

1.a. Option 1 (using Composer)

  • Add the following to your composer.json file-
  • Run composer install command.
  • Include your autoloader file (if not already), like this-

1.b. Option 2

  • Copy class.ncrypt.php file from ncrypt/src/ directory to your project.
  • Include the class in your project file, like this-

2. Instantiate the class

Create an object of the class.

3. Configure (optional)

Optionally set secret key, secret IV and cipher

4.1. How to encrypt a plain text/string

Pass your string to encrypt() method-

4.2. How to decrypt an encrypted string

Pass the already encrypted string to decrypt() method-

Requirement (minimum)

  • PHP 5.3.0
  • php_openssl library needs to be enabled. See here

Link: https://github.com/mukto90/ncrypt

Disclaimer: This library is NOT guaranteed to be secure enough to use in your serious projects.

How to handle error messages in PHP

Error messages are nightmares to a programmer. Default error messages are annoying and looking ugly. And kind of security threat as well. You probably wish to show a custom message when an error occurred. And tried and failed?

I had written this small code snippet for one of my projects and now wondering if this can help others. This function will triggered when a shutdown event happens.

Place this code at the top of your project file. Or maybe create a separate file and include where required.

This will help you handle messages for fatal error, undefined function error and required file not found error. And a general message for other errors.

So, when an undefined function is called, for example

It’ll show your defined message instead of showing-

You can change messages in line 27, 31, 35 and 39 with your own texts too.

A simple two-way function to encrypt or decrypt a string

Update: I have released Ncrypt, a library (OOP based) that facilitates the same (two-way encryption and decryption). See here

This function will provide you a two-way system to encrypt a string or decrypt an encrypted string.

So, how to encrypt a string?

Just call this function and pass your string. Additionally set your second parameter to 'e' (optional). To encrypt Hello World!, write-

It’ll generate a encrypted string RTlOMytOZStXdjdHbDZtamNDWFpGdz09

And, how to decrypt?
Pass your encrypted string to the function and set second parameter to 'd'.

 

Transfer large files from one server to another in seconds

Few days ago, I was working for one my clients from the USA. I was asked to transfer his site from BlueHost to HostGator! The task was so easy. I told him that, it may require around one hour to transfer his entire site.

But, when I logged in to the cPanel, I wonder that, total file size to be transferred is around 1GB. It’ll take two and half hours ONLY to download the site to my local server! Now what?

I used the script below.

 

How to use:

  1. Create a new file on your destination server. Say, file.php
  2. Copy the code above and paste it to file.php
  3. Go to your source server via file manager/FTP. Zip entire site.
  4. Run your file.php and give the URL of zipped file of the source. Click Submit. Wait for seconds till the page reloads.
  5. Go to destination server. Amazing! The file is here! Unzip it.
  6. You are done! It’ll take dramatically very small time!

Note: if you wish to move the files to a specific folder in the destination, just ‘uncomment’ line# 13 and replace download with your own folder name.