Sep 28

Get List of Last Changed Stored Procs in SQL Server

Sometimes when you have created/updated several stored procs in your database in SQL Server and you only need to transfer the changed ones to another copy of the database, then the best way is to get a listing of the stored procs by date.

 

select name, modify_date from sys.procedures order by modify_date desc

This saves you the whole trouble of remembering which stored procs were added or updated of late.

Sep 26

IIS Folder Permissions not inherited by files

Generally, when you assign specific access rights for particular Windows users to a web folder in IIS, you expect files created in that folder to automatically inherit those permissions. This work as long as the files are created directly in that folder. For eg.if you have an uploads folder where you upload files via FTP.

But it does not work in cases where files are not directly created in that folder. The best example being uploads. ASP.NET uploads are not directly created in the target folder; they generally get created in the Windows temp folder and then get moved to the destination folder. (A lot depends on the upload component used of course).

In such a case, files do not automatically inherit the permission of the destination folder they are copied to. They inherit the permission of the folders in which they are created. So if the temp folders does not allow anonymous access, then a file moved from the folder to eg. the uploads folder, will not allow itself to be accessed via the web anonymously. Most of the times accessing an uploaded file via a web page throws up a Authentication dialog box from the browser.

The  way around this is to set anonymous permissions to the temp folder (or whatever folder the file is getting created in first).

So if your IIS has a Guest user account named IUSR_XXXX then IUSR_XXX needs to have permissions on both the temp folder and the destination uploads folder.

Permissions

IMPORTANT: It is best not to fiddle around with default settings of the Windows temp folder. Better to create your own temp folder which will be used by your upload component.

 

 

Sep 14

Import Large data file into MySQL

How to import a large sql file into mysql when phpmyadmin has hit it’s 5 or so megabyte limit?

This is a common problem  so I will place this up here to make it easier to find.

  • Create the destination database in MySQL
  • Put the .sql file into a convenient location (the MySQL /bin directory would be perfect w/XAMPP)
  • login to MySQl
  • SOURCE sqlfilename (if you put it outside mysql bin, you probably need the full path
Sep 09

PHP is loved by many for reasons you wouldnt have thought of.

The age-old debate between PHP versus ASP.NET is not really what I am trying to get into here. I am also not trying to talk about “PHP being faster” or “PHP being free” and “85% of the webservers run Linux and so Apache and PHP come built in anyway” concepts.

I am seeing this from a programmer’s point of view. I have worked with many languages right from 8086 assembly, c, C++, c# to asp, ASP.NET, PHP etc. I think I have gained enough insight to see why a language becomes popular – its all about programmer psychology.

The reason why PHP is so popular is actually the reason why sometimes its looked down upon:

 

Its a collection of separate functions and modules

with no common thread linking them.

 

That is precisely why PHP is so popular.

Let me explain.  PHP is mostly used for web applications though it can be used for console applications as well, but I still think 99% of the world uses PHP for creating websites. It is not a framework like ASP.NET per se and that is why it lets you be sloppy and start off with a blank page and banging out code. (Try doing that in ASP.NET) .It has functions for nearly every damn thing you would ever need to create applications right from string functions to parsing text to file handling to regular expressions or mathematical functions.

What it says is “hey, I am not forcing you to be disciplined in your code – that is your responsibility but I do have everything you need. Feel free to use it as you need“.

That is what makes it so appealing. As a programmer I can follow any structure or architecture I want. PHP gives me the building blocks to create my own structure. It does not force me into a particular model.

It is also very forgiving.It wont crash the website if it encounters an error unless its a very serious resource linked error.

It lets you play around with code without too much restrictions. It allows quick and dirty coding.

This is precisely what programmers need – a language which lets you run amok while providing you a safety net and also having all the building blocks in place. Something like C minus the risks .

I have not really worked in Ruby or Python but most probably they also provide something similar. Any language which gets popular will certainly have the above features.

At the other extreme is COBOL which no programmer ever felt passionate about. It robs you of all freedom. Writing code in COBOL is like filling up a tax form – you dont have any choice. You just have to follow a strict outline .

I welcome comments on other languages

 

 

May 04

Too many CKeditor fields in jquery tabs makes IE freeze.



IE seems to freeze or take a long time to render a page if there are more than 2 or 3 CKeditor textareas in the page. This problem is specific to IE as Firefox, Chrome and Safari do not seem to have this problem.

The basic reason for this is that IE gets stuck in instantiating the ckeditor javascript object; maybe something in the ckeditor script creates this problem.

We had this particular problem we used a JQuery tab plugin with about 8 or more tabs and each tab had a textarea field wrapped in a ckeditor object.

The final solution that was used is simple: Dont instantiate all the ckeditor objects at page load time. Instantiate for each textarea when required. In other words, let the ckeditor object get created at page load for the default tab. For all the other tabs, convert the textarea in the target tab into a ckeditor when the tab is clicked. This was a simple solution which stopped IE from freezing and also sped up the page load time.

The code snippets are given below. It is assumed that jquery libraries and the ckeditor scripts are included in the page.

The HTML part is given below. The fields are using PHP code to obtain values, but that part can be ignored. Note that the default Homepage tab is already set to class=ckeditor so it becomes a ckeditor object at page load. The other textareas are set to a dummy class called “xckeditor”:

	<tr valign="top">
				<td colspan="2">
					<div id="tabs">
					<ul>
						<li><a href="#tabs-1">Homepage</a></li>
						<li><a href="#tabs-2">About Us</a></li>
						<li><a href="#tabs-3">Privacy</a></li>
						<li><a href="#tabs-4">Terms</a></li>
						<li><a href="#tabs-5">Advertise</a></li>
						<li><a href="#tabs-6">Partner</a></li>
						<li><a href="#tabs-7">Competition</a></li>
						<li><a href="#tabs-8">Bookmark</a></li>
						<li><a href="#tabs-9">Daily Deals Newsletter</a></li>
						<li><a href="#tabs-10">FAQ</a></li>
					</ul>

					<div id="tabs-1">
						<div class="editor_adjust">
							<textarea name="txtHomepageContent" id="txtHomepageContent"  rows="10" cols="40" class="ckeditor"><?php echo($sitecountry->getValue("homepage_content"));?>

The javascript is given below. Note that there is no code to make a textarea into a plain textarea by removing the ckeditor class. This was not required since once a textarea was made into a ckeditor object it was not affecting the page speed:

jQuery(document).ready(function($) {
	$("#tabs").tabs({
		event: 'click'
	}),
	$( "#tabs" ).tabs({
	   select: function(event, ui) {
		   var i = ui.index;

		   if (i == 1) {
			   $('#txtAboutUsContent').removeClass("xckeditor");
			   $('#txtAboutUsContent').ckeditor();
		   }
		   else if (i == 2) {
			   $('#txtPrivacyContent').removeClass("xckeditor");
			   $('#txtPrivacyContent').ckeditor();
		   }
		   else if (i == 3) {
			   $('#txtTermsContent').removeClass("xckeditor");
			   $('#txtTermsContent').ckeditor();
		   }
		   else if (i == 4) {
			   $('#txtAdvertiseContent').removeClass("xckeditor");
			   $('#txtAdvertiseContent').ckeditor();
		   }
		   else if (i == 5) {
			   $('#txtPartnerContent').removeClass("xckeditor");
			   $('#txtPartnerContent').ckeditor();
		   }
		   else if (i == 6) {
			   $('#txtCompetitionContent').removeClass("xckeditor");
			   $('#txtCompetitionContent').ckeditor();
		   }
		   else if (i == 7) {
			   $('#txtBookmarkContent').removeClass("xckeditor");
			   $('#txtBookmarkContent').ckeditor();
		   }
		   else if (i == 8) {
			   $('#txtNewsletterContent').removeClass("xckeditor");
			   $('#txtNewsletterContent').ckeditor();
		   }
		   else if (i == 9) {
			   $('#txtFaqContent').removeClass("xckeditor");
			   $('#txtFaqContent').ckeditor();
		   }
	   }
	});	

});

A good enhancement would have been to place a check if a textarea was already assigned the ckeditor class before running the

Mar 27

A very efficient way of instantiating a class dynamically in PHP



Since PHP is a loosely typed language, it provides great flexibility in creating variables and objects at runtime without having to specifically declare them first. It is well known that $i=10, $i=”test”, $i = date() will all work without creating any syntax or compile errors.

A lesser used concept is that this feature can be used to create an object at runtime without having to check for the object type at first. Eg.if you have had 3 classes and only one of them had to be created depending on a condition the following would be the sample code:

<?php
	error_reporting(E_ALL);

	class Generic {
		function xprint() {
			echo("generic");
		}
	}
	class A extends Generic{
		function xprint() {
			echo("A");
		}
	}

	class B extends Generic{
		function xprint() {
			echo ("B");
		}
	}

	class C extends Generic{
		function xprint() {
			echo("C");
		}
	}

	$test = "B";
	if ($test =="B")
		$x = new B();
	else if ($test == "A"
		$x = new A();
    else if ($test == "C")
	    $x = new C();
	$x->xprint();
	echo("end");
?>

Instead of doing multiple checks and then creating the relevant class, a more efficient way is to pass the classname as a string and then create it directly as shown below:

<?php
	error_reporting(E_ALL);

	class Generic {
		function xprint() {
			echo("generic");
		}
	}
	class A extends Generic{
		function xprint() {
			echo("A");
		}
	}

	class B extends Generic{
		function xprint() {
			echo ("B");
		}
	}

	class C extends Generic{
		function xprint() {
			echo("C");
		}
	}

	$test = "B";
	$x = new $test();
	$x->xprint();
	echo("end");
?>

The above code will print “B” since it creates the class B in $x. What about classes where arguments are required in the constructor? Simple call it with the arguments eg. $x = new $test(“arg1″, “arg2″);

Nov 22

Handling special symbols in csv file in PHP



PHP provides the fgetcsv function to automatically import values from a csv file. However it does not consistently work with cases where the starting of a field value has an extended character like the pound sign or a foreign character. In such cases, fgetcsv simply omits that first character and gets the remaining data of the column.

Eg.”Buyagift.co.uk”,”273″,”xmas1075″,”£10 off when you spend over £75 on all products excluding our amazing special offers”

will be imported as

“Buyagift.co.uk”,”273″,”xmas1075″,”10 off when you spend over £75 on all products excluding our amazing special offers”

One officially recommended way of handling this is to set the locale before running fgetcsv . But somehow this does not always work. After trying various approaches we have come up with our own solution which works in all cases.

We do our own preprocessing of the csv file before passing it to fgetcsv(). Our approach is to detect any character following a comma which is beyond the ascii printable range of codes from 32 to 126. If it detects any such character it precedes it with a slash. The complete  data thus updated is written back to the file and then passed to fgetcsv().

So “Buyagift.co.uk”,”273″,”xmas1075″,”£10 off when you spend over £75 on all products excluding our amazing special offers”

will become “Buyagift.co.uk”,”273″,”xmas1075″,”£10 off when you spend over £75 on all products excluding our amazing special offers”

At a later stage the once the data array has been obtained from fgetcsv, we remove the preceding slash by running stripcslashes() to the value.

The code fragment is given below:

$f = fopen(“myfile.csv”, “r”);
if ($f) {
$data = fread($f, filesize(“myfile.csv”));
fclose($f);
$arr = explode(“n”, $data);
$newData = “”;
for($i = 0; $i < count($arr); $i++) {
$item = $arr[$i];
if (strlen($item) > 2) {
$x = explode(“,”,$item);
for($j = 0; $j < count($x); $j++)
{
if (ord($x[$j]) > 126)
{
$x[$j] = “\”.$x[$j];
}
}
$arr[$i] = implode(“,”,$x);
$item = $arr[$i];
$newData .= $item . “rn”;
}

}
}

// rewrite processed data back to file
$f = fopen(“myfile.csv”, “w”);
if ($f) {
fwrite($f, $newData);
fclose($f);
}
else
echo(“Unable to create file:”);

Handling The Euro Symbol

The above approach does not work for the Euro symbol since it does not get correctly interpreted even if a file is open in utf-8 mode. For this, the solution to open the file and simply replace the euro symbol with its html entity equiv: & html ; (remove the spaces in between).

Eg. $data = str_replace(“€”,”&euro;”,$data);

Sep 01

Installing PEAR for PHP



pearsmall

Installation

For Windows:

  • To update your PEAR installation, request http://pear.php.net/go-pear in your browser and save the output to a local file go-pear.php.
  • Assuming you have installed PHP already, and it is runnable from the command line type php go-pear.php

For FreeBSD:

  • Add the PEAR DB package from the ports /usr/ports/databases/pear-DB

Test Installation

To make sure that PEAR is working simply type in pear at the command prompt. If nothing shows then check that the PEAR path is part of your system path. Check the version of PEAR by typing pear version

To use PEAR and PEAR compatible packages in your applications, you normally include them into your PHP scripts using require_once().  For this to work, PEAR’s php_dir must be a part of PHP’s include path.

First check where PEAR installs php files by typing

pear config-get php_dir

Whatever the folder is, this folder will contain System.php

To find which configuration file is used by your PHP installation. On command line, execute php –ini

When you execute a phpinfo() in the browser it should always the show the PEAR path in its include path for the PEAR packages to work.




<em></em>


Jul 30

Installing VSFTPD



1.Install it from the ports

cd /usr/ports/ftp/vsftpd

make config

make install clean

2.Edit the vstfpd.conf file in /usr/local/etc/vsftpd.conf to customize the settings

3.To run it from the command line:

/usr/local/libexec/vsftpd &

3.To make it run at startup create an executable file in /etc/rc.d

#! /bin/sh

#

/usr/local/libexec/vsftpd &

4.Restart computer