View Single Post
Old 05-02-2008, 09:05 PM   #17 (permalink)
Highway of Life
The Wanderer
 
Highway of Life's Avatar
 
Join Date: May 2008
Location: Beware of programmers carrying screwdrivers
Posts: 21
Thanks: 0
Highway of Life is on a distinguished road
Default

Claims and or examples are not necessary to demonstrate basic and correct programming concepts.

That said, I’ll give a very basic example of proper usage and why type casting it of utmost importance when programming.
If you are interacting with the database, MySQL query for example, it’s important to use typecasting in the following example:
PHP Code:
$sql 'SELECT field1, field2, field3
        FROM some_table
        WHERE field_name = ' 
. (int) $var_id
$var_id cannot be a string otherwise you open it up to SQL Injection.
If you sanitise the variable with mysql_escape_string(), you are wasting resources... instead, you should force var type integer.
Enclosing the variable into single quotes and believing that it is somehow secure again gives a false sense of security and leaves the query open once again to SQL Injection.
Any string inserted into an SQL Query must be enclosed in single quotes -- along with mysql_escape_string() for the user-input, but integers should not, as demonstrated in the example above.

In the case of SQL UPDATE or INPUT, if you do not force var type int, you will also have SQL Errors if the value is not strictly an integer.

You don’t use shortcuts when security is in question -- you do it right.
All basic programming concepts with any language, especially PHP.
Anyone who has worked in C or Java knows how important typecasting is.
__________________
- Highway of Life
[ Software Engineer | PHP Developer | phpBB.com Team Member ]
phpBB Academy at StarTrekGuide
Send a message via AIM to Highway of Life Send a message via MSN to Highway of Life
Highway of Life is offline  
Reply With Quote