Sunday, 25 November 2012

Filtering PHP form inputs

There's a vital need to validate user inputs in PHP - to make sure that users have put something sensible into the boxes on your forms. And there are multiple ways of doing this:

a) You can check the incoming strings against regular expressions. In the old days you may have used the ereg functions, but these days you would use preg functions - slighly more complex, but more powerful and quicker. And the ereg functions have been deprecated. Using regular expressions, you need to define yourself what a particular string should look like - so you have a great flexibility

b) From PHP 5.2, you can use the filter_var function to filter what's in a variable. It will return FALSE if there's no match, or the value that the variable contains if it does match. For example, "does $sample contain an integer?":


  $result = filter_var($sample, FILTER_VALIDATE_INT);
And (sample program [here]) you get results like:
  Looking at 404
  Integer result - 404
  int(404)

and
  Looking at Graham Ellis
  NOT an Integer
  bool(false)


c) If you're using the Zend Framework, there's a validation element available within each form component / widget and you can use that to check is the form have been validly filled in.

So - which of these should you use? If you're using the MVC (Model View Controller) approach, using the Zend Framework, then it's logical to use the functions that are provided by the framework. For major systems, some sort of framework is an excellent idea - whether you use Zend, one of the others, or routines that you write yourself (your own framework) is up to you. If you use your own, then you'll be coding one of the other two options, once only, within your own framework setup as part of your standard.

filter_var is an excellent tool to use for checking specific types - email addreses, integers, IP addresses and the like; they're coded into PHP's functions so you san save yourself a great deal of work in formulating regular expressions, and you know they'll be updated and maitained with future releases as standards may change, rather than you having to update regular expressions yourself.

Saturday, 24 November 2012

Tasting OOP Concepts


If you have not yet entered the realm of Object Oriented Programming, then you are at a disadvantage, and you are falling behind fast.

OOP is essentially a method of programming with the use of classes, or Objects, which tie like things together, remove the need for repetition of code and perform the basic tasks of production very simply. Objects are essentially classes that collect a bunch of functions together and wrap them in a wrapper that can be reused over and over again without the need to rewrite functionality or procedures every time you need to do something.

Procedural Programming works by following a routine from the top to the bottom of each page as the server reads every file on your server. With OOP, there could be one or two objects being instantiated, which, in turn could instantiate a few, a hundred or a thousand other objects which could all perform certain tasks depending on variables passed into the objects. OOP is faster, simpler, easier to debug, uses less server resources, less code, is faster loading and more logical to work with once you figure out the basic principles. Go OOP - It changed my development style forever.

A small hint to make your PHP site much faster

If you've dug through your code to the best of your abilities, and optimized it as far as you can.  If your site is still slow, there are a few things you can do.  The first is install a byte code cacher like eAccelerator or APC.  Both of them store the compiled version of a PHP file in memory, which saves the time it would take for the server to compile the code.  While this is not a GREAT savings, it can be dramatic.  You can save between 5% and 25% off load times depending on your code.  The downside to this is two fold.  First, you must have root level access to the server to compile in the cacher.  The second problem is that they use memory.  And lots of it!  The average Joomla configuration will consume about 15 megs of ram.  PER APACHE CHILD.  One Apache child can serve one request at a time.  You can see how this will quickly limit the amount of traffic you can receive.  The other alternative it install a page cacher, such as my Joomla component.  By storing generated pages between requests, these programs can cut down page load times by over 90% or more.  Don't forget that you don't want to put a band aid on a broken bone, but if all else fails, these methods do work.

In general:

Match your hardware to your load, NOT to your generation time.  To upgrade to a dual Xeon server because your pages take 4 seconds to load on that Celeron is nothing short of a waste of money.  Poor code will run poorly no matter what hardware you put it on!  Optimize your code so that it loads fast on that Celeron (or shared host for that matter), and upgrade to the dual Xeon when your demand requires it!  You can do a lot with a little bit of processing power.  And don't let a host tell you that you need a dedicated server because your site is using too many resources.

Friday, 23 November 2012

General Issues with AJAX

AJAX is growing very fast and that is the reason that it contains many issues with it. We hope with the passes of time they will be resolved ab AJAX will be ideal for web applications. We are listing down few issues which AJAX has as a challenge.
Complexity is increased
  • Server side developers will need to understand that presentation logic will be required in the HTML client pages as well as in the server-side logic
  • Page developers must have JavaScript technology skills
AJAX-based applications can be difficult to debug, test, and maintain
  • JavaScript is hard to test - automatic testing is hard
  • Weak modularity in JavaScript
  • Lack of design patterns or best practice guidelines yet
Toolkits/Frameworks are not mature yet
  • Most of them are in beta phase
No standardization of the XMLHttpRequest yet
  • Future version of IE will address this
No support of XMLHttpRequest in old browsers
  • Iframe will help
JavaScript technology dependency & incompatibility
  • Must be enabled for applications to function
  • Still some browser incompatibilities
JavaScript code is visible to a hacker
  • Poorly designed JavaScript code can invite security problem

AJAX - A Closer Look


AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS and Java Script.
Conventional web application trasmit information to and from the sever using synchronous requests. This means you fill out a form, hit submit, and get directed to a new page with new information from the server.
With AJAX when submit is pressed, JavaScript will make a request to the server, interpret the results and update the current screen. In the purest sense, the user would never know that anything was even transmitted to the server.

Technologies Used in AJAX

JavaScript

  • Loosely typed scripting language
  • JavaScript function is called when an event in a page occurs
  • Glue for the whole AJAX operation

DOM

  • API for accessing and manipulating structured documents
  • Represents the structure of XML and HTML documents

CSS

  • Allows for a clear separation of the presentation style from the content and may be changed programmatically by JavaScript

XMLHttpRequest

  • JavaScript object that performs asynchrous interaction with the server