Friday

Installing MS SQL Server Client Library on Linux/Unix

Slashdot

Problem: Installing SQL Server client library on Debian/Ubuntu Linux.

If you want to install PHP on a Linux machine and want to use Sql Server as your database then you have to install FreeTDS library on your server machine so that you could make your PHP/Apache server machine talk to your SQL Server 2000 Database. Let's go through the installation steps. Before we proceed please read the disclaimer below.

DISCLAIMER: I have made every attempt to present accurate information, but I cannot guarantee that inaccuracies will not occur. I (the author) will not be held responsible for any claim, loss, damage or inconvenience caused as a result of any information found on this website (blog).

I have used the instructions on Debian/Ubuntu server machines. Make sure you have GNU compilation tools and libraries.

In Debian/Ubuntu, try the following:
gcc -v
make -v

If you see the version numbers of the above two software that means gcc and make are installed on your computer. If you dont have these software installed then you have to first install them.

In Ubuntu, do the following
sudo apt-get install build-essential

This will install the necessary software on your computer. I'd recommend using wget FTP client to download the software on Linux/Unix machines.
wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz

The above command will download the software. Now, unzip and untar the downloaded gzipped tar file by executing the command below.

tar –xvzf freetds-stable.tgz

Enter the folder where you untarred the downloaded file.
cd freetds-stable

Configure and compile the software as follows (make sure you execute 'make install' command below as root):
./configure --prefix=/usr/local/freetds -- enable-msdblib
make
# make install
cd ..

FreeTDS has been installed in the folder /usr/local/freetds. Now, if you want to install PHP then you have to configure PHP (while installing) using the switch as follows:
--with-mssql=/usr/local/freetds


I hope this will be a greate help for you. Please let me know if you want me to write on something else.

Labels: , , , , ,

Monday

Simple Coding Style for PHP

Slashdot


Many programmers use different kinds of coding styles to write code I have my own version that (i think) is simple and easy-to-use. Before we proceed, please read the disclaimer below.

DISCLAIMER: I have made every attempt to present accurate information, but I cannot guarantee that inaccuracies will not occur. I (the author) will not be held responsible for any claim, loss, damage or inconvenience caused as a result of any information found on this website (blog).


Code Formatting and Layout:

Code Formatting and Layout – which includes indentation, line length, use of white space, and use of Structured Query Language (SQL).

Use Soft Tabs (soft tabs are not actually tabs each soft tab is represented by a certain number of regular spaces) instead of Hard Tabs (regular tab). The reason is that tab spacing is different for different editors. Tab width of five spaces is good.
Try to break up lines after every 80 characters because 80 characters is the standard width of a Unix terminal and is a reasonable width for printing to hard copy in a readable font.

Some examples:

if ($month == ‘september’ || $month == ‘april’ || $month == ‘june’ ||
$month == ‘november’) {
return 30;
}

This should be changed to following:

if ($month == ‘september’ ||
$month == ‘april’ ||
$month == ‘june’ ||
$month == ‘november’)
{
return 30;
}

This method works equally well for functions’ parameters:

mail(email@companyname.com,
“subject”,
$body,
“from: \r\n”);

SQL Guidelines:
We’ll follow the following rules:
  • Capitalize keywords
  • Break lines on keywords
  • Use table aliases to keep the code clean

Example:
Consider the following query:

$query = “select firstname, lastname from employees,
departments where employees.dept_id = department.dept_id
and department.name = ‘Arts’”;


Above should be changed to following:


$query = “SELECT firstname, lastname
FROM employees e, departments d
WHERE e.dept_id = d.dept_id
AND d.name = ‘Arts’”;



Using braces in control structures (e.g. if, for, foreach, while):
To avoid confusion, we’ll always use braces, even when only a single statement is being conditionally executed.

if( isset ($name))
echo “hello $name”;

Must be changed to following:

if( isset ($name))
{
echo “hello $name”;
}

Consistency in using braces:
We’ll use BSD style braces for conditional statements. In BSD style braces the braces are placed on the line following the conditional with the braces outdented to align with the keyword. Follow the same rule for functions.

if ($conditional)
{
//statement
}

Some more guidelines:
  • “foreach” is better than “for” which is better than “while”.
  • Never use nonsense names for variables e.g. foo, sz, var and etc.
  • Try not to use global variables but if for some reason use them then all global variables and constants will be represented by capital letters e.g. $CACHE_PATH, $FILE_NAME and etc.
  • Try making classes where possible.
  • Long-lived variable (not necessarily a global, or even in the main scope; it is simply a variable that is used through any significant length of code and/or whose representation can use clarification) should have concise but descriptive names.
  • Temporary variables should be short and concise. Numeric variables used for iteration should always be named i,j,k,l,m, and n.
  • We’re not going to use tags like the following . We’ll be using or <script language="”php”"> </script> (work in Front Page too).
  • Use echo not print.
  • We’ll be using C and C++ style comments i.e. “//” and “/* */”. We won’t use “#”.
  • Comments like the following are useless so avoid using them.
// Increment i
$i++;

  • Using phpdoc for documentation.
  • For multiword names for variables, use underscores to break words.


$num_elements = count($elements);

  • Function Names: They should be all lowercase, and multiword names should be separated by underscores. e.g.



function print_hello($name)
{
echo “Hello $name”;
}

Class Names:
  • The first letter of a class name is capitalized. This visually distinguishes a class name from a member name.
  • Underscore should be used to simulate nested namespaces.
  • Multiword class names should be concatenated, and the first letter of each word should be capitalized. e.g.


class XML_RSS {}
class Text_PrettyPrinter {}

Method Names:
The Java style is to concatenate words in multiword method names and uppercase the first letter of every word after the first. e.g.

class XML_RSS
{
function startHandler() {}
}

Naming Consistency:
Variables that are used for similar purposes should have similar names. e.g.

$max_element;
$min_element;
$sum_element;
$prev_item;
$curr_item;
$next_item;


Matching Variable Names to Schema Names:
Variable names that are associated with database records should always have matching names.

$query = “SELECT firstname, lastname, employee_id
FROM employees”;
$results = mysql_query($query);
while(list($firstname, $lastname, $employee_id) = mysql_fetch_row())
{
//….
}

Documentation:
We’ll use phpDocumentor to generate documentation on the fly. Please take a look at the software www.phpdoc.org.

Wednesday

AJAX File Uploads (using Dojo and PHP)

Slashdot

It is easy to upload files using Ajax and PHP. How? I'll try to explain below.

We'll be using some common sense, Dojo Framework for AJAX, HTML, Apache, Javascript, and PHP. Make sure you install Apache and PHP (read other tutorials for that). I'll explain how to setup Dojo (for this tutorial) below. But before we proceed please read and understand the disclaimer below.

DISCLAIMER: I have made every attempt to present accurate information, but I cannot guarantee that inaccuracies will not occur. I (the author) will not be held responsible for any claim, loss, damage or inconvenience caused as a result of any information found on this website (blog).

Make sure you configure PHP to allow file uploads.
  • Fire up your browser and go to http://dojotoolkit.org/. Download Dojo form the website. I've used version 0.4.1 for this tutorial.
  • After downloading the zip file go to the folder where you want to put your file upload code (I've named this file index.php). Unzip the downloaded file in that folder and rename the new folder created (e.g. dojo-0.4.1-ajax.zip) to dojo.
  • Enter the code below in the head (between <head> and </head>) section of your HTML file (that contains file upload form). I'm naming it index.php.

<script language="javascript" type="text/javascript" src="./dojo/dojo.js"></script>

  • Now in the body of the index.php enter the form as follows.
<form id="upload_file" action="file_upload_script.php" method="post"
enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="8388608" />
<input type="file" name="select_file" tabindex="1" size="35" />
<input type="button" value="Submit" onClick="upload_file_submit()" tabindex="2">
</form>

For the sake of simplicity I am using a very simple form. You can add more fields if you want.

  • Now, enter the javascript code below in your head section of index.php.

<script language="javascript">
<!--
// Dojo configuration and Variable Initialization
// Put this code in index.php before the line where you include the javascript for dojo
// djConfig = { isDebug: true };
dojo.require("dojo.io.*");
dojo.require("dojo.io.IframeIO");
ctr = 0;
function upload_file_submit()
{

var bindArgs = {

formNode: document.getElementById("upload_file"), // form's id
mimetype: "text/plain", // Enter file type info here
content:
{
increment: ctr++,
name: "select_file", // file name in the form
post_field: "" // add more fields here .. fields will be accessible through $_POST["psot_fields"]
},
handler: function(type, data, evt)
{
// handle successful response here
if(type == "error") alert("Error occurred. ");
else
{
// getting error message from PHP's file upload script
res = dojo.byId("dojoIoIframe").contentWindow.document.getElementById("output").innerHTML;
// Incase of an error, display the error message
if(res != "true") alert(res);
else alert("File uploaded successfully.");
}
}
};
var request = dojo.io.bind(bindArgs);
}

//-->
</script>

  • Now, create a file named file_upload_script.php and add the code below in that file.

<script language="php">
// You can send any message to javascript client using die(create_message(<message>)); ...
//this function will terminate the script and will return control to the client

if( isset($_POST["MAX_FILE_SIZE"]) && isset($_FILES))

{
if( $_FILES['select_file']['size'] > $_POST["MAX_FILE_SIZE"])
die(create_message("File size must be less than ".$_POST["MAX_FILE_SIZE"]." bytes."));

$uploadFile = "./file_name_on_server.txt";

if(move_uploaded_file( $_FILES['select_file']['tmp_name'], $uploadFile))
{
$fp = fopen ($uploadFile, "rb");
if(!$fp) die(create_message("Error opening uploaded file."));
fclose($fp);
// SENDING TRUE IS MANDATORY SO THAT THE JAVASCRIPT

//FUNCTION KNOWS THAT THE FILE HAS BEEN UPLOADED PROPERLY
die(create_message("true"));
}
else die(create_message( $_FILES['select_file']['tmp_name']." could not be uploaded successfully."));

}
else die(create_message( "Error in form input: Data is missing. "));

function create_message($string)

{
$output = "";
$output .= "<html><head></head><body>";
$output .= "<textarea id=\"output\">";
$output .= trim($string);
$output .= "</textarea>";
$output .= "</body></html>";
return $output;
}
</script>


That's it. Go to index.php from your browser and upload a text file. Press submit. If there is no error you should see File uploaded successfully message.

Labels: , , , , ,

Monday

Installing Atmail Webmail Client on Mac OS X

Because of a very good response from readers I have created my own website.
"Installing Atmail Webmail Client on Mac OS X" can now be found here.

Labels:

Sunday

General Tips & Tricks

Slashdot




Tips & Tricks


Disclaimer: I (the author of this blog) wants to let you know that you'll use any or all of the instructions on this page at your own risk and I am not responsible for any kind of damage caused (indirectly or directly or in any other way) by following the instructions below or by reading the article and/or my blog.

Here is a list of tips and tricks that I found useul. This list is my on-going work so expect it to change very often and keep visiting.





  • How to create a one-to-one relationship in MS Sql Server 2000 Enterprise Manager Diagram Editor:
    Make the column unique in the table where the key is acting as foreign key.
  • Cloak / Uncloak SCM (subversion) directories in Dreamweaver
    Its very easy to do that. You just have to go to "
    Adobe Exchange" and search for the string "svn cloak uncloak". It'll show you only one result. Go to the result page and download the dreamweaver extension. Now, fire up your Dreamweaver (I tried version 8), click on Command->Manage Extensions. This will open up a tiny window on your screen. Click on the toolbar button "Install Extension" and select the file you downloaded from Adobe Exchange. This will install the extension. After installation is complete you'll see two new menu items under Command Menu item (i) Clock SCM Directories (ii) Uncloak SCM directories. Names are self-explanatory, so wont tell you what to do next.
  • Go to Top of the Page Using Javascript
    Use the code below
    window.scrollTo(0,0);
  • Javascript: Execute a function every second
    Use the Code below to do that
    var timer = null;
    timer = setInterval('function()', 1000); // time = 1000 ms
  • MS SQL SERVER 2000: Comparing DATETIME Value and Date String:
    In order to compare a DateTime column with a Date string you have to use the string below in SQL's Where clause.
    WHERE CONVERT(VARCHAR, [birth_date], 1) = '12/27/81'
    where birth_date is a DateTime column in the table.
  • Javascript: Sleep for 2 Seconds
    Use the function below to do that. Pass 2000 to this function to sleep for 2 seconds.
    function js_sleep(milliseconds){
    var date = new Date();
    var curDate = null;
    do{ curDate = new Date(); }while(curDate - date < milliseconds);
  • PHP: Finding name of caller
    function called_function()
    {
    $backtrace = debug_backtrace();
    print "Caller is:".$backtrace[1]['function'];
    }




Please let me know if you have some tips and tricks that I can add here.

NOTE: See readers' comments after the advertisement.

Labels: , , , , ,

Debian Installation and Hardening

Because of a very good response from readers I have created my own website. "Debian installation and hardening" can now be found here.

Labels:

SEO - A Long List of Free Web Directories

Slashdot
Cliky Directory
DigGeek web directory
WeAreYourFriends.se Svenska Länkar
Flexengine Directory
Freekick Temple
Black Dhalia Internet Directory
all2time directory http://directory.all2time.com/ Free web directory. All only for you. Submit your site and increase your google page rank.
Internet Webresources
URL Chief Web Directory
Top Dot Directory
Tater Links : Darkside SEO Links
Sams Directory
Add Your URL
Matrixsolutnz.net
Affordable Shopping Cart Software and Network Management Systems by Maxxfusion – Website design - hosting - maintenance
Link Nom
LinkBook
Findsome.info
BMVG Link Directory
eWeb Index Directory - URL Submission
epot.ca
Enterwork Directory
Dynn.Org Web Directory
Duane Gartman - Seo Friendly Web Directory
DPBTIN Directory
Dir Submission WebDirectory
Free Web Directory
Directory Member Website Listings
directoryJP - a general web directory with free links. submit today.
Mountain Net
Get In Search Engines
DirectoryVault
Seo friendly directory
WTS Link Directory
Skype Media Web Directory
Seo friendly web directory
Xland Web Directory
Seo friendly directory
Directory Link

Target coupons and promo codesSfind coupon codes for Target.com or any other online discount store with allfreecoupons.com.


Portal IT Directory
Idirect Connect Directory
Work Injury Claim - offering information and locating available services for accident victims.
SiteTutor
gdirectory Website directory for all directory listingsgdirectory Website directory

Tsection Web Directory
info-listings
OAST - The Home of Top Sites
Buy Modern Art Directory
www.firetown.com
Speedy Directory
directory.xperts.ro
UrlCan Web Directory - Direct link to your site for free.
!tzalist Computers Directory
Artoo Links - SEO Friendly Directory Droid
Click my Brick Directory
D911.NET Free Link Directory - This is the link directory where you can submit the URL to your homepage. Powered by PHP Link Directory.
dir.ajnin.com
Maldives Directory
Bookmark Directory
directory.helpedia.com
Ldmstudio Directory
Seo Friendly Directory
Portal IT Directory
Seo Friendly Web Directory
SEO Poland Web Directory
Domain Link Exchange
71K.NET - Web Directory
A Net Directory
Yonan Research Web Directory
AskBEE Free Directory
Kasan Web Directory
Profit Geek Directory
Web Directory
EVMINA - Web Directory and Searhengine
Clarib Web Directory
Bindexed.com
SEO TEAM
Directory Bin
Free Website Directory
Digital Directory Service | Increase Web Traffic, free submissions!
A List Directory
A List Sites
Alvz.com
Anaximander Directory

Labels: