When you write a simple web based application, such as a tax calculator, it's always a good ides to echo back the values that your user filled in to the initial form as a part of the response page.
That way, anyone who prints out the resulting screen will know just WHAT the question was that the page answers!
It's also a good idea (a VERY good idea - read
mandatory) to sanitise the user's inputs, checking against injection attacks using dingle quotes, < characters, & characters, and so on.
These checks help avoid injection attacks with Javascript and HTML, and prepare you against injection attacks against a database you may add later
When you echo back the results page and present a further copy of the form that can be filled in, you should echo the values that the user entered the previous time ... and you need to be careful to sanitise the string and make sure it will accept space characters by quoting back the value when you echo it.
Users often need to correct or modify values and resubmit forms, and it's very antisocial of the programmer to present them with a completely blank form as a punishment for making just one mistake!
Finally, keep the bulk of your PHP towards the top of your code and the HTML towards the bottom.
That way, you can easily change the look and feel of the page without having to rework the logic, or can easily change the logic without major work on the look and feel
These are techniques that we'll teach you from day one of our
PHP Programming Course and we continue with on our
PHP Techniques Workshop.
I have just written and presented, in front of my delegates, an example to show these principles all in a single piece of code.
You can see the source
here and run it
here.
(written 2008-06-16 18:24:09)
Associated topics are indexed under
G906 - Well House Consultants - Programming Standards [2364] Getting it right from the start - new programmers - (2009-08-17)
[2363] Alpaca Case or Camel Case - (2009-08-16)
[2322] Looking for a practical standards course - (2009-08-05)
[1852] Perl and Blackberries - (2008-10-23)
[1596] Selling curry to the chinese takeaway - (2008-03-31)
[945] Code quality counts - (2006-11-26)
[356] Sudoku helper or sudoku cheat - (2005-06-23)
[343] Should I use structured or object oriented? - (2005-06-10)
[272] More to programming than just programming - (2005-04-08)
[148] Programming in isolation - (2004-12-11)
H117 - Security in PHP [2628] An example of an injection attack using Javascript - (2010-02-08)
[2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
[1779] Injection Attacks - avoiding them in your PHP - (2008-08-31)
[1747] Who is watching you? - (2008-08-10)
[1694] Defensive coding techniques in PHP? - (2008-07-02)
[1542] Are nasty programs looking for security holes on your server? - (2008-02-17)
[1482] A story about benchmarking PHP - (2007-12-23)
[1396] Using PHP to upload images / Store on MySQL database - security questions - (2007-10-19)
[1387] Error logging to file not browser in PHP - (2007-10-11)
[1323] Easy handling of errors in PHP - (2007-08-27)
[1086] Injection attacks - safeguard your PHP scripts - (2007-02-20)
[1052] Learning to write secure, maintainable PHP - (2007-01-25)
[947] What is an SQL injection attack? - (2006-11-27)
[920] A lion in a cage - PHP - (2006-11-10)
[426] Robust checking of data entered by users - (2005-08-27)
[345] Spotting a denial of service attack - (2005-06-12)
Some other Articles
Plenty of car parking at Well House Manor, Hotel, MelkshamAccounts in PHP - an OO demoAdding a button to a web page to print the pageAstroturfing - the online definitionPHP - Sanitised application principles for security and useabilitySoftware - changes and delays. But courses must run on time!CSS training - Cascading Style Sheets (UK course)A warm welcome for visitors from the USAComparing Objects in C++What a lot of files! (C++ / Polymorphism demo)