Wednesday, August 06, 2008

SOAP with PHP : Create Web Service using SOAP by Example

SOAP is a wonderful technology that can help you in developing great applications. In this tutorial, we will investigate the use of SOAP in PHP.

Unless you have been living in a cave somewhere without Internet access for the last few years, you have undoubtedly heard of XML, SOAP and Multi-Tiered Application Programming. If you are like many programmers, including myself, you were quite taken aback by these ideas and technologies. You may have gone so far as to simply dismiss them as irrelevant to your skill set. It's time to wake up and realize they're here to stay... and for good reason!

XML and SOAP, and in turn Multi-Tiered Programming, are technologies that can take you from being a run of the mill code hacker to a professional application developer that actually builds cool things that work and which other people can work on. These technologies enable you to build applications that separate data from presentation, keep things organized and enable your application to scale as your needs and user base increases.

If you believe like I do that the Internet is the ultimate building ground of our future, then you have to see that the 'hackish' method in which most applications for the web are built and designed is pitiful. I know that I am quite guilty of it, myself. Many times I get an itch and I just scratch it without thinking of what the future holds or the maintainability of my application. Sure the job gets done; the itch has gone away momentarily. But when the itch comes back six months down the road and I have to add or modify features, I am utterly disappointed in myself over the sorry shape of my code.

You may be asking, how can XML and SOAP help me to avoid poor application design? Well, by themselves they won't help at all. First and foremost you must get yourself into the mind set that it needs to take place. XML and SOAP are just two tools that will allow you to accomplish your goal.

Define Our Goal

Today we will build a Web Service using SOAP. In doing so, I hope that you will become familiar with the technology so that you can start incorporating it into your future applications.

Before we get too much further along, let's make sure we are all on the same footing regarding the basic terminology that we will deal with in this tutorial.

  • XML: "XML is the Extensible Markup Language. It is designed to improve the functionality of the Web by providing more flexible and adaptable information identification." (http://www.ucc.ie/xml/#acro)%br%%br%In other words, XML is a method for describing your data. For the purpose of this tutorial, we will not be directly manipulating any XML. Instead, we will examine the XML resulting from our scripts. The libraries and protocols we will use through this tutorial will handle the XML manipulation for us.%br%%br%
  • SOAP: Simple Object Access Protocol. "SOAP is a lightweight protocol for exchange of information in a decentralized, distributed environment. It is an XML based protocol that consists of three parts: an envelope that defines a framework for describing what is in a message and how to process it, a set of encoding rules for expressing instances of application-defined datatypes, and a convention for representing remote procedure calls and responses." (http://www.w3.org/TR/2000/NOTE-SOAP-20000508/) is what you are here for. We will develop both a client and a server for our SOAP service. In this tutorial, we will be using the NuSOAP library. (http://dietrich.ganx4.com/nusoap/index.php)
  • WSDL: "WSDL is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information." (http://www.w3.org/TR/wsdl) with XML, we will not be directly any WSDL documents. The wonderful NuSOAP library will generate WSDL documents for us. What you need to know about WSDL is that it is a document that describes a Web Service. It can tell a client how to interact with the Web Service and what interfaces that Web Service provides.
  • Client: We will define a Client as a script that uses a Web Service.
  • Server: Conversely, a Server will be defined as a script that provides a Web Service.

  • Today we are going to build a Web Service that will return a stock price given a particular stock symbol. This is a classic example of where Web Services are of great use.

    You may be building an application that needs the data and could very easily just pull the data directly from your data source. Building a Web Service for it, however, allows you to give other applications easy access the same data in the future. It also separates the data extraction from the data source from the application itself. Say you were storing the data in a MySQL database but later decided to move it to a SQLite database... in this scenario your application wouldn't know the difference. Its calls to the Web Service remain unchanged.

    To provide a stock quote service you will have to have the stock prices and symbols stored in some fashion or another. This tutorial is not going to concentrate on the storage mechanism or how to obtain the prices. I will simply provide you will a table schema and some sample data to work with.

    CREATE TABLE `stockprices` (
    `stock_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
    `stock_symbol` CHAR( 3 ) NOT NULL ,
    `stock_price` DECIMAL(8,2) NOT NULL ,
    PRIMARY KEY ( `stock_id` )
    );
    INSERT INTO `stockprices` VALUES (1, 'ABC', '75.00');
    INSERT INTO `stockprices` VALUES (2, 'DEF', '45.00');
    INSERT INTO `stockprices` VALUES (3, 'GHI', '12.00');
    INSERT INTO `stockprices` VALUES (4, 'JKL', '34.00');

    Create a SOAP server


    The first thing we need to do is to create the SOAP server. This is the script that will fetch the data from the database and then deliver it to the Client. One wonderful thing about the NuSOAP library is that this same Server script will also create a WSDL document for us.

    The first step is to create a function that will fetch the data we want. Create this function just as you would any other. It is just straight up PHP. The one trick is to name the function something sensible, as this will be the name that is used when the Client contacts the Server.

    function getStockQuote($symbol) {

    mysql_connect('server','user','pass');
    mysql_select_db('test');
    $query = "SELECT stock_price FROM stockprices "
    . "WHERE stock_symbol = '$symbol'";
    $result = mysql_query($query);

    $row = mysql_fetch_assoc($result);
    return
    $row['stock_price'];
    }
    ?>

    Now, it is time to turn this function into a Web Service. Basically, all we have to do is include the NuSOAP library, instantiate the soap_server class and then register the function with the server. Let's go through it step by step, after which I will present the completed script.

    The first thing necessary is to simply include the NuSOAP library.

    require('nusoap.php');

    Next, instantiate an instance of the soap_server class.

    $server = new soap_server();

    The next line is used to tell NuSOAP information for the WSDL document it is going to create for us. Specifically we specify the name of the server and the namespace, in that order.

    $server->configureWSDL('stockserver', 'urn:stockquote');

    Now, we register the function we created with the SOAP server. We pass several different parameters to the register method.

    The first is the name of the function we are registering.

    The next parameter specifies the input parameters to the function we are registering. Notice that it is an array. The keys of the array represent the names of the input parameters, while the value specifies the type of the input parameter. One thing that pure PHP programmers might find odd is that I had to specify what types my input and return parameters are with the designations of xsd:string and xsd:decimal. It is required that you describe your data properly. You are not dealing with a loosely typed language here.

    The third parameter to the register method specifies the return type of the registered function. As shown below, it is fashioned in the same way as the last parameter, as an array.

    The next two parameters specify the namespace we are operating in, and the SOAPAction. For more information on the SOAPAction see http://www.oreillynet.com/pub/wlg/2331.

    $server->register("getStockQuote",
    array('symbol' => 'xsd:string'),
    array('return' => 'xsd:decimal'),
    'urn:stockquote',
    'urn:stockquote#getStockQuote');

    Now, we finally finish it off with two more lines of code. The first simply checks if $HTTP_RAW_POST_DATA is initialized. If it is not, it initializes it with an empty string. The next line actually calls the service. The web request is passed to the service from the $HTTP_RAW_POST_DATA variable and all the magic behind the scenes takes place.

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
    ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA);

    Here is the completed server script which I have saved in a file named stockserver.php.

    function getStockQuote($symbol) {

    mysql_connect('server','user','pass');
    mysql_select_db('test');
    $query = "SELECT stock_price FROM stockprices "
    . "WHERE stock_symbol = '$symbol'";
    $result = mysql_query($query);

    $row = mysql_fetch_assoc($result);
    return
    $row['stock_price'];
    }

    require(
    'nusoap.php');

    $server = new soap_server();

    $server->configureWSDL('stockserver', 'urn:stockquote');

    $server->register("getStockQuote",
    array(
    'symbol' => 'xsd:string'),
    array(
    'return' => 'xsd:decimal'),
    'urn:stockquote',
    'urn:stockquote#getStockQuote');

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
    ?
    $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA);
    ?>

    The WSDL Document

    At this point you have a fully functioning SOAP Server. Clients can connect to it and request data. If you haven't done so already, bring up the script in your browser and see what you get. You should get a page giving you a link to the WSDL document for the Server. Click on it and you should see the resulting WSDL document. Surprise, surprise, it is in XML! If you read over this document, you will see that it describes what happens for a request and as a response for your particular SOAP Service.

    Note that while it is possible to create a SOAP Server without having it create the WSDL file, I recommend creating the WSDL document anyway. It is simple enough, so why not?

    Creating a SOAP Client


    Creating a SOAP Client to access our Server with is just as simple as creating the Server was. Understand though that the Client does not necessarily need to be a PHP Client. The SOAP Server we just created can be connected to by any type of Client, whether that be Java, C#, C++, etc.

    To create the SOAP Client, all we need to do are three things.

    First, include the NuSOAP library. This is done just as it was for the Server.

    require_once('nusoap.php');

    Secondly, we need to instantiate the soapclient class. We pass in the URL of the SOAP Server we are dealing with.

    $c = new soapclient('http://localhost/stockserver.php');

    Last make a call to the Web Service. The one caveat is that the parameters to the Web Service must be encapsulated in an array in which the keys are the names defined for the service. You will see that I have an array key named 'symbol' because that is the name of the input parameter of my function. If you remember how we specified the input parameters when we registered the function with the server, you will see that this is very similar.

    $stockprice = $c->call('getStockQuote',
    array('symbol' => 'ABC'));

    Now, here is the completed Client script, which I have saved in a file named stockclient.php.

    require_once('nusoap.php');

    $c = new soapclient('http://localhost/stockserver.php');

    $stockprice = $c->call('getStockQuote',
    array(
    'symbol' => 'ABC'));

    echo
    "The stock price for 'ABC' is $stockprice.";

    ?>
    ?>

    Creating a SOAP Client to access our Server with is just as simple as creating the Server was. Understand though that the Client does not necessarily need to be a PHP Client. The SOAP Server we just created can be connected to by any type of Client, whether that be Java, C#, C++, etc.

    To create the SOAP Client, all we need to do are three things.

    First, include the NuSOAP library. This is done just as it was for the Server.

    require_once('nusoap.php');

    Secondly, we need to instantiate the soapclient class. We pass in the URL of the SOAP Server we are dealing with.

    $c = new soapclient('http://localhost/stockserver.php');

    Last make a call to the Web Service. The one caveat is that the parameters to the Web Service must be encapsulated in an array in which the keys are the names defined for the service. You will see that I have an array key named 'symbol' because that is the name of the input parameter of my function. If you remember how we specified the input parameters when we registered the function with the server, you will see that this is very similar.

    $stockprice = $c->call('getStockQuote',
    array('symbol' => 'ABC'));

    Now, here is the completed Client script, which I have saved in a file named stockclient.php.

    require_once('nusoap.php');

    $c = new soapclient('http://localhost/stockserver.php');

    $stockprice = $c->call('getStockQuote',
    array(
    'symbol' => 'ABC'));

    echo
    "The stock price for 'ABC' is $stockprice.";

    ?>
    ?>

    There it is. It really is that simple.

    Conclusion

    Hopefully after reading through this tutorial you have an understanding of how simple it is to create a SOAP Server and Client with NuSOAP. It simply astonished me how utterly simple it was after I actually took a look at it! Before I ever laid eyes on it I had dreams of a complex system that would take months to utilize. Luckily, NuSOAP came along and made that task simpler than anyone could ever ask for.

    As you can see, SOAP is a wonderful tool for separating your application into smaller more manageable pieces. Do realize that SOAP isn't the cure all for everything. Its overuse is just as bad as any other poor design. The key to a good application design is patience and planning. Sit down, take out the old pencil and paper and write things down. Understand what you are really getting yourself into and ask lots of 'What If' questions. Think about the future of the application and ask yourself about the different ways it may be used in the future. The number one pitfall of application design is painting yourself into a corner. If you just thought about it ahead of time you could have started at the other side of the room.



    Tuesday, August 05, 2008

    AdSense Secret - How to choose the best keywords for adSense

    Ever wondered how to select the best keywords for your Adsense websites? Your focus is earning money as well as attracting organic traffic towards your website. Here is what you need to know while choosing a keyword, a scientific study that maps the ideal keywords for your next upcoming adsense website.

    We shall first study how to choose keywords that lead to more Adsense money.

    CPC - Advantages and Drawbacks

    What does CPC indicate?

    When we think of high paying keywords, the first thing that pops up in our mind is CPC. CPC (Cost Per Click) is the maximum amount of money an advertiser is willing to pay for a click. Higher the CPC of a particular keyword, more will be the payouts you can expect by targeting that keyword in your AdSense websites. You can get the most accurate CPC from the source itself - Google.

    However, CPC is not the only factor that tells the profitability of a keyword. There are many other factors that you should consider.

    Drawbacks of CPC

    - What if there are no advertisers bidding on a keyword having high CPC? Obviously, it means no one is going to pay you that high for a click.
    - What if the traffic on the keyword is technical enough and does not click on ads (banner blindness)?
    - What if Google gets loads of traffic on that keyword, enough to fulfill the desires of high paying advertisers? In this case, Google will only throw remaining peanuts towards you.

    Obviously, Google will like to keep all the high paying ads within its own pages. So you see, CPC just shows you a small part of the entire picture. We shall explore the rest part of the picture...

    Number of Sponsors - The Most Important Figure

    Keywords with high CPC do not always come with high number of advertisers. There are many keywords in Google's Adwords system that have a high price, but almost no or very less number of advertisers. Now, if you build a web page or website around a keyword on which no one is bidding, Google will try to fill the ad blocks with ads of the related keywords rather than the high paying keyword you were initially targeting. There is no guarantee that ads of the related keywords will be good converters. Therefore, there is every likelihood that your clicks will be passed through SmartPricing Filter, eventually resulting in lesser payouts.

    Hence, it is important to see the number of sponsors bidding on the keyword in question. If there are more the advertisers, there will be more competition among them to get the top position. This means that there will be more revenue per click that they are willing to share with you. In other words, more sponsors mean more people fighting against one another other to pay you more Adsense money.

    Number of clicks on an Ad

    It refers to the number of clicks an advertiser gets when his ad appears on the top. If the number of clicks and number of advertisers is less, just ignore those keywords, no matter what their CPC is. However, if the clicks per month are lower than expected, but the number of advertisers is a good one - it's the right choice for you. Such keywords are called "Niche AdSense' keywords." Here the word 'niche' is used in commercial context. This means that Google is not able to generate traffic on such keyword ads. This makes it difficult for it to exhaust the advertising budgets of the advertisers. Therefore, in order to meet its targets, Google happily shares high paying ads with you, resulting in more payouts. Try to find such "Niche AdSense' keywords."

    Coming to Higher number of clicks - It indicates:

    Either the traffic segment is ignorant about the online advertising concepts, and click on these ads unknowingly. This means you will experience higher CTR on your ads when placed at appropriate place. You don't have to put in much efforts to fight banner blindness.

    Or, the traffic is highly commercial and willing to purchase the advertised product over internet. This means that there are very little chances of your facing the SmartPricing phenomenon.

    Number of clicks along with the other stats like Number of sponsors and CPC, can enable you to make more wise decisions while choosing keywords for your AdSense content.

    Bidding Quality

    It's important to see the pattern in which people are bidding on a keyword. Suppose there are 400 advertisers bidding on a keyword. The top 20 of these are paying something like $15 per click, while the rest of them pay somewhere between $2 and $0.05. Now you might have found some decent tools that give you the average of top 3 or top 5 positions which is good, but not good enough. The point is that although the top 20 advertisers are paying higher, but the rest 380 advertisers are paying quite low. There is a high probability of your getting the ads of those 380 sponsors. Therefore, the average of the top 20 advertises can be really misleading. The solution to this problem is discussed in the later part of this page.

    We have enough Adsense money now. Let's build some traffic on your website.

    Traffic Building for AdSense:

    Choosing Niche Keywords

    Niche keywords are the keywords that are highly searched by the web surfers, and are rarely used by your competitors. Less competition means more traffic to your website. Targeting ten niche keywords is easier and more fruitful that targeting a highly competitive keyword. Traffic from niche keywords when directed to a relevant page increases your CTR and conversion ratio.

    Determining Competition

    People generally take the number of results returned by search engines as the number of pages competing on a keyword. But it is wrong. The Search Engine Results get irrelevant after 10 - 15 pages. Irrelevancy further increases with the depth. The pages that have the keyword dumped in a corner are not competing against you, but search engines will still list them. In fact they have to.

    It is assumed that if a webmaster is targeting a web page with a particular keyword, the keyword is used in the title as well as in the anchor text linking to that webpage. Such a page is listed higher by the search engines as it is dedicated to what you searched for. So how to filter out the most relevant results? Check it out!

    Inanchor intitle and Its Precision - The Solution

    In Google, you can easily determine the EXACT number of pages that are competing against you. You can precisely list out the pages that are using a particular keyword in their page titles or in the anchor texts linking to them.

    The query can be applied as follows: intitle:keyword inanchor:keyword.

    For example, if the keyword is "hair treatment", the formula will be used in the following manner: intitle:hair inanchor: hair intitle: treatment inanchor: treatment. This figure gives you the exact number of pages that are ACTUALLY Targeting with these keywords, and not those that have just created a page or a small paragraph on the same topic. Google emphasizes on Anchors and Page titles. That's the reason, it supports such a search query.

    We shall now discuss some other traditional ways to determine competition.

    R/S Ratio

    Here, R refers the number of competitor websites for a particular keyword as per the search result of the search engine. And S refers the number of searchers using that keyword while searching their queries. This means that for better results, you have to choose the keywords with lower R/S ratio.

    R/S ratio becomes polluted when someone uses the number of results as the number of competing websites. As explained above, counting the number of results as the number of competing pages is the biggest mistake one can make while choosing a keyword. However, the figure becomes quite useful when inanchor intitle is used to create R/S.

    KEI Analysis

    KEI (Keyword Effectiveness Index) is a formula for measuring the effectiveness of a keyword. The formula was devised by Sumantra Roy. However, this figure also depends upon the number of searches and competition, but with a difference. This formula analyzes the number of searches and competition in such a way that if the searches increase, KEI increases; and if the competition increases, KEI decreases. Higher the KEI, more profitable will be the keyword. However, it becomes polluted when the number of search results are used as the number of competitors.

    Determining Traffic

    Determining traffic for a keyword is quite important before targeting it. Along with the competition stats, it lets you make out the niches present in any industry. Besides, it lets you predict (to some extent) how much traffic you can expect if you promote a website around a particular keyword. There are two known sources for determining traffic. Overture Keyword Suggestion Tool (now a part of Yahoo Search Engine) and WordTracker. When talking about accuracy, Wordtracker monitors the queries on some meta search engines that actually render it inaccurate . Reason? There is no known stat about how much of the community you are targeting is searching on those meta search engines. However, it's a good tool to make out the niches.

    In my opinion, Yahoo owned Overture gives you more accurate stats than Wordtracker. This is for the reason that Overture is a PPC engine. It can show you how many people are searching for a particular term on its vast network. A network which is bigger than any Meta Search Engine. Still, it is not that accurate, but at least better than Wordtracker. The tools that are predicting Google Searches for you are just doing guess work. No one knows the algorithm they use.

    Summing It Up

    Profitability of a keyword depends more on the number of sponsors than CPC. Number of clicks and bidding quality can't be determined by just monitoring the first 3 or 8 places. It can be rightly assessed by taking out the average of all the 400 sponsors bidding on a keyword. Sounds very tiring for keyword research, right?

    Wednesday, July 23, 2008

    20 ways to Secure your Apache Configuration

    Here are 20 things you can do to make your apache configuration more secure.

    Disclaimer: The thing about security is that there are no guarantees or absolutes. These suggestions should make your server a bit tighter, but don't think your server is necessarily secure after following these suggestions.

    Additionally some of these suggestions may decrease performance, or cause problems due to your environment. It is up to you to determine if any of the changes I suggest are not compatible with your requirements. In other words proceed at your own risk.

    First, make sure you've installed latest security patches

    There is no sense in putting locks on the windows, if your door is wide open. As such, if you're not patched up there isn't really much point in continuing any longer on this list. Go ahead and bookmark this page so you can come back later, and patch your server.

    Hide the Apache Version number, and other sensitive information.

    By default many Apache installations tell the world what version of Apache you're running, what operating system/version you're running, and even what Apache Modules are installed on the server. Attackers can use this information to their advantage when performing an attack. It also sends the message that you have left most defaults alone.

    There are two directives that you need to add, or edit in your httpd.conf file:

    ServerSignature Off
    ServerTokens Prod

    The ServerSignature appears on the bottom of pages generated by apache such as 404 pages, directory listings, etc.

    The ServerTokens directive is used to determine what Apache will put in the Server HTTP response header. By setting it to Prod it sets the HTTP response header as follows:

    Server: Apache

    If you're super paranoid you could change this to something other than "Apache" by editing the source code, or by using mod_security (see below).

    Make sure apache is running under its own user account and group

    Several apache installations have it run as the user nobody. So suppose both Apache, and your mail server were running as nobody an attack through Apache may allow the mail server to also be compromised, and vise versa.

    User apache
    Group apache

    Ensure that files outside the web root are not served

    We don't want apache to be able to access any files out side of its web root. So assuming all your web sites are placed under one directory (we will call this /web), you would set it up as follows:


    Order Deny,Allow
    Deny from all
    Options None
    AllowOverride None


    Order Allow,Deny
    Allow from all

    Note that because we set Options None and AllowOverride None this will turn off all options and overrides for the server. You now have to add them explicitly for each directory that requires an Option or Override.

    Turn off directory browsing

    You can do this with an Options directive inside a Directory tag. Set Options to either None or -Indexes

    Options -Indexes

    Turn off server side includes

    This is also done with the Options directive inside a Directory tag. Set Options to either None or -Includes

    Options -Includes

    Turn off CGI execution

    If you're not using CGI turn it off with the Options directive inside a Directory tag. Set Options to either None or -ExecCGI

    Options -ExecCGI

    Don't allow apache to follow symbolic links

    This can again can be done using the Options directive inside a Directory tag. Set Options to either None or -FollowSymLinks

    Options -FollowSymLinks

    Turning off multiple Options

    If you want to turn off all Options simply use:

    Options None

    If you only want to turn off some separate each option with a space in your Options directive:

    Options -ExecCGI -FollowSymLinks -Indexes

    Turn off support for .htaccess files

    This is done in a Directory tag but with the AllowOverride directive. Set it to None.

    AllowOverride None

    If you require Overrides ensure that they cannot be downloaded, and/or change the name to something other than .htaccess. For example we could change it to .httpdoverride, and block all files that start with .ht from being downloaded as follows:

    AccessFileName .httpdoverride

    Order allow,deny
    Deny from all
    Satisfy All

    Run mod_security

    mod_security is a super handy Apache module written by Ivan Ristic, the author of Apache Security from O'Reilly press.

    You can do the following with mod_security:

    • Simple filtering
    • Regular Expression based filtering
    • URL Encoding Validation
    • Unicode Encoding Validation
    • Auditing
    • Null byte attack prevention
    • Upload memory limits
    • Server identity masking
    • Built in Chroot support
    • And more

    Disable any unnecessary modules

    Apache typically comes with several modules installed. Go through the apache module documentation and learn what each module you have enabled actually does. Many times you will find that you don't need to have the said module enabled.

    Look for lines in your httpd.conf that contain LoadModule. To disable the module you can typically just add a # at the beginning of the line. To search for modules run:

    grep LoadModule httpd.conf

    Here are some modules that are typically enabled but often not needed: mod_imap, mod_include, mod_info, mod_userdir, mod_status, mod_cgi, mod_autoindex.

    Make sure only root has read access to apache's config and binaries

    This can be done assuming your apache installation is located at /usr/local/apache as follows:

    chown -R root:root /usr/local/apache
    chmod -R o-rwx /usr/local/apache

    Lower the Timeout value

    By default the Timeout directive is set to 300 seconds. You can decrease help mitigate the potential effects of a denial of service attack.

    Timeout 45

    Limiting large requests

    Apache has several directives that allow you to limit the size of a request, this can also be useful for mitigating the effects of a denial of service attack.

    A good place to start is the LimitRequestBody directive. This directive is set to unlimited by default. If you are allowing file uploads of no larger than 1MB, you could set this setting to something like:

    LimitRequestBody 1048576

    If you're not allowing file uploads you can set it even smaller.

    Some other directives to look at are LimitRequestFields, LimitRequestFieldSize and LimitRequestLine. These directives are set to a reasonable defaults for most servers, but you may want to tweak them to best fit your needs. See the documentation for more info.

    Limiting the size of an XML Body

    If you're running mod_dav (typically used with subversion) then you may want to limit the max size of an XML request body. The LimitXMLRequestBody directive is only available on Apache 2, and its default value is 1 million bytes (approx 1mb). Many tutorials will have you set this value to 0 which means files of any size may be uploaded, which may be necessary if you're using WebDAV to upload large files, but if you're simply using it for source control, you can probably get away with setting an upper bound, such as 10mb:

    LimitXMLRequestBody 10485760

    Limiting Concurrency

    Apache has several configuration settings that can be used to adjust handling of concurrent requests. The MaxClients is the maximum number of child processes that will be created to serve requests. This may be set too high if your server doesn't have enough memory to handle a large number of concurrent requests.

    Other directives such as MaxSpareServers, MaxRequestsPerChild, and on Apache2 ThreadsPerChild, ServerLimit, and MaxSpareThreads are important to adjust to match your operating system, and hardware.

    Restricting Access by IP

    If you have a resource that should only by accessed by a certain network, or IP address you can enforce this in your apache configuration. For instance if you want to restrict access to your intranet to allow only the 176.16 network:


    Order Deny,Allow
    Deny from all
    Allow from 176.16.0.0/16

    Or by IP:

    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1

    Adjusting KeepAlive settings

    According to the Apache documentation using HTTP Keep Alive's can improve client performance by as much as 50%, so be careful before changing these settings, you will be trading performance for a slight denial of service mitigation.

    KeepAlive's are turned on by default and you should leave them on, but you may consider changing the MaxKeepAliveRequests which defaults to 100, and the KeepAliveTimeout which defaults to 15. Analyze your log files to determine the appropriate values.

    Run Apache in a Chroot environment

    chroot allows you to run a program in its own isolated jail. This prevents a break in on one service from being able to effect anything else on the server.

    It can be fairly tricky to set this up using chroot due to library dependencies. I mentioned above that the mod_security module has built in chroot support. It makes the process as simple as adding a mod_security directive to your configuration:

    SecChrootDir /chroot/apache

    There are however some caveats however, so check out the docs for more info.

    Acknowledgments

    I have found the book Apache Security to be a highly valuable resource for securing an apache web server. Some of the suggestions listed above were inspired by this book.

    Suggestions

    Please post any suggestions, caveats, or corrections in the comments and I will update the post if necessary.

    Tuesday, July 22, 2008

    Some basic Unix/Linux network commands for Troubleshoot

    Troubleshooting or just need a quick refresher on some basic and advanced Unix Linux network related commands?

    ifconfig - configure a network interface (setup)
    route - show / manipulate the IP routing table
    ping, ping6 - send ICMP ECHO_REQUEST to network hosts
    netstat
    - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

    tcpdump - dump traffic on a network
    tcpslice - extract pieces of and/or glue together tcpdump files

    traceroute - print the route packets trace to network host
    tracepath, tracepath6 - traces path to a network host discovering MTU along this path
    iwconfig - configure a wireless network interface
    iwlist - Get more detailed wireless information from a wireless interface
    wpa_supplicant - Wi-Fi Protected Access client and IEEE 802.1X supplicant
    wpa_supplicant.conf [wpa_supplicant] - configuration file for wpa_supplicant
    ipcalc - perform simple manipulation of IP addresses
    nc - arbitrary TCP and UDP connections and listens
    snort - open source network intrusion detection system
    ettercap - A multipurpose sniffer/content filter for man in the middle attacks
    ethereal - Interactively dump and analyze network traffic

    Wednesday, July 02, 2008

    New things in HTML5

    Please note that still HTML 5 in under draft mode. there are certainly many things changed when it come to live. but myself also waiting for this new kind html. I have list down some of the features which I read through w3c and different searches.

    I hope browser will also be ready for it.


    Html5 is created in such a way that it will be backward compatibility.
    it will have html + xml as their base.
    in html4 we are writting <html>
    but as per rule html 5 should be written as follows.

    <?xml version=”1.0″?>
    <html xmlns=”http://www.w3.org/1999/xhtml”>

    * meta elements are replaced.<meta http-equiv=”content-type” content=”text/html; charset=urf-8″>
    <meta charset=”utf-8″>

    in html5 their no necessary to use doctype. as everything is going to handle with xml.

    Following are the new elements are added in html5

    1.sections. - generic document or application sections.
    2.article - this represent independant piece of contents.
    3.aside - piece of content which is only sightly related to whole document.
    4.header - header of the section.
    5.footer - footer of the section
    6.nav - represent a section of the document for navigation.
    7.dialog - used for conversation
    8.figure - its kind of embed function used for video or graphics
    9.audio video these are elements used for showing audio / video.
    10.embed
    11. m - represent marked text
    12. meter - used disk usage.
    13. time - represent time of the page.
    14. convas - is used to create dynamic bitmap images on the fly.
    15. command - used to invoke commands which user will invoke.
    16. datagrid - represent an interactive representational of tree list.
    17. details - this will shows additional informtion
    18. datalist to used to represent to make comboboxes see the following example.
    <input list=”browser”>
    <datalist id=”browser”>
    <option value=”1″>1</option>
    <option value=”2″>2<option>
    </datalist>
    19. datatemplate, rule, nest - used for templating mechanism.
    20. event-source - is used to to catch server sent events.
    21. output - it is used for some sort of scripting calculation.
    22. input type has following new values.
    datetime
    datetime-local
    date
    month
    week
    time
    number
    range
    email
    url

    New attributes

    Thursday, June 19, 2008

    2008-2009 Web Trends - WEB 3.0

    What then can we expect from the next 10 or so years on the Web? As NatC commented in this week's poll, the biggest impact of the Web in 10 years time won't necessarily be via a computer screen - "your online activity will be mixed with your presence, travels, objects you buy or act with." Also a lot of crossover will occur among the 10 trends below (and more) and there will be Web technologies that become enormously popular that we can't predict now.

    We're well into the current era of the Web, commonly referred to as Web 2.0. Features of this phase of the Web include search, social networks, online media (music, video, etc), content aggregation and syndication (RSS), mashups (APIs), and much more. Currently the Web is still mostly accessed via a PC, but we're starting to see more Web excitement from mobile devices (e.g. iPhone) and television sets (e.g. XBox Live 360).

    Bearing all that in mind, here are 10 Web trends to look out for over the next 10 years...

    1. Semantic Web

    Sir Tim Berners-Lee's vision for a Semantic Web has been The Next Big Thing for a long time now. Indeed it's become almost mythical, like Moby Dick. In a nutshell, the Semantic Web is about machines talking to machines. It's about making the Web more 'intelligent', or as Berners-Lee himself described it: computers "analyzing all the data on the Web – the content, links, and transactions between people and computers." At other times, Berners-Lee has described it as "the application of weblike design to data" - for example designing for re-use of information.

    As Alex Iskold wrote in The Road to the Semantic Web, the core idea of the Semantic Web is to create the meta data describing data, which will enable computers to process the meaning of things. Once computers are equipped with semantics, they will be capable of solving complex semantical optimization problems.

    So when will the Semantic Web arrive? The building blocks are here already: RDF, OWL, microformats are a few of them. But as Alex noted in his post, it will take some time to annotate the world's information and then to capture personal information in the right way. Some companies, such as Hakia and Powerset and Alex's own AdaptiveBlue, are actively trying to implement the Semantic Web. So we are getting close, but we are probably a few years off still before the big promise of the Semantic Web is fulfilled.

    Semantic Web pic by dullhunk

    2. Artificial Intelligence

    Possibly the ultimate Next Big Thing in the history of computing, AI has been the dream of computer scientists since 1950 - when Alan Turing introduced the Turing test to test a machine's capability to participate in human-like conversation. In the context of the Web, AI means making intelligent machines. In that sense, it has some things in common with the Semantic Web vision.

    We've only begun to scratch the surface of AI on the Web. Amazon.com has attempted to introduce aspects of AI with Mechanical Turk, their task management service. It enables computer programs to co-ordinate the use of human intelligence to perform tasks which computers are unable to do. Since its launch on 2 November 2005, Mechanical Turk has gradually built up a following - there is a forum for "Turkers" called Turker Nation, which appears to have light-to-medium level patronage. However we reported in January that Mturk isn't being used as much as the initial hype period in Nov-Dec 05.

    Nevertheless, AI has a lot of promise on the Web. AI techniques are being used in "search 2.0" companies like Hakia and Powerset. Numenta is an exciting new company by tech legend Jeff Hawkins, which is attempting to build a new, brain-like computing paradigm - with neural networks and cellular automata. In english this means that Numenta is trying to enable computers to tackle problems that come easy to us humans, like recognizing faces or seeing patterns in music. But since computers are much faster than humans when it comes to computation, we hope that new frontiers will be broken - enabling us to solve the problems that were unreachable before.

    3. Virtual Worlds

    Second Life gets a lot of mainstream media attention as a future Web system. But at a recent Supernova panel that Sean Ammirati attended, the discussion touched on many other virtual world opportunities. The following graphic summarizes it well:

    Looking at Korea as an example, as the 'young generation' grows up and infrastructure is built out, virtual worlds will become a vibrant market all over the world over the next 10 years.

    It's not just about digital life, but also making our real life more digital. As Alex Iskold explained, on one hand we have the rapid rise of Second Life and other virtual worlds. On the other we are beginning to annotate our planet with digital information, via technologies like Google Earth.

    4. Mobile

    Mobile Web is another Next Big Thing on slow boil. It's already big in parts of Asia and Europe, and it received a kick in the US market this year with the release of Apple's iPhone. This is just the beginning. In 10 years time there will be many more location-aware services available via mobile devices; such as getting personalized shopping offers as you walk through your local mall, or getting map directions while driving your car, or hooking up with your friends on a Friday night. Look for the big Internet companies like Yahoo and Google to become key mobile portals, alongside the mobile operators.

    Companies like Nokia, Sony-Ericsson, Palm, Blackberry and Microsoft have been active in the Mobile Web for years now, but one of the main issues with Mobile Web has always been usability. The iPhone has a revolutionary UI that makes it easier for users to browse the Web, using zooming, pinching and other methods. Also, as Alex Iskold noted, the iPhone is a strategy that may expand Apple's sphere of influence, from web browsing to social networking and even possibly search.

    So even despite the iPhone hype, in the US at least (and probably other countries when it arrives) the iPhone will probably be seen in 10 years time as the breakthrough Mobile Web device.

    5. Attention Economy

    The Attention Economy is a marketplace where consumers agree to receive services in exchange for their attention. Examples include personalized news, personalized search, alerts and recommendations to buy. The Attention Economy is about the consumer having choice - they get to choose where their attention is 'spent'. Another key ingredient in the attention game is relevancy. As long as the consumer sees relevant content, he/she is going to stick around - and that creates more opportunities to sell.

    Expect to see this concept become more important to the Web's economy over the next decade. We're already seeing it with the likes of Amazon and Netflix, but there is a lot more opportunity yet to explore from startups.


    Image from The Attention Economy: An Overview, by Alex Iskold

    6. Web Sites as Web Services

    Alex Iskold wrote in March that as more and more of the Web is becoming remixable, the entire system is turning into both a platform and the database. Major web sites are going to be transformed into web services - and will effectively expose their information to the world. Such transformations are never smooth - e.g. scalability is a big issue and legal aspects are never simple. But, said Alex, it is not a question of if web sites become web services, but when and how.

    The transformation will happen in one of two ways. Some web sites will follow the example of Amazon, del.icio.us and Flickr and will offer their information via a REST API. Others will try to keep their information proprietary, but it will be opened via mashups created using services like Dapper, Teqlo and Yahoo! Pipes. The net effect will be that unstructured information will give way to structured information - paving the road to more intelligent computing.

    Note that we can also see this trend play out currently with widgets and especially Facebook in 2007. Perhaps in 10 years time the web services landscape will be much more open, because the 'walled garden' problem is still with us in 2007.


    Image from Web 3.0: When Web Sites Become Web Services, by Alex Iskold

    7. Online Video / Internet TV

    This is a trend that has already exploded on the Web - but you still get the sense there's a lot more to come yet. In October 2006 Google acquired the hottest online video property on the planet, YouTube. Later on that same month, news came out that the founders of Kazaa and Skype were building an Internet TV service, nicknamed The Venice Project (later named Joost). In 2007, YouTube continues to dominate. Meanwhile Internet TV services are slowly getting off the ground.

    Our network blog last100 has an excellent overview of the current Internet TV landscape, with reviews of 8 Internet TV apps. Read/WriteWeb's Josh Catone also reviewed 3 of them - Joost, Babelgum, Zattoo.

    It's fair to say that in 10 years time, Internet TV will be totally different to what it is today. Higher quality pictures, more powerful streaming, personalization, sharing, and much more - it's all coming over the next decade. Perhaps the big question is: how will the current mainstream TV networks (NBC, CNN, etc) adapt?


    Zattoo, from Internet Killed The Television Star: Reviews of Joost, Babelgum, Zattoo, and More, by Josh Catone

    8. Rich Internet Apps

    As the current trend of hybrid web/desktop apps continues, expect to see RIA (rich internet apps) continue to increase in use and functionality. Adobe's AIR platform (Adobe Integrated Runtime) is one of the leaders, along with Microsoft with its Windows Presentation Foundation. Also in the mix is Laszlo with its open source OpenLaszlo platform and there are several other startups offering RIA platforms. Let's not forget also that Ajax is generally considered to be an RIA - it remains to be seen though how long Ajax lasts, or whether there will be a '2.0'.

    As Ryan Stewart wrote for Read/WriteWeb back in April 2006 (well before he joined Adobe), "Rich Internet Apps allow sophisticated effects and transitions that are important in keeping the user engaged. This means developers will be able to take the amazing changes in the Web for granted and start focusing on a flawless experience for the users. It is going to be an exciting time for anyone involved in building the new Web, because the interfaces are finally catching up with the content."

    The past year has proven Ryan right, with Adobe and Microsoft duking it out with RIA technologies. And there's a lot more innovation to happen yet, so in 10 years time I can't wait to see what the lay of the RIA land is!

    9. International Web

    As of 2007, the US is still the major market in the Web. But in 10 years time, things might be very different. China is often touted as a growth market, but other countries with big populations will also grow - India and African nations for example.

    For most web 2.0 apps and websites (R/WW included), the US market makes up over 50% of their users. Indeed, comScore reported in November 2006 that 3/4 of traffic to top websites is international. comScore said that 14 of the top 25 US Web properties now attract more visitors from outside the US than from within. That includes the top 5 US properties - Yahoo! Sites, Time Warner Network, Microsoft, Google Sites, and eBay.

    However, it is still early days and the revenues are not big in international markets at this point. In 10 years time, revenue will probably be flowing from the International Web.

    10. Personalization

    Personalization has been a strong theme in 2007, particularly with Google. Indeed Read/WriteWeb did a feature week on Personalizing Google. But you can see this trend play out among a lot of web 2.0 startups and companies - from last.fm to MyStrands to Yahoo homepage and more.

    What can we expect over the next decade? Recently we asked Sep Kamvar, Lead Software Engineer for Personalization at Google, whether there will be a 'Personal PageRank' system in the future. He replied:

    "We have various levels of personalization. For those who are signed up for Web History, we have the deepest personalization, but even for those who are not signed up for Web History, we personalize your results based on what country you are searching from. As we move forward, personalization will continue to be a gradient; the more you share with Google, the more tailored your results will be."

    If nothing else, it'll be fascinating to track how Google uses personalization over the coming years - and how it deals with the privacy issues.

    Conclusion

    We've covered a lot of ground in this post, so tell us know what you think of our predictions. What other Web trends do you forsee over the next decade?

    Do Trace - Debug ( Profiling ) of PHP script by xdebug & kcachegrind


    Step 1 : Install XDebug

    xdebug is an open source debugger available for PHP. xdebug can be used to display more information in error traces. It can also be used to collect detailed code coverage & profiling information.

    Installation

    You need to install following packages to prepare environment for installation of pecl module xdebug.

    sudo apt-get -y install php-pear php5-dev build-essential

    Now we install xdebug using pecl.

    sudo pecl install xdebug

    Above command will download, compile & install xdebug on your system.

    Configuration

    Open usr/local/lib/php.ini and append following line.

    zend_extension=/usr/lib/php5/20060613/xdebug.so

    Be careful about that number part in the path as it could be different for you. Now restart apache by issuing following command.

    sudo apache2ctl restart

    Now try to view the output of your phpinfo() function. If you find xdebug word in that then it means you have successfully installed xdebug.

    Stacktrace

    xdebug.collect_params accepts values from 1 to 4. Where 1 refers to less verbosity & 4 refers to maximum verbosity while displaying stack traces. The easiest way of setting this option is using function ini_set in PHP.

    ini_set("xdebug.collect_params", )

    Get to know more stack trace options of xdebug.

    var_dump

    Once after installing xdebug when ever you call var_dump you are actually calling xdebug version of var_dump instead of built in one. By default xdebug version of var_dump gives you friendly info on the variables you are trying to inspect.

    See more options on controlling display of var_dump information.

    Debugging

    xdebug uses DBGp protocol to communicate with external debuggers. Vim already has a plugin to do that. So you can debug php scripts from within vim.

    Download php debugger plugin for vim.

    Step 2: Enable Proiling

    You need to add following line to /usr/local/lib/php.ini to enable profiling of php scripts.

    xdebug.profiler_enable=1
    Now restart the apache server by issuing following command.
    sudo apache2ctl restart

    When ever you access a php page through apache, xdebug will create a file something like cachegrind.out.15093. xdebug by default uses /tmp directory to dump files which contain profiling information. You can change this target directory by using xdebug option xdebug.profiler_output_dir and you can change result file name by using the option xdebug.profiler_output_name.

    See more xdebug profiling options.

    Some times you don't want to profile all requests. xdebug provides a selective mechanism to trigger profiling of specific requests. In order to enable this option you have to add following configuration option to php.ini.
    xdebug.profiler_enable_trigger=On

    Now you can trigger profiling of specific requests by adding XDEBUG_PROFILE to the query part of the GET request.

    Analyzing Profiling results

    We need to use kcachegrind to analyse profile file results. We can install kcachegrind by issuing following command.

    sudo apt-get -y install kcachegrind

    Now open your profle result file in kcachegrind and you can visuall inspect which part of your script is eating cpu resources. Callmap & Callgrap provide easy to understand visualizations to find bottlenecks in your script.

    Tuesday, May 27, 2008

    What is new in Firefox 3


    The closest rival to Microsoft's Internet Explorer is ready to get an upgrade. Browser developer Mozilla has announced June release for its popular browser, Firefox.

    Firefox 3 promises to add several new features that will enhance the users browsing experience as well as make it for secure. In fact, Mozilla claims that Firefox 3.0 will run twice as fast as the previous version while using less memory.

    So, here's a peak into all the added features users will find in Firefox 3.0.

    Firefox 3 adds several new features that will make it more secure against online frauds, forgeries, viruses and Trojan.

    1.Security

    An important addition is One-click site info where the users can click the site favicon in the location bar to see who owns the site and to check if their connection is protected from eavesdropping.

    Also, the Identity verification is prominently displayed in the new version. When a site uses Extended Validation (EV) SSL certificates, the site favicon button will turn green and show the name of the company users are connected to.

    The new version has enhanced malware protection which warns users when they enter a site which can install viruses, spyware, trojans or other malware.

    Version 3 also lets users to access Web Forgery Protection page that displays the content suspected of web forgeries.

    Also, the new version promises secured data protection. User's bookmarks, history, cookies, and preferences will now be stored in a transactionally secure database format which will prevent data loss even if their system crashes.

    2.Improved Javascript Engine


    According to Firefox team, improvements to the JavaScript engine as well as profile guided optimization has enhanced the performance of Firefox 3.0.

    The new Firefox 3.0 will enable web applications like Google Mail and Zoho Office to run twice as fast compared to Firefox 2. Also, SunSpider test from Apple shows improvements over previous releases.

    3.Performance


    Firefox 3 endeavours to reduce the amount of memory used over a Web browsing session.

    Memory cycles are broken and collected by an automated cycle collector, a new memory allocator has been added to reduce fragmentation, a large number of leaks have been fixed, and caching strategies have been tuned.


    4.Add-ons

    Also, the new version of Firefox automatically checks new add-ons and plugins and will disable older, insecure versions.

    The new version promises to make users browsing experience more organised and clutter-free.

    An information bar replaces the old password dialogue so that users can now save passwords after a successful login. The add-ons whitelist has been removed making it possible to install extensions from third-party sites in fewer clicks.

    The revised Download Manager will make it easier to locate downloaded files, and users can search the name of the website where the file came from. Also, the users will be able to resume downloads after restarting the browser or resetting their network connection.

    Simplifying add-on installation, the new version will make it possible to install extensions from third-party sites in fewer clicks.

    Also, in case a user wants to install Firefox add-ons, the new Add-ons Manager will also display a list of recommended add-ons and downloads from the Firefox website.

    5.Zoom


    Another feature that adds volume to the new version is Full page zoom. This functionality will let users zoom in and out of entire pages, scaling the layout, text and images, or optionally only the text size. Users settings will be remembered whenever they return to the site.

    6.Activity


    Another new feature, called activities, allows users to highlight text on a page, click on it, then instantly send it to another site, like a mapping, e-mail or blogging service.The new version promises to make users browsing experience more organised and clutter-free.

    Multiple text selections can be made with Ctrl/Cmd; double-click drag selects in "word-by-word" mode; triple-clicking selects a paragraph.

    7.Bookmark


    Next time a user wants to add a bookmark the new Firefox will make the task easier for him. He will be able to add bookmarks from the location bar with a single click. A dropdown box will let him name it, choose a folder to put it in as well as add a tag to categorise it.

    There will also be a Smart Bookmarks Folder from where he can access his recently-bookmarked and tagged pages, as well as his most-frequently visited pages.

    Another feature called Places Organiser will help the user view, organise and search through all his bookmarks, tags, and browsing history with multiple views and smart folders to store his frequent searches.



    8.New Developer Tools


    Firefox 3.0 has several developer tools too. Like there are new tools for graphics and font handling.

    New graphics and text rendering architectures in Gecko 1.9 provides rendering improvements in CSS, SVG as well as improved display of fonts with ligatures and complex scripts.

    Regarding colour management, Firefox 3.0 will be able to adjust images with embedded color profiles. There's also offline support option that aims to enable web applications to provide offline functionality.

    Just simple css code of 7 lines for Rounded corner : without any images

    Css Code:

    .rtop, .rbottom{display:block; }
    .rtop *, .rbottom *{display: block; height: 1px; overflow: hidden}
    .r1{margin: 0 5px; background:#9BD1FA;}
    .r2{margin: 0 3px; background:#9BD1FA;}
    .r3{margin: 0 2px; background:#9BD1FA;}
    .r4{margin: 0 1px; height: 2px; background:#9BD1FA;}
    .contain{background:#9BD1FA;text-align:center;}



    HTML Code:

    <div id="container" style="width:100px;">
    <b class="rtop">
    <b class="r1"></b>
    <b class="r2"></b>
    <b class="r3"></b>
    <b class="r4"></b>
    </b>
    <div class="contain">Ashok Sudani</div>
    <b class="rbottom">
    <b class="r4"></b>
    <b class="r3"></b>
    <b class="r2"></b>
    <b class="r1"></b>
    </b>
    </div>

    For Example:

    Ashok Sudani


    Monday, May 26, 2008

    Javascript in 600 Seconds

    Breakdown...

    Basic Types

    var intValue = 1;
    var floatValue = 3.0;
    var stringValue = "This is a string\n";
    var sqString = 'This is also a string';

    Javascript is a dynamically typed language. Variables are declared with the keyword var. Common simple types are supported.

    Arrays

    var emptyList = [];
    var homogenousList = [1, 2, 3];
    var heterogenousList = ["one", 2, 3.0];

    Javascript has built-in collection objects. The Array object is a dynamically typed sequence of Javascript values. They are created with the bracket notation [] or with the new operator on the Array object (e.g. new Array(5)).

    Property Maps

    var emptyMap = {};
    var homogenousMap = {"one": 1, "two": 2, "three": 3};
    var heterogenousMap = {"one": 1,
    "two": "two",
    "three": 3.0};

    Along with Arrays are the Object objects. They act as property maps with strings serving as keys to dynamically typed data.

    Access

    // Dot notation property access
    window.alert("Homogenous map property \"one\" "
    + homogenousMap.one);
    // Subscript notation property access
    window.alert("Homogenous map property \"two\" "
    + homogenousMap["two"]);

    Assignment

    homogenousMap["one"] = 10;
    homogenousMap.two = 20;

    Removal

    delete homogenousMap["one"];
    delete homogenousMap.two;

    Iteration

    for (var key in heterogenousMap) {
    window.alert("Heterogenous map property \""
    + key
    + "\" = "
    + heterogenousMap[key]);
    }

    Functions

    var callable = function (message) { // <-- notice assignment
    window.alert("Callable called with message = "
    + message);
    }


    function createClosure(initial) {
    var res = function () {
    initial = initial + 1;
    window.alert("Closure with modified state "
    + initial);
    }
    return res;
    }

    function callCallable(f, x) {
    f(x);
    }

    function composeCallables(f, g, x) {
    f(g(x));
    }

    Functions are first-class objects. That means that they can be created dynamically, stored, passed and returned just like any other value.

    Objects

    function MyObject(name, value) {
    this.name = name;
    this.value = value;
    }

    Javascript supports prototype based object orientation. Not a class type but an object constructor is created for new objects with particular properties. In the example above the this keyword used to reference the ''current instance'' of the object. The this object is essentially a property map with members accessed (and initialized) in this example with the dot notation.

    The object constructor, MyObject, is an object constructor not in how it's defined, which looks like any other Javascript function, but in how it's ''invoked''.

        var my = new MyObject("foo", 5);

    The new operator before the function invokes the function with a newly construced object as this and returns that the initialized object.

    Object Prototype

    Part of what makes a language object oriented is that data not only has properties but also ''behaviors''. Also known as: member functions; methods; and object messages. To implement a member function in Javascript one would be tempted to write something like what's below based on the member initialization exampled above.

    function BadObject(data) {
    this.data = data
    this.memberFunction = function () {
    // ...functions on data...
    }
    }

    While the code above will work without error, it does create a new closure for each member function for each new instance of the object. What's really required is a class level function that works on instance data. But remember, Javascript objects aren't class based but prototype based. So how do we implement "class" level member functions? (Skip to Implementation) Better yet, how do we implement "class" level members functions in general?

    Enter the prototype member.

    The internal object member, prototype, has language defined significance in that it is used for resolving property names if the property isn't found in the current property map. It's considered internal because, while the instance's prototype member is ''inherited'' from the ''constructor's'' prototype member, it cannot be accessed directly from the object instance itself. The defined prototype member is a property map itself which holds members for property name resolution. Consider the example below:

     var parentPropertyMap = {"bar": "I'm the bar"};

    // Define the constructor with inheritable properties
    function ChildObject(foo) {
    this.foo = foo;
    }
    ChildObject.prototype = parentPropertyMap;

    childPropertyMap1 = new ChildObject("I'm the foo1");
    childPropertyMap2 = new ChildObject("I'm the foo2");

    // Prints "childPropertyMap1.foo = I'm the foo1"
    window.alert("childPropertyMap1.foo = "
    + childPropertyMap1.foo);

    // Prints "childPropertyMap2.foo = I'm the foo2"
    window.alert("childPropertyMap2.foo = "
    + childPropertyMap2.foo);

    // Prints "childPropertyMap1.bar = I'm the bar"
    window.alert("childPropertyMap1.bar = "
    + childPropertyMap1.bar);

    // Prints "childPropertyMap2.bar = I'm the bar"
    window.alert("childPropertyMap2.bar = "
    + childPropertyMap2.bar);

    The member foo is an instance member added to the instance's property map during construction:

     function ChildObject(foo) {
    this.foo = foo;
    }

    while bar is in the constructor's prototype:

     var parentPropertyMap = {"bar": "I'm the bar"};
    ...
    ChildObject.prototype = parentPropertyMap;

    which is ''inherited'' during the new operation:

     childPropertyMap1 = new ChildObject("I'm the foo1");
    childPropertyMap2 = new ChildObject("I'm the foo2");

    In other words, the member, bar, is shared across all instances of ChildObject.

    Therefore, by implementing the prototype member of the constructor function, we can think of the constructor function itself as the "class" object. Complete with static class functions:

     function ClassObject() {}
    ClassObject.staticClassFunction = function(x) {
    return x * 2;
    }

    static class variables:

     function ClassObject() {}
    ClassObject.staticClassVariable = 5;

    shared member variables:

     function ClassObject() {}
    ClassObject.prototype.sharedMember = 5;

    and of course, shared member functions:

     function ClassObject(x) {
    this.x = x;
    }
    ClassObject.prototype.memberFunction = function(x) {
    return x * this.x;
    }

    Member Function Implementation

    function Message(message) {
    this.message = message;
    }

    Message.prototype.show = function() {
    window.alert("Message.show() with message = "
    + this.message);
    }


    Example Code

    //////////////////////////////////////
    // Basic Types
    var intValue = 1;
    var floatValue = 3.0;
    var stringValue = "This is a string\n";

    ///////////////////////////////////////
    // Array
    var emptyList = [];
    var homogenousList = [1, 2, 3];
    var heterogenousList = ["one", 2, 3.0];

    ///////////////////////////////////////
    // Property Map
    //
    var emptyMap = {};
    var homogenousMap = {"one": 1, "two": 2, "three": 3};
    var heterogenousMap = {"one": 1,
    "two": "two",
    "three": 3.0};

    ///////////////////////////////////////
    // Functions as values
    //
    var callable = function (message) { // <-- notice assignment
    window.alert("Callable called with message = "
    + message);
    }

    function createClosure(initial) {
    var res = function () {
    initial = initial + 1;
    window.alert("Closure with modified state "
    + initial);
    }
    return res;
    }

    ///////////////////////////////////////
    // Functions as arguments
    //
    function callCallable(f, x) {
    f(x);
    }

    function composeCallables(f, g, x) {
    f(g(x));
    }

    ///////////////////////////////////////
    // Objects
    //
    function MyObject(name, value) {
    this.name = name;
    this.value = value;
    }

    ///////////////////////////////////////
    // Objects with Member Functions
    //
    function Message(message) {
    this.message = message;
    }

    Message.prototype.show = function() {
    window.alert("Message.show() with message = "
    + this.message);
    }

    ///////////////////////////////////////
    // Demo Utilities
    //
    function quote(message) {
    return "\"" + message + "\"";
    }

    ///////////////////////////////////////
    // HTML Invoked demonstration
    //
    //
    function main() {
    window.alert("Integer = " + intValue);
    window.alert("Float = " + floatValue);
    window.alert("String = " + stringValue);

    for (var item in emptyList) {
    window.alert("Empty list item = " + item);
    }

    // Script style index iteration
    for (var i in homogenousList) {
    window.alert("Homogenous list item = "
    + homogenousList[i]);
    }

    // C style index iteration
    for (var i=0; i < heterogenousList.length; ++i) {
    window.alert("Heterogenous list item = "
    + heterogenousList[i]);
    }

    // Dot notation property access
    window.alert("Homogenous map property \"one\" "
    + homogenousMap.one);
    // Subscript notation property access
    window.alert("Homogenous map property \"two\" "
    + homogenousMap["two"]);

    for (var key in heterogenousMap) {
    window.alert("Heterogenous map property \""
    + key
    + "\" = "
    + heterogenousMap[key]);
    }

    callable("(Function value invoked)");
    closure();
    closure();

    callCallable(closure);
    composeCallables(callable, quote, "My Message");

    var my = new MyObject("foo", 5);
    window.alert("MyObject my.name = " + my.name);
    window.alert("MyObject my[\"value\"] = " + my["value"]);

    var msg = new Message("bar");
    for (var key in Message.prototype) {
    window.alert("Message prototype member \""
    + key
    + "\" = "
    + Message.prototype[key]);
    }

    window.alert("Message msg.message = " + msg.message);
    msg.show();
    }

    Friday, May 23, 2008

    40 Unique Resources for JavaScript Coders

    Are you an advanced JavaScript coder looking for more sites to sharpen your coding prowess? Maybe you’re a web designer wanting to double as a developer (or at least know enough to add a bit of rich content into your designs). Either way, if you’re looking for more information on the topic of JavaScript, the following resources are worth a gander.

    Reference, Resources, & Tutorials

    DevGuruDevGuru - JavaScript Quick Reference

    DevGuru provides an extensive list of JavaScript syntax, alphabetized similar to a glossary for easy scanning and searching.

    TechCheatSheets.com - Javascript Cheat SheetsTechCheatSheets.com - Javascript Cheat Sheets

    A roundup of 10 JavaScript cheat sheets in one place; includes cheatsheets for frameworks such as jQuery and Prototype.

    Google Groups - comp.lang.javascriptGoogle Groups - comp.lang.javascript

    If you’re looking for a community of JavaScript’ers comp.lang.javascript is an active and helpful community of developers.

    jQuery for DesignersjQuery for Designers

    jQuery for Designers is geared towards designers who want to learn about the jQuery library to add more dynamic content in their designs.

    Freetechbooks.com - Free Online JavaScript BooksFreetechbooks.com - Free Online JavaScript Books

    In this collection, you’ll be able to download 5 excellent e-books on the topic of JavaScript, all for free.

    DZoneDZone

    Although not purely a JavaScript resource, DZone regularly features articles, tutorials, resources, and news about JavaScript.

    W3Schools - JavaScript TutorialW3Schools - JavaScript Tutorial

    W3School’s section on JavaScript offers beginning to advanced JavaScript topics.

    15 Days Of jQuery15 Days Of jQuery

    Straight off the home page, 15 Days of jQuery has "Fantastic tutorials and example code that takes you from zero to hero in no time flat".

    The "Mootorial"The "Mootorial"

    //clientside’s tutorial on the mootools framework has a built-in console for you to try out JS code.

    Premade Scripts/Code

    AjaxDaddyAjaxDaddy

    A collection of downloadable DHTML scripts. AjaxDaddy provides a demo for the featured scripts.

    MiniAjax.comMiniAjax.com

    Another site with a collection of DHTML and Ajax code, similar to AjaxDaddy.

    JavaScript KitJavaScript Kit

    Here, you’ll find downloadable scripts, as well as tutorials and guides on JavaScript.

    Dynamic Drive JavaScript code libraryDynamic Drive JavaScript code library

    DHTML scripts organized into 16 categories including Calendars, Image Effects, Links & Tooltips, and more.

    DHTMLgoodies.comDHTMLgoodies.com

    Yet another place to get your fix of DHTML/Ajax scripts. They also have a fairly nice and straight-forward Ajax basics tutorial.

    4umi useful Javascript4umi useful Javascript

    A "database" of useful scripts and code snipplets that are updated fairly often.

    Articles & Blog Posts

    The Most Complete AJAX Framework and JavaScript Libraries List(124+)The Most Complete AJAX Framework and JavaScript Libraries List(124+)

    The title pretty much says it all — it’s a huge list of JS frameworks/libraries.

    The seven rules of unobtrusive JavaScriptThe seven rules of unobtrusive JavaScript

    This excellent article outlines seven things to keep in mind when trying to develop unobtrusive JavaScript solutions.

    How simple is making your javascript unobtrusive? Easy as PieHow simple is making your javascript unobtrusive? Easy as Pie.

    A basic introductory article on "unobtrusive JavaScript.

    The Top 40 Free Ajax & Javascript Code for Web DesignersThe Top 40 Free Ajax & Javascript Code for Web Designers

    A list of scripts geared towards web designers (i.e. not a lot of manual coding involved).

    How to choose a JavaScript frameworkHow to choose a JavaScript framework

    Outlines a few considerations when deciding which JS framework is right for you.

    Efficient JavaScriptEfficient JavaScript

    An article on quick tips for optimizing your JavaScript code.

    Ten Javascript Tools Everyone Should HaveTen Javascript Tools Everyone Should Have

    A list of JS code snipplets recommended to have in your coding arsenal; among them are numeric sorting and working with cookies.

    Serving JavaScript FastServing JavaScript Fast

    Optimal tips for serving/loading your JavaScript libraries quickly.

    The Great Browser JavaScript ShowdownThe Great Browser JavaScript Showdown

    A comparison of the top 4 web browsers (IE7, Firefox 2, Safari 3.0.4, and Opera 9.5) when it comes to handling JS.

    Quick guide to somewhat advanced JavaScriptQuick guide to somewhat advanced JavaScript

    A guide on Object-Oriented JavaScript coding.

    Blogs & News

    John Resig - BlogJohn Resig - Blog

    John Resig is the creator/lead developer of jQuery and author of "Pro Javascript Techniques".

    Ajaxian - JavaScript> JavaScript">Ajaxian - JavaScript

    Ajaxian is a news site about Ajax and Rich Internet Applications. Over 850 stories have been tagged under the JavaScript topic.

    Snook.CA - JavaScript CategorySnook.CA - JavaScript Category

    Snook.CA is Johnathan Snook’s site on the topic of web development. He writes about JavaScript, as well as other web dev topics.

    AjaxlinesAjaxlines

    Ajaxlines provides news and resources on the topic of Ajax. It currently has 140+ posts tagged under JavaScript.

    QuirksBlogQuirksBlog

    QuirksBlog is part of JavaScript guru/web developer Peter-Paul Koch’s QuirksMode.org. His book ppk on JavaScript is an excellent book to own.

    Ajaxonomy - BlogsAjaxonomy - Blogs

    Ajaxonomy is a wonderful resource for JavaScript’ers interested in Ajax and other web technologies. It has many posts tagged with JavaScript.

    Ajax Bestiary - A JavaScript Field GuideAjax Bestiary - A JavaScript Field Guide

    Ajax Bestiary is a regularly updated blog on JavaScript.

    Awesome Frameworks/Libraries

    Prototype JavaScript frameworkPrototype JavaScript framework

    Prototype was one of the first popular frameworks. Several libraries and frameworks are based on Prototype (or still require it).

    jQueryjQuery

    jQuery is lightweight, elegant, and touted as one of the easiest JS frameworks to use.

    mootoolsmootools

    My personal favorite.

    The Yahoo! User Interface Library (YUI)The Yahoo! User Interface Library (YUI)

    A big and extremely robust JavaScript toolkit by Yahoo!.

    JavaScriptMVCJavaScriptMVC

    JavaScriptMVC is a relatively new but very promising framework that offers a lot of unique components and features not found in other frameworks.

    script.aculo.usscript.aculo.us

    A robust effects library that’s been used by top websites such as Digg, Feedburner, and Apple; requires the inclusion of Prototype.

    Ext JSExt JS

    Another solid framework; it does have a restrictive license for commercial purposes. Check out the Web Desktop demo.

    MochiKitMochiKit

    MochiKit is a robust library that offers a lot of utility functions and effects classes.

    DojoDojo

    Dojo is another framework to consider. visit the Spotlight section on the website to see real companies using Dojo.

    Additional Resources

    Author’s note - April 16, 2008. After the publication of this article, I recieved several recommendations from readers about additional resources that didn’t make the list. I’d like to add even more excellent JavaScript resources worth taking a look at. I’ll add more as suggestions come along.



    Ref: http://sixrevisions.com/resources/40_resources_for_javascript_coders/