Tag Archives: php

Installing MSSQL driver for PHP on Ubuntu

The driver used is FreeTDS, TDS stands for Tabular Data Stream which is an application layer protocol, used to transfer data between a database server and a client. It was initially developed by Sybase Inc. (that’s where the name of the package comes from, for those curious) and later by Microsoft for their relational SQL databases.

In most cases this should work:

sudo apt-get install php5-sybase freetds-common libsybdb5

but if it doesn’t, you probably need to change the TDS version that is in use by the driver. The configuration file is named freetds.conf, and should be in your /etc/ folder.

There are 2 ways for this:

  • Change the global settings, uncomment the global version and set it to the desired one, e.g.
    • tds version = 8.0
  • Or make a new server and set the version explicitly
    • [exampleServer]
      host = server.com
      port = 1433 (this is default MSSQL port)
      tds version = 7.0
If you picked the latter way, then just use exampleServer as the host, e.g.
mssql_connect("exampleServer\\SQLEXPRESS", "user", "password");

Ow, if it still isn’t working, then you have probably forgotten to allow remote connections on your server. From the start menu find and open “SQL Server Surface Area Configuration” then “Surface Area Configuration for Services and Connections”. Find your instance, expand the “Database Engine” tab, select “Remote Connections” and choose the “Using both TCP/IP and Named Pipes” option. After applying, you must restart your instance for the settings to take effect.

For info on TDS versions used in specific MSSQL version you can go here, or just use the direct link and download [MS-TDS].pdf and go to Appendix A. (or just use the newest version, think it’s 8.0 at the moment, it should work fine)

Tagged , , , ,

How to setup LDAP Server on Ubuntu

LDAP (Lightweight Directory Access Protocol), in short, is an application protocol used to lookup for information on a server. As far as I figured, it is used mostly for user authorization or (e-mail) address books. The Wikipedia article on LDAP is quite good, so you can get basic info/ideas there.

Continue reading

Tagged , , , , ,

Generate QR Code for URLs using Google API

Since I want to implement QR codes into the URL shortener, I wrote a function in PHP that generates the link usin’ Google API.

 


/**
 * Class for QR Codes 
 *
 */
class QR {
	/**
	 * Function returns URL to Google API QR Generated image
	 * 
	 * @param url - The URL to be turned to QR Code
	 * @param size - Size of the QR image
	 * @param errorCorrection - Amount of reduntant information the QR code has (L - default, M, Q, H)
	 * @return Google API QR Generated image (to be used directly in img tag)
	 */
	public static function getQRforURL($url, $size, $errorCorrection = "L") {
		$link = "https://chart.googleapis.com/chart?" . 
				"cht=qr" . "&" .
				"chs=$size" . "x$size" . "&" .
				"chl=" . urlencode($url) . "&" .
				"choe=UTF-8" . "&" .
				"chld=$errorCorrection";
		return $link;
	}
}

Tagged , ,

Singleton Pattern


Design patterns are reusable solutions to common software engineering problems. They are best-practice in software design. It is not easy to apply them, and it often happens that pattern misuse may lead to bigger problems, a good design supported by patterns will last and give you less headaches. Examples in PHP and Java are given.

Singleton is a creational pattern (creational patterns deal with object creation mechanisms) which restricts instantiation of a class to one (and only one) object. We’ll discuss it in 3 chapters:

Tagged , , ,

Number converter

Since it’s needed for URL Shorteners, and that’s what I’m writing a tutorial about, here’s a number converter from decimal, to whatever base you need (if you supply a base array long enough). Written in PHP.

Update (2011-07-27):

Changed method to static and added an exception.

/**
 * Class with static methods for converting numbers from decimal to a given base number system
 *
 */
class NumberConverter {
	/**
	 * Array to be used for conversion
	 */ 
	private static $base = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+=";

	/**
	 * Get's the $number character from $base array 
	 * 
	 * @param number - the position of the character needed
	 * @return Returns a character from $base for a given $number 
	 */
	private static function getCharacter($number) {
		return self::$base{$number};
	}

	/**
	 * Converts a decimal number to a given base 
	 * 
	 * @param number - Number to be converted
	 * @param toBase - Number base to be converted to
	 * @throws Exception
	 */
	public static function fromDecimalToBase($number, $toBase){

		// Check if the base is greater then the max possible base
		if ($toBase > strlen(self::$base)) {
			throw new Exception('Base too big.');
		}
		// Convert number to integer (better safe than sorry)
		$number = intval($number);

		// Set a flag if the number is negative, do a positive conversion then just add the minus sign
		$negative = false;
		if ($number <= 0) {
			$negative = true;
			$number = $number * -1;
		}

		$converted = "";
		while ($number != 0) {
			// Get a number in the new base
			$modulus = $number % $toBase;
			// Get the number representation in that base
			$converted = self::getCharacter($modulus) . $converted;
			// Remove the extracted number and continue until the number is 0
			$number = intval(($number - $modulus) / $toBase);
		}

		// Add the minus sign if it was negative
		if ($negative) {
			$converted = "-" . $converted;
		}
		 
		return $converted;

	}
}
Tagged , ,