SL8R.co.uk - Code Zone

Code, Code and Code

SL8R.co.uk - Code Zone header image 4

Entries from May 2008

PHP: Avoiding mySQL Injections

April 30th, 2008 · No Comments

Sample code to avoid SQL injections using mysql_real_escape_string (http://uk.php.net/mysql_real_escape_string) which converts special characters to escape sequences to ensure they are suitable for submission to SQL
$mySQL = “UPDATE address SET postcode=’.mysql_real_escape_string($postcode).’ WHERE id=’.mysql_real_escape_string ($account).’”;
$myResult = mysql_query($mySQL);

addthis_url = ‘http%3A%2F%2Fwww.sl8r.co.uk%2F2008%2F04%2F30%2Fphp-avoiding-mysql-injections%2F’;
addthis_title = ‘PHP%3A+Avoiding+mySQL+Injections’;
addthis_pub = ”;

[Read more →]

Tags: Code

PHP: Require() and Include()

April 23rd, 2008 · No Comments

Essentially, require() and include() act in much the same way in that they both include the contents of another file - the only difference being that if include() fails then processing will continue (but produce a warning) but if require() fails then processing will stop with a fatal error (as well as produce the warning).

[...]

[Read more →]

Tags: Comment

PHP: Defining Constants

April 23rd, 2008 · 1 Comment

To define a constant, use the format:
define(constant_name,constant_value,case_insensitive)
e.g.
define(”MYCONSTANT”,”This is the value of the constant”,false);
print “Constant is ” . constant(”MYCONSTANT”);
?>

addthis_url = ‘http%3A%2F%2Fwww.sl8r.co.uk%2F2008%2F04%2F23%2Fphp-defining-constants%2F’;
addthis_title = ‘PHP%3A+Defining+Constants’;
addthis_pub = ”;

[Read more →]

Tags: Code

PHP: Page Inclusion using GET selection

April 18th, 2008 · No Comments

Sample code to handle page inclusion based on get values 
// parse the passed value
switch($_GET['page'])
{
  // check for page1
  case(’page1′):
  include ‘page1.php’;
  break;
  // check for page2
  case(’page2′):
  include ‘page2.php’;
  break;
  // send other enquiries to our 404 page
  default:
  include ‘page404.php’;
  break;
}

addthis_url = ‘http%3A%2F%2Fwww.sl8r.co.uk%2F2008%2F04%2F18%2Fphp-page-inclusion-using-get-selection%2F’;
addthis_title = ‘PHP%3A+Page+Inclusion+using+GET+selection’;
[...]

[Read more →]

Tags: Code

PHP: Encoding HTML Special Characters

April 18th, 2008 · 1 Comment

When working with untrusted information it is always worth running it through the htmlspecialchars function.
This function converts characters that are used by html into safer versions, e.g. ‘<’ becomes $lt;
myString = htmlspecialchars($_GET['fieldname'], ENT_QUOTES);

addthis_url = ‘http%3A%2F%2Fwww.sl8r.co.uk%2F2008%2F04%2F18%2Fphp-encoding-html-special-characters%2F’;
addthis_title = ‘PHP%3A+Encoding+HTML+Special+Characters’;
addthis_pub = ”;

[Read more →]

Tags: Code

PHP: Add a number of days to a date

April 11th, 2008 · 1 Comment

example code to add a number of days to a date
// add 5 days
$daystoadd=5;
// calculate hours
$hours=$daystoadd * 24;
// get today
$dd=date(d);
$mm=date(m);
$yy=date(y);
// calculate the new date
$newdate=date(”d-m-y”, mktime($hours, 0, 0, $mm, $dd, $yy));
// show the new date
echo “The new date is: ” . $new;

addthis_url = ‘http%3A%2F%2Fwww.sl8r.co.uk%2F2008%2F04%2F11%2Fphp-add-a-number-of-days-to-a-date%2F’;
addthis_title = ‘PHP%3A+Add+a+number+of+days+to+a+date’;
addthis_pub [...]

[Read more →]

Tags: Code

PHP: Format and show an expiration date

April 8th, 2008 · No Comments

A useful routine to calculate and show an expiration date based on a given number of hours
// Hours to expiration
$iHrs = 72;
// Date from database
$dbDate = ‘2008-04-08 16:53:19′;
// obtain current date and time
$iTimeNow = date(”U”);
// calculate expiry time
$iExpiryTime = ($iHrs * 3600) + strtotime(”$dbDate”) ;
// set the message to display
$strMessage = “”;
// compare times
if ($iTimeNow [...]

[Read more →]

Tags: Code

ASP: Teaser Text

April 7th, 2008 · No Comments

An early routine written in ASP to cut off a given string after a given number of words:
strText = “This is some example text To prove the Get teaser function”
Response.Write GetTeaser(strText,9)

function GetTeaser(strText,iWords)
  Dim strTaster
  Dim iCount
  strTaster = “”
  iCount = 0
  While (iCount < iWords)
    strWord = Left(strText,Instr(strText,” “))
    strTaster = strTaster & ” [...]

[Read more →]

Tags: Code

Shell Script: Random Number

April 5th, 2008 · 2 Comments

To get a random number at a shell script in Unix (I tried this on AIX 5 and it worked) use $RANDOM
echo $RANDOM
26558
echo $RANDOM
16737
echo $RANDOM
11669

addthis_url = ‘http%3A%2F%2Fwww.sl8r.co.uk%2F2008%2F04%2F05%2Fshell-script-random-number%2F’;
addthis_title = ‘Shell+Script%3A+Random+Number’;
addthis_pub = ”;

[Read more →]

Tags: Code

J2ME: Splash Screen Timer

April 4th, 2008 · No Comments

Timer Example
private Timer myTimer;
  myTimer = new Timer();
  // Show Splash Screen
  myTimer.schedule( new waitSplash(), 2000 );
// process the splash screen timer
  private void dismiss()
  {
  myTimer.cancel();
  // change to your form here
  myDisplay.getDisplay(this).setCurrent(yourForm);
  }
// if we reached here then timer has completed
  private class waitSplash extends TimerTask
  {
  public void run()
  {
  dismiss();
  }
  [...]

[Read more →]

Tags: Code