Super simple way to expand bitly urls using PHP

OK. Agree that provides an API for expanding URL’s. The main purpose of our simple exercise here is to use  curl functions in PHP to achieve the same. We will write a simple function that will accept a bitly url as parameter. Then we will use curl to get the expanded url.

Below is the full code for function which we will call expandURL.

	function expandURL($url)
		$retVal = 'Error';
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		if(curl_exec($ch) != false)
			$response = curl_exec($ch);
			if($response != false)
				$responseInfo = curl_getinfo($ch);
				if($responseInfo['http_code'] == 200)
					$retVal =  'Expanded URL: '.$responseInfo['url'];
				else if($responseInfo['http_code'] == 404)
					$retVal =  'URL Not found';
					$retVal =  'HTTP error: '.$responseInfo['http_code'];
				$retVal =  curl_error($ch);
			$retVal =  'cURL error ocurred : '.curl_error($ch);
		return $retVal;

	$result = expandURL('');
	echo $result;

How this works?

$retVal is the return value from function. First we initialize a curl session for bitly url using curl_init. Next 2 lines are most important.

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Since our aim is not to display any output directly in browser, we will set CURLOPT_RETURNTRANSFER to 1 (true). It will  return the value after curl execution in a string and will not output anything to browser.

Continue reading Super simple way to expand bitly urls using PHP

5 JSON resources for web developers

JSON(Javascript Object Notation) is defined as a lightweight data interchange format and fat-free lightweight alternative to xml. Since json is the native data form of Javascript, it can be used on the browser more easily then XML.

If you are a web developer who works with javascript/ajax a lot and still wondering what this json thing is, here is the first step to start with. Read this post about json and its uses.

Below is a list of 5 resources from where you can learn about json in detail.


First place to go to find definitive information. Douglas crockford is the man who invented json. is a one stop place for everything json.

2- Mastering JSON

Detailed examples as well as retrieving and sending json data between browser and server. You cam also find tips on security and best practices.

3- JSON vs XML debate

Arguments are going on between which is better to use as a data format. Go read and judge yourself.

4- JSON with Ajax and PHP

5- Slide on JSON

FAQ(Javascript) : User confirmation on close button of browser

Problem: On clicking the close button of browser ask for user confirmation with a custom message

Solution: Use the window.onbeforeunload method. This method expects a string value. This string will be displayed to user along with the default string. Default string that is displayed to user is:

Are you sure you want to navigate away from this page?
Press OK to continue, or Cancel to stay on the current page.

You can see an example in action here. Full code is given below.

<script type="text/javascript">
  window.onbeforeunload = function()
     return 'Please do not leave this page.'; 
alternatively, you can also write the following
this will also work the same as above 
window.onbeforeunload = confirmFromUser;
function confirmFromUser()
	return 'Please do not leave this page.';

	Try reloading this page or closing your browser. <br/>
        Also try back, forward buttons.
	<a href="">Click me</a>

onbeforeunload event will fire on reloading the page, using browser’s back/forward button or even on clicking an anchor that navigates user from current page to any other location.

PHP – Turn register globals on off using htaccess


My application is hosted on a server where php has register_globals set to on in php.ini. But using register_globals = on makes code error prone and is dangerous to use.
I want to turn it of for my application regardless of what is its setting for other applications.
Since the value of register_globals cannot be set at run time bu using ini_set, how can I do it?


Use htaccess file to control this behaviour. Create a file named .htaccess in root directory of your application and write the following line in it:

php_flag register_globals off

Thats all you need to do.

You can verify this by creating a phpinfo() file. Suppose your server has register_globals set to on and you have turned it off using above syntax.

phpinfo() will show the local value as off and master value as on.

Note: Of course, this will work only if your apache configuration allows to override htaccess settings.

FAQ: How to fix error 403: access forbidden for phpmyadmin in wampserver?


I am getting a large number of response that steps mentioned below are not working. It might be a firewall issue as well. Therefore, first and foremost, check that the port you are using for accessing phpmyadmin is not blocked by Windows firewall. Proceed below once you are sure of it.

The Problem:

PhpMyadmin works fine on localhost but accessing it over a network gives error 403: Access Forbidden.


Reason is pretty clear – phpmyadmin is not configured to be accessed over a network. Permission to access it over network is not given.


Locate the file phpmyadmin.conf in your wamp installation. If you have wampserver version 2 or higher, this file can be found at C:\wamp\alias\phpmyadmin.conf (Of course assuming that you installed wamp in C:\wamp). For earlier wamp versions this file is at C:\wamp\Apache2\conf\alias\phpmyadmin.conf.
Once found,open this file. Contents of the file will be like the following:

Alias /phpmyadmin "c:/wamp/apps/phpmyadmin3.1.3.1/"
# to give access to phpmyadmin from outside
# replace the lines
#        Order Deny,Allow
#	Deny from all
#	Allow from
# by
#        Order Allow,Deny
#   Allow from all
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Deny,Allow
	Deny from all
	Allow from

As it is clearly written in the file itself, replace the line Deny from all (second line from the bottom )with Allow from all. Save the file, restart apache and you are done.

PhpMyadmin can be accessed from outside now.