Write secured PHP

October 6th, 2008

Image Source:sitepointstatic.com

PHP programming was written to be an easy language to program. Completely opposite to being very security oriented. It was so until the 4.1 version. Undeniably, Php has grown to be popular in the short term. But it also created many, many, very badly written scripts. And it gave a very big number of programmers a mindset which is not inclined to the security aspect of programming. Maybe, the main reason behind it, is the automation of conversion by PHP of inputted data to script variables using or enabling though php.ini setting register globals. In the 4.1 version of PHP, this type of behavior was left enabled but was not recommended to be used. With PHP 4.2, this type of behaviour was disabled by default. It broke thousands and thousands of scripts worldwide. This dilemma led to forcing the coders to write more secure codes. They made scripts break. This was opposed stongly before but somehow they saw that the move was a smart one.

Flow Control and Iteration

September 29th, 2008


Image Source:theopensourcery.com

Flow control and iteration are two useful features in most programming languages. Without them, programs would be linear.Flow control means exactly what it sounds like. Control the flow. When using flow control, you are regulating the order in which the code is executed, the number of times it is to be executed or if it is executed at all. Flow control can be classified into three primary categories.

  • Conditionals - specifies whether or not to run a selected piece of code based on some conditions.
  • Iteration - also known as looping, it specifies that a piece of code be run a number of times.
  • Functions - allows you to create named blocks of code to be called by name in the script. This runs a piece of code multiple times from multiple places in the script.

Looping Statements Doesn’t loop?

August 10th, 2008


Image Source: www.pragprog.com
Did you know that looping statements doesn’t loop the whole if-else statement if you put an if-else statement in the looping statement? That’s right. Looping statements only goes through the if-else statements once. Once the looping statement activates the statements inside the if-else statement, the looping statement will only loop the statement inside the if-else statement. That is why it is not recommendable to use if-else statement inside a looping statement, such as do-while statements, while statements, and for statements. The only thing I recommend for you to use a switch statement. Looping statements reads the whole switch statement, possibly because switch statements requires a specific value for a condition. I don’t really know, but it works. Don’t worry. Switch statement also has a “Default” statement inside it that works just like the “else” statement in the if-else statement. The only disadvantage is that you won’t be able to set comparisons like greater than (») or less than («).

Arrays

July 1st, 2008

Arrays are what tables are to C-based programming languages and what databases are for SQL-based languages. Arrays or tables as they are sometimes called can be used to store the contents of several variables and to create one, you use the following syntax:

Array(key=>value)

The array in the syntax refers to the name of the array being created, the key is the index which is set automatically to a numeric character or string if none is specified. Value is the assigned value or content of the said array which can be seen easily in the following array creation example:

$b=array('z'=>‘Comedy”,’y'=>”Horror”,’x;’=>”Action”);
print_r($b);
?>

This piece of code would produce an output of :

Array ([0] => Comedy [1] => Horror [2] => Action)

More on array functions in the next posts.

Getting Started with PHP Programming

June 1st, 2008

The first step to get us on our way to programming in PHP would be to set up an ideal development environment. You need a Web Server software like Apache (which would be what we are going to use) which is only one of many out there. Most of these web servers are open-source meaning that they are free. Now, being free doesn�t mean that they are not up to standards for there are standards that are set by independent groups that are comprised of the many developers who together formulate or give a loosely defined set of standards for others to follow. Apache has versions for Linux but there are also for the Windows, Unix and Mac OS. The installer can be downloaded along with the detailed manual from PHP.net

Cookies - not the one you dunk in milk

May 29th, 2008

If you have been browsing the web foir quite sometime, then you might have come across cookies which allows your browser to remember passwords and other information that may have anything to do with your actions on a particular website. These can be preferences that would allow the browser or webpage on the browser to determine your habits online allowing it to perform the requested operations faster and easier. A cookie is called a client-based information for it is stored on the client computer (yours when you browse the web) and is taken or stored intact along with all the information it contains for a specified amount of time or till you delete your browsing history and cookies. If you have come across tracking cookies, these are information that defines you to a particular web page which is called upon on your next visit. When creating cookies with your page, there are specific parameters (cookies by the way are created using user-made code in PHP) that must be remembered so as not to fill up the hard disks of your client computers as they visit your website page again and again. The function setcookie is used with the following syntax:

setcookie(var1, var2, time)

The var 1 stands for the name of the cookie you are creating, with the var 2 containing the value of the said cookie and the last parameter is the time which specifies the amount of time it takes for the function to kill (terminate/delete) the cookie. A more defined and elaborate explanation of the function can be found on this <a href=”http://www.php.net/manual/en/function.setcookie.php”>page</aq>.

Templates and ready made Apps on the Web (Part 2)

May 25th, 2008

There are a lot of programmers who share code and sample applications which you can use for free (provided you clear it up first) but for budding programmers, they’re all there for the taking. Sites like <a href=”http://websitetips.com/php/”>websitetips.com</a>, <a href=”http://www.smartwebby.com/PHP/default.asp”>Smartwebby.com</a> and many other blogs and help sites allows access to tips from programmers like you making it a pleasant experience altogether. Can’t find a question, post questions to the many forums and you’ll get help from others in no time, you might even get help from the guru’s themselves who made the phenomenon that is PHP possible for all of us to use.

Templates and ready made Apps on the Web (Part 1)

May 21st, 2008

Thanks to the basic foundation on which PHP is founded on, the language being free of all legalities and other stuff that makes deployment of other languages on the many platforms quite expensive. But the many people who makeup the PHP community are not ones who keep knowledge to themselves for there are tons of free pre-made templates for applications and processes on the web available for programmers from all skill levels. They make it fun and easy to learn the language in no time and the community fosters growth to all of those who benefit from the use of PHP. Search online for help and you will surely get it in the form of forums and other helpful articles.

Further shortening of your code (Part 2)

May 17th, 2008

As stated in the previous entry, shortening or simplifying your source code makes it easier and better when you need to find bugs and a good example of this would be :

Example :
if ($Mickey == true) echo ‘Welcome to the Mickey Mouse Club House’;
elseif ($MainCharacter == false) echo ‘Hi Barney!’;

Making things a bit simpler, say by taking away the ‘==’ and ‘!=’ gives you;

if ($Mickey) echo ‘Welcome to the Mickey Mouse Club House.’;
elseif (!$MainCharacter) echo ‘Hi Barney!’;

Simplifying code should be the aim of each and every programmer (who’s head aches when you get some bugs you got to fix) so you get to code less and better than the other guy. Remember, we’re all on the same plane using the same free language so we should be helping others for the better.

Further shortening of your code (Part 1)

May 13th, 2008

Many people are using PHP for development and most have truly mastered the art of programming the easy way round. Sadly, many older web sites that used these older styles of programming are not only long but reminiscent of reading something similar to alien writing that is long and incomprehensible to the layman. But good for all of us, there are some people out there who are willing to share their learned ways to those who are just beginning to master the strengths and powers of PHP. The traditional PHP way of checking for Boolean expressions was long and filled with those double equal signs (==)and others that were akin to some other mathematically adept languages but there are simpler ways of doing things as there always is with any language. In the next post we would give and example of the concept.