Roshan Book

My Tech Notebook

Category Archives: Programming

Creating a TinyURL with TinyURL’s API


function createTinyUrl($strURL) {
    $tinyurl = file_get_contents("".$strURL);
    return $tinyurl;
To call this function

How To Create Your Own Tinyurl Style URL Shortener

A URL shortener is a service that takes in a long URL and  gives you a short and memorable URL that you can distribute to anyone you want. The service takes care of redirecting users who land up at the shortened URL to the original URL.

TinyURL, of course, was one of the pioneers in the field but ever since Micro blogging sites like Twitter started to take off, a number of services have come up that all provide essentially the same URL shortening service.

One major problem with using a third-party URL shortening service is that all your links are always bound to that one service and if they ever go out of business, like did, all your data is gone too. And that is why, if you’re serious about saving all the wonderful links that you’ve discovered from around the web, you might want to consider running your own URL shortening service.

YOURLS gives you exactly that.


First of all, you need to download YOURLS to your web server and unzip the package.


Now, use your favorite tool to create a mysql database for YOURLS. If you’re using a shared hosting provider, there must be a way to do this from inside your domain control panel. If not, just open a ticket with your hosting provider and let them handle it for you. I named my database ‘kunz_yourls‘.

Now, browse over to the includes directory and rename the file named ‘config-sample.php’ to ‘config.php’.

Open ‘config.php’ in a text editor and edit the following variables.


The comments in the file are pretty self explanatory and you can also get an overview here.


Now, from your web browser, visit the site http://<website>/admin, where website is the domain name where you installed YOURLS. Click the “Install YOURLS” button.

If all went well, you will see the following message which means that YOURLS was successfully installed on your domain.


Start creating your own Short URLs

Browse over to http://<website>/admin/index.php and login using the username/password combination that you entered earlier in config.php. If you didn’t enter any, try username/password. That’s the default combination and that is why you should change it when you’re installing YOURLS.

Upon login, you’ll be able to create short URLs easily by just entering the complete URL in the prominently displayed text box and clicking “Shorten the URL” button.


You can even get some nice and simple statistics about the short URLs that you’ve created.


Be sure to check out the bundled tools. You get two cool bookmarklets that you can use to create short URLs right from your browser toolbar.

YOURLS also provides an API that you can take advantage of in custom applications and such.

Did you find YOURLS useful? Will you use it over, say, Let us know in the comments.

Redirecting non-www to www with .htaccess

Source :

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This will redirect any requests to to There are several benefits from doing that:

  • It will avoid duplicate content in Google
  • It will avoid the possibility of split page rank and/or split link popularity (inbound links).
  • It’s nicer, and more consistent.

Note that if your site has already been indexed by Google without the www, this might cause unwanted side effects, like lost of PR. I don’t think this would happen, or in any case it would be a temporary issue (we are doing a permanent redirect, 301, so Google should transfer all rankings to the www version). But anyway, use at your own risk!

Something nice about the code above is that you can use it for any website, since it doesn’t include the actual domain name.

Redirecting www to non-www

If you want to do the opposite, the code is very similar:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

In this case we are explicitly typing the domain name. I’m sure it’s possible to do it in a generic way, but I haven’t had the time to work one out and test it. So remember to change ‘my-domain’ with your domain name!

Transform Gedit Into A Web Developer IDE


Gedit is the default text-editor in Gnome based distro. To many, it may seems like a normal text-editor with no special functions (just like notepad in Windows), but for those regular users, they will swear by it and marvel at how powerful and versatile it can be. With its plugins system, it can easily transform itself from a basic text-editor to complicated and powerful programming IDE. Here’s how you can do it.

NoteFor this tutorial, I will focus mainly on HTML, PHP and CSS since that are the web programming languages I used. You can modify it to suit your needs.

First, install the extra set of gedit plugins In your system.

sudo apt-get install gedit-plugins

This will install the following plugins:

  • Commander: Command line interface for advanced editing
  • Color Picker: Pick a color from a dialog and insert its hexadecimal representation.
  • Character Map: Insert special characters just by clicking on them.
  • Draw Spaces: Draw Spaces and Tabs
  • Session Saver: Save and restore your working sessions
  • Word Completion: Word completion using the completion framework
  • Multi Edit: Edit document in multiple places at once
  • Code comment: Comment out or uncomment a selected block of code.
  • Bracket Completion: Automatically adds closing brackets.
  • Show/Hide Tabbar: Add a menu entry to show/hide the tabbar.
  • Smart Spaces: Forget you’re not using tabulations.
  • Embedded Terminal: Embed a terminal in the bottom pane.
  • Bookmarks: Easy document navigation with bookmarks
  • Join/Split Lines: Join several lines or split long ones

You won’t need all of them, but it is useful to have them readily available.

Install gedit-Gmate

Gmate is an additional set of plugin for gedit to make it more similar to TextMate. It contains code snippets, plugins, and an automatic registration of rails-related files.

To install Gmate:

sudo apt-add-repository ppa:ubuntu-on-rails/ppa
sudo apt-get update
sudo apt-get install gedit-gmate exuberant-ctags

Activating the plugins

Now that you have installed the extra plugins, it is time to activate and configure the necessary plugins. Open gedit and go to “Edit -> Preferences“. Click on the “Plugins” tab.


Scroll down the list and select the following plugins:

  • bracket completion – Automatically add closing brackets
  • class browser – show all the functions and tags for the current file.
  • color picker
  • File browser pane – easy file access from the side pane
  • Find in File / Find in Project
  • Session Saver
  • Smart indent
  • TextStyle autocompletion – Press “ESC” to autocomplete
  • Tag list – insert commonly used tags
  • Word Completion

Note: The above plugins are the one that I used. You are free to activate/deactivate any other plugins.

Once you have activated the plugins, your gedit should now be equipped with syntax highlighting, auto-completion, tag list for quick insertion, color picker, class browser that display all your functions, file browser, bracket balancing etc.

Installing extra tag list

The default tag list only comes with support for HTML and XSL. To add support for PHP, CSS and xHTML, we need to install additional set of tag list.

Download gedit_webdev_tags-0.1.tar.gz. Extract the content into a folder “taglist” in your Home directory. Open a terminal:

cd taglist
sudo cp *.tags /usr/share/gedit-2/plugins/taglist

Open your gedit, you should be able to see the tag list for CSS, PHP and xHTMLnow.


Install Clientside

Clientside plugin for gedit is an useful plugin for web developer who deals with CSS and Javascript. It can minify and beautify javascript and CSS. It also includes jsLint and has option to gzip the current file.

First install the prerequisites:

sudo apt-get install nodejs php5-cli

Download the ClientSide zip file.


Extract the zip file. Copy the “clientside.gedit-plugin” file and the “clientside” folder to “/home/USERNAME/.gnome2/gedit/plugins”. If the “plugins” folder does not exist, create it.

Go to your gedit. In the plugin section, activate the clientside plugin. Click on the Configure Plugin button. Enter the following:

How do I call NodeJS?: nodejs
How do I call PHP?: php


Configuring FTP access

You can directly transfer your files to your server via FTP without a FTP client.

Open Nautilus File Manager and go to “File -> Connect to Server”. Connect to your FTP server. Remember to check the box “Add bookmark”.


In your gedit, open your side pane and go to the file browser. At the dropdown field, select “Bookmarks”. You should see the FTP server bookmark. You can click on it to connect to your server.


That’s it. Your gedit should be a fully functional web developing IDE now.

Title bar gone – Linux Mint 13 xfce

Here is Roshanbook’s annual report

The stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

4,329 films were submitted to the 2012 Cannes Film Festival. This blog had 25,000 views in 2012. If each view were a film, this blog would power 6 Film Festivals

Click here to see the complete report.

Learning Ruby Part 11 Looping

4.times {puts “manish”} – > prints manish 4 times

3.upto<7> {puts “manish”} – > Prints manish counting 3 to 7

15.downto<12>{puts “manish”} – > Prints manish from 15 upto 12. That is 4 times

0.step<15,5> {puts “manish”} – > Prints  manish 4 times. It steps by 5 , rather than 1. Counting starts from 0

1.upto<10> {|variable| puts variable} –> It prints the value inside variable and print it out like



3 to 10



Learning Ruby Part 9 Expressions





It gives 9

Assigning value of variable to variable



Now if we change value of d to 3, f still has value 33.





Same thing could be written as


Same thing could be done with –, *, /

Learning Ruby Part 8–Variables

We can create Methods that don’t belong to any class.

Declaring a common method

def crap

puts “I am a crappy crap”


Calling Method directly



Adding value to variable



Few things to keep in mind

A variable should start with letters or _ .

No spaces allowed

No symbols like %

Variables are case sensitive

Learning Ruby Part 7–Strings

String means bunch of text like “my name is manish”


Declaring a string

My name is Manish

Methods of Strings

.length  – >Returns number of characters. It includes space

.upcase –> Converts everything to uppercase

+ –> It adds two strings

.reverse-> It reverse the strings

.reverse.upcae – > Here we added two methods for a string. It will reverse and change string to uppercase

Full list of string methods can be found here

%d bloggers like this: