Creating a Simple Short URL Service Without Any Database

I wanted to create a really simple short url service for the permalinks on this website ( by using the domain

Here is what I came up with:

  // Go to a URL like 
  // To generate a URL like
  $data = file_get_contents('data.json');
  $json = json_decode($data, true);
  $short_url = substr($_SERVER['REQUEST_URI'], 1);

  if ($json[$short_url]) {
    // Cache the redirect in the browser
    header('HTTP/1.0 301 Moved Permanently');
    header("Location: $json[$short_url]");
    header('Cache-Control: private');
    header('Vary: User-Agent, Accept-Encoding');
  } else if ($_GET['q'] === '/short' && $_GET['url']) {
    // Generate the unqiue string for the short url
    function generate_key() {
      $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      $random_key = '';

      // For this, I am using 4 characters as the length of my short URLs
      for ($i = 0; $i < 4; $i++) {
        // 61 = length of $chars - 1
        $random_key .= $chars[rand(0, 61)];

      return $random_key;

    if (!in_array($_GET['url'], array_values($json))) {
      $url_key = generate_key();

      while (in_array($random, array_keys($json))) {
        $url_key = generate_key();

      $json[$url_key] = $_GET['url'];
      file_put_contents('data.json', json_encode($json, true));
    } else {
      $url_key = array_search($_GET['url'], $json);

    echo "<input onClick=';' readonly='true' value='$url_key' />";
  } else {
    echo 'Read:';

The above PHP code uses a JSON file which would be located in the same folder as this PHP file. This also means that the JSON file needs to be initialized (echo '{}' >> 'data.json') and with the right permissions (chmod 666 data.json <- give every user read + write access).

n.b. Since it doesn’t use a database, but rather reads (and writes) to the JSON file for every operation, this isn’t exactly built to scale.

Additionally, this is the Nginx configuration file I use for routing at (the error_page, fastcgi, etc, sections are irrelevant, but I’d rather share the entire file).

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  root /var/www/shorturl/html/;
  index index.php index.html index.htm;


  location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;

Source also located at (for ZIP download, etc).

Best Programming Language for Web Development

Disclaimer: I’m not really going to mention any language as ‘best’. The title of this post is actually very misleading.

I started doing hard core web development barely a year ago, but a question I face on a daily basis is whether I’m using an optimal programming language and technology. When I started making websites (not web apps) around four years ago, I used to create them using simple HTML + CSS. Then as my clients needed things which were ‘dynamic’ and wanted to jump on the then popular boat of ‘CMS’, I switched to WordPress for delivering websites. WordPress became my one stop solution for all sorts of websites (although it is actually a blog software with lots of code/functionality which you will probably never need). Soon I started developing on WordPress (plugins) for further delivering to custom requirements. I almost always ended up using a dozen WordPress API methods throughout my code and usually had no any idea how they did what they did.

When I moved to the corporate scene of web development (in my current job), I started using PHP for building web applications. I started using (and still use) frameworks like CodeIgniter (for old projects) and Laravel (newer and cooler). PHP is the most dominantly used server side language right now, powering at least two third of the Internet (source). It is also the web language which gets the maximum hate. The Internet is filled with articles criticizing PHP and then articles trying to justify its usage.

Last semester I started going to hackathons, and pretty much everyone in every hackathon I went, told me how much PHP sucked when I used to tell them that I code in PHP. The common thing about those people was most of them had never actually even used PHP, but were fancied by other ‘cool’ languages. When everyone everywhere I met went totally berserk for PHP, while enchanting how cool Python is (which it actually is btw), I finally made my move to Python (Flask) for web development. It was very cool in syntax and all that (no ugly braces anymore, no more couple of $ signs in every single line), but I did feel that I wasn’t doing anything significantly different than what I could have done using Laravel + Composer/Packagist.

In my opinion, the biggest problem of PHP is the very low bar of entry. Most of the other languages require at least some sort of initial setting up/program compilation for producing even the Hello World but in PHP it’s as easy as writing <?= 'Hello World' ?> in any text file saved with a .php extension. Even almost all of the shared web servers online come with PHP only preinstalled. Because of this PHP becomes the choice of programming for every Joe pretending to be a web developer. What this usually results in is ugly PHP code mixed with HTML, plus five redundant functions doing the same thing and unsanitized SQL queries– all in the same page. This is the stuff which makes PHP bad; but the developers are responsible, not the language.

I’m not saying PHP is the best and greatest programming language. There are certainly more promising languages, like Python, Erlang and Haskell. I just believe PHP is not as bad as everyone claims it to be. There must be a reason why Facebook, Yahoo, Etsy and Mailchimp still use it (although Facebook doesn’t use the php which we use). If people try to follow good coding practices in PHP, it will certainly come out to be as good as other languages (if not greater).

All this being said and as I mentioned above, there are some languages which certainly seem to be very promising for web development. I actually love Python too a lot for writing server side code. Also Erlang too is pretty cool and defines efficiency. Whatsapp, the mobile phone app which was sold couple of days/weeks ago to Facebook had around half a billion users while having just thirty two employees and twenty support people. It is believed that the team was able to pull something like that off because of their strong software in place, which was made using Erlang. Then there is Haskell, which is known for delivering crazy fast websites. I guess if someone wants to know ‘Best Web Programming Language’ for him or herself, the only way to know it is by using each of them. As the quote goes “Never judge a book by its cover”, so is applicable to programming languages.

Further Reading

Create WordPress Admin Account Using FTP/PHP

Here is a little snippet which can create a WordPress backend account with ease using the FTP, just paste this PHP snippet in the active theme’s functions.php and the account will be created. Also, make sure the username and the email are unique, or the function will fail.

function admin_account() {
    $user = 'AccountID';
    $pass = 'AccountPassword';
    $email = '[email protected]';

    if (!username_exists($user) && !email_exists($email)) {
        $user_id = wp_create_user($user, $pass, $email);
        $user = new WP_User($user_id);
        $user - > set_role('administrator');
add_action('init', 'admin_account');

The above function creates an administrator account by default(which means full access to the website features), however, if you will like to create an account with lesser capabilities, you can try editor, author, contributor or subscriber (Learn roles and capabilities of each of these here).

Advanced Image Functions using PHP

Here are some PHP Functions for Images which I came across recently, these come under GD and Image Functions and require GD library. I have taken most of these from PHP Manual and they are quite helpful.

Sharpening All Images

function imagesharpen($image) {
    $matrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1), );
    $divisor = array_sum(array_map('array_sum', $matrix));
    $offset = 0;
    imageconvolution($image, $matrix, $divisor, $offset);
    return $image;

Sharpening Images via Function

function GDThrowError($message) { 
    // don't throw plain text errors in a function that's supposed to return an image 
    // as per "the principle of least astonishment": the user is expecting 
    // a jpeg, and they'll be less astonished to get a JPEG error message 
    // than they would be if they got plain text.  Imagine this was called 
    // from an image tag, which makes sense, that's how you use images: 
    // plain text errors won't even reach most users, they'd have to copy 
    // the img src into the address bar.  It probably wont' even occur to 
    // them, as it's not typically helpful to view the source of an image 
    // resource. 
    $font = 2; 
    // create a canvas with a bit of padding 
    $errimg = imagecreate((imagefontwidth($font) * strlen($message)) + 20, imagefontheight($font) + 10); 
    $bg = imagecolorallocate($errimg, 255, 255, 255); 
    $textcol = imagecolorallocate($errimg, 0, 0, 0); 
    imagestring($errimg, 2, 10, 5, $message, $textcol); 
    header('Content-type: image/jpeg');  

function GDMakeJpegLookLikeCrap($target) { 
    // image dimensions are no longer needed (see below), but getimagesize can do some simple validation 
    if (($dims = @getimagesize($target)) === false || $dims['mime'] != 'image/jpeg') 
        GDThrowError('The file you specified couldn\'t be found or is not a valid jpeg image.  Make sure you spelled it correctly and provided the correct path.'); 
    // the original function creates a new image and resamples the source to it using the same height and width here. 
    // I imagine this was to add future resizing functionality but as it is it does nothing but waste resources 
    $image = imagecreatefromjpeg($target); 
    // don't really know/care what this is.  If you're interested see         
    // try tweaking these three vars for different effects, but there is a sharpening function in the php docs (above links) and it's not a trivial operation 
    $spnMatrix = array( array(-1,-1,-1,), 
    $divisor = 8;  
    $offset = 0;  
    imageconvolution($image, $spnMatrix, $divisor, $offset);  
    // I like to send headers as late as possible to avoid already sent errors and duplicate header content 
    header('Content-type: image/jpeg');  
    imagejpeg($image, null, 100);  

// example call 
$s_image = (isset($_GET['image'])) ? $_GET['image'] : null; 

if (preg_match('/\.(jpg|jpeg)$/i', $s_image)) { 
} else { 
    GDThrowError('Please specify a jpeg file to sharpen in the form: ' . $_SERVER['PHP_SELF'] . '?image=filename.jpg'); 

Convert Image To PGM/Grayscale

function imagepgm($image, $filename = null) {
    $pgm = "P5 ".imagesx($image)." ".imagesy($image)." 255\n";
    for ($y = 0; $y < imagesy($image); $y++) {
        for ($x = 0; $x < imagesx($image); $x++) {
            $colors = imagecolorsforindex($image, imagecolorat($image, $x, $y));
            $pgm .= chr(0.3 * $colors["red"] + 0.59 * $colors["green"] + 0.11 * $colors["blue"]);
    if($filename != null) {
        $fp = fopen($filename, "w");
        fwrite($fp, $pgm);
    } else {
        return $pgm;

Giving Image Sepia Effect

// replace with your files
$originalFileName    = $filename;
$destinationFileName = "2".$filename;

// create a copy of the original image
// works with jpg images
// fell free to adapt to other formats ;)
$fullPath = explode(".",$originalFileName);
$lastIndex = sizeof($fullPath) - 1;
$extension = $fullPath[$lastIndex];
if (preg_match("/jpg|jpeg|JPG|JPEG/", $extension))
    $sourceImage = imagecreatefromjpeg($originalFileName);

// get image dimensions
$img_width  = imageSX($sourceImage);
$img_height = imageSY($sourceImage);

// convert to grayscale
// note: this will NOT affect your original image, unless
// originalFileName and destinationFileName are the same
for ($y = 0; $y <$img_height; $y++) { 
    for ($x = 0; $x <$img_width; $x++) { 
        $rgb = imagecolorat($sourceImage, $x, $y);
        $red   = ($rgb >> 16) & 0xFF; 
        $green = ($rgb >> 8) & 0xFF; $blue = $rgb & 0xFF; //sepia; 
        $red2 = min($red*.393 + $green*.769 + $blue*.189,255); 
        $green2 = min($red*.349 + $green*.686 + $blue*.168,255); 
        $blue2 = min($red*.272 + $green*.534 + $blue*.131,255); // shift gray level to the left $grayR = $red2 << 16;
        // R: red $grayG = $green2 << 8 ;
        // G: green $grayB = $blue2;
        // B: blue // OR operation to compute gray value $grayColor = $grayR | $grayG | $grayB; // set the pixel color imagesetpixel ($sourceImage, $x, $y, $grayColor); imagecolorallocate ($sourceImage, $gray, $gray, $gray);
} // copy pixel values to new file buffer
$destinationImage = ImageCreateTrueColor($img_width, $img_height); 
imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height); // create file on disk imagejpeg($destinationImage, $destinationFileName); // destroy temp image buffers imagedestroy($destinationImage); imagedestroy($sourceImage); 

Image Reflection

function imagereflection($src_img) {
  $src_height = imagesy($src_img);
  $src_width = imagesx($src_img);
  $dest_height = $src_height + ($src_height / 2);
  $dest_width = $src_width;

  $reflected = imagecreatetruecolor($dest_width, $dest_height);
  imagealphablending($reflected, false);
  imagesavealpha($reflected, true);

  imagecopy($reflected, $src_img, 0, 0, 0, 0, $src_width, $src_height);
  $reflection_height = $src_height / 2;
  $alpha_step = 80 / $reflection_height;
  for ($y = 1; $y <= $reflection_height; $y++) {
    for ($x = 0; $x < $dest_width; $x++) {
      // copy pixel from x / $src_height - y to x / $src_height + y
      $rgba = imagecolorat($src_img, $x, $src_height - $y);
      $alpha = ($rgba & 0x7F000000) >> 24; $alpha = max($alpha, 47 + ($y * $alpha_step)); $rgba = imagecolorsforindex($src_img, $rgba); $rgba = imagecolorallocatealpha($reflected, $rgba['red'], $rgba['green'], $rgba['blue'], $alpha); imagesetpixel($reflected, $x, $src_height + $y - 1, $rgba); } } return $reflected; } 

Putting Text Vertically On Images

$string = 'Your Text';
$font_size = 2;
$img = imagecreate(20,90);
$bg = imagecolorallocate($img,225,225,225);
$black = imagecolorallocate($img,0,0,0);

$len = strlen($string);
for ($i=1; $i<=$len; $i++) {
    imagecharup($img, $font_size, 5, imagesy($img)-($i*imagefontwidth($font_size)), $string, $black);
    $string = substr($string,1);
header('Content-type: image/png');

Putting Watermark On Images Watermark can be a png(with transparency) and can be placed anywhere on the image.

function imagelogo (&$dst_image, $src_image, $dst_w, $dst_h, $src_w, $src_h, $position='bottom-left') { 
    if ($position == 'random') { 
        $position = rand(1,8); 
    switch ($position) { 
        case 'top-right': 
        case 'right-top': 
        case 1: 
            imagecopy($dst_image, $src_image, ($dst_w-$src_w), 0, 0, 0, $src_w, $src_h); 
        case 'top-left': 
        case 'left-top': 
        case 2: 
            imagecopy($dst_image, $src_image, 0, 0, 0, 0, $src_w, $src_h); 
        case 'bottom-right': 
        case 'right-bottom': 
        case 3: 
            imagecopy($dst_image, $src_image, ($dst_w-$src_w), ($dst_h-$src_h), 0, 0, $src_w, $src_h); 
        case 'bottom-left': 
        case 'left-bottom': 
        case 4: 
            imagecopy($dst_image, $src_image, 0 , ($dst_h-$src_h), 0, 0, $src_w, $src_h); 
        case 'center': 
        case 5: 
            imagecopy($dst_image, $src_image, (($dst_w/2)-($src_w/2)), (($dst_h/2)-($src_h/2)), 0, 0, $src_w, $src_h); 
        case 'top': 
        case 6: 
            imagecopy($dst_image, $src_image, (($dst_w/2)-($src_w/2)), 0, 0, 0, $src_w, $src_h); 
        case 'bottom': 
        case 7: 
            imagecopy($dst_image, $src_image, (($dst_w/2)-($src_w/2)), ($dst_h-$src_h), 0, 0, $src_w, $src_h); 
        case 'left': 
        case 8: 
            imagecopy($dst_image, $src_image, 0, (($dst_h/2)-($src_h/2)), 0, 0, $src_w, $src_h); 
        case 'right': 
        case 9: 
            imagecopy($dst_image, $src_image, ($dst_w-$src_w), (($dst_h/2)-($src_h/2)), 0, 0, $src_w, $src_h); 

// example: 

imagelogo($image, $watermark, imagesx($image), imagesy($image), imagesx($watermark), imagesy($watermark), 'random'); 

?> {/code}

Make White Background Transparent


function transparentImage($src){  //making images with white bg transparent
for($x = 0; $x < imagesx($src); ++$x)
            for($y = 0; $y < imagesy($src); ++$y)
                $color=imagecolorat($src, $x, $y);
                $r = ($color >> 16) & 0xFF; $g = ($color >> 8) & 0xFF; $b = $color & 0xFF; for($i=0;$i<270;$i++){ if($r.$g.$b==($r1+$i).($g1+$i).($b1+$i)){ $trans_colour = imagecolorallocatealpha($src, 0, 0, 0, 127); imagefill($src, $x, $y, $trans_colour); } } } } return $src; } $image='abc/abc.jpg'; $src = imagecreatefromjpeg($image); $src=transparentImage($src); //Lets make the jpegs transparent ?>

Crop Blank Edges From Image

* $image image cursor (from imagecreatetruecolor) 
* $backgound image curosr (from imagecolorallocate) 
* $paddng int 
function imageCrop($image, $background = false, $padding = 0) { 
      $background = imagecolorallocate($image, 255, 255, 255); 

    $top = imageSY($image); 
    $left = imageSX($image); 
    $bottom = 0; 
    $right = 0; 

    for ($x = 0 ; $x < imagesx($image) ; $x++) { 
        for ($y = 0 ; $y < imagesy($image) ; $y++) { 

          // if there match 
            if(imagecolorat($image, $x, $y) != $background) { 

              if($x < $left) 
                $left = $x; 
              if($x > $right) $right = $x; if($y > $bottom) $bottom = $y; if($y < $top) $top = $y; } } } $right++; $bottom++; // create new image with padding $img = imagecreatetruecolor($right-$left+$padding\*2,$bottom-$top+$padding\*2); // fill the background imagefill($img, 0, 0, $background); // copy imagecopy($img, $image, $padding, $padding, $left, $top, $right-$left, $bottom-$top); // destroy old image cursor imagedestroy($image); return $img; } ?>

Limit File Download Speed (Using PHP)

Yes, using simple PHP snippet you can limit the download speed of a file. This can be done for Bandwidth throttling or for Rate limiting. One practical application of this script will be slowing down the speed at which someone can download a file for free. If you pay for the service then the speed would be either unlimited or less restrictive.
Another application would be if you are generating the file as it is being created. Like, If there is a 10mb lossless image, and you know that it takes at most 1 second to create 256kb of it, then you can set the script to stream 256kb every second. This way the user can start receiving data as soon as the first 256kb are ready.
{code type=php}
// The file that will be sent to the user
$your_file = ‘’;

// Rename the file name
$new_file = ‘’;

// Set the download speed limit (70 kb/s)
$download_speed = 70;

if(file_exists($myl_file) && is_file($my_file)) {

// Headers
header(‘Cache-control: private’);
header(‘Content-Type: application/octet-stream’);
header(‘Content-Length: ‘.filesize($my_file));
header(‘Content-Disposition: filename=’.$new_file);

// Flush the content

// File stream
$file = fopen($my_file, “r”);

while (!feof($file)) {

// Send the current part of the file to the browser
echo fread($file, round($download_speed* 1024));

// Flush the content to the browser

// Sleep one second

// Close file stream

else {
die(‘Error: The file ‘.$my_file.’ does not exist!’);
This script can be used to limit the download speed at which one can download the file. You can also try out QoS Bandwidth Throttle in PHP.