Thursday, March 12, 2009

How to use MD5 and SHA1 in php (cryptography)

MD5 - Message-Digest algorithm 5
SHA1 - Secure Hash Algorithm
These are the two main inbuilt functions used in php to encrypt sensitive data.
SHA1 is now consider and recommended to be more secure than MD5.


<?php

echo md5('password'); //5f4dcc3b5aa765d61d8327deb882cf99

echo '<br /><br />';

echo sha1('password'); //5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

?>


If you run above code your will see the encrypted string of the word password.
You can clearly see that sha1 is longer than md5. sha1 is encrypted with more bits.

Once a value is encrypted you cannot reaverse the process. So how you gonna check the values again? check the following code.


<?php

$stored_password = sha1('my_pass');

$pass = $_POST['pass'] // input password

if($stored_password == sha1($pass))
{
echo 'correct password';
}
else
{
echo 'wrong password';
}

?>


Always remember to encrypt the input data with relevant encryption method before comparing values.

Tuesday, March 10, 2009

How to make the site feed available for browsers

IE RSS feed browser icon
RSS Feed Browser Icon FireFox

Firefox RSS feed browser icon
RSS Feed Browser Icon IE

You can easily make available your feed to IE or Firefox by adding one simple line of code.
Add this line right after   tag.


<link rel="alternate" type="application/rss+xml" title="siteName.com RSS News Feed" href="feed path" />



Thats it now your feed is available automatically by Firefox and IE, This will also help feed aggregator automatically find and index your feed.

Wednesday, March 4, 2009

How to create a popup DatePicker for a text box using jQuery

For this we can use a very nice widget created by jQuery.
First we will download the datepicker widget from the site
http://jqueryui.com/download

Only select the Datepicker widget to download


After downloading the zip file, extract the files into a folder called jquery.


<html>
<head>
<title>jQuery UI Datepicker - Default functionality</title>

<link type="text/css" href="jquery/theme/ui.all.css" rel="Stylesheet" />
<script type="text/javascript" src="jquery/jquery-1.3.1.js"></script>
<script type="text/javascript" src="jquery/jquery-ui-personalized-1.6rc6.js"></script>

<script type="text/javascript">
$(function() {
$("#datepicker").datepicker();
});
</script>
</head>
<body>

<p>Date: <input type="text" id="datepicker"></p>

</body>
</html>




You can read more about this Datepicker from here.
http://jqueryui.com/demos/datepicker/

Saturday, February 28, 2009

How to check for a valid date using php

php have given the checkdate() function to validate user inputted dates.
checkdate($month, $day, $year) all parameters must be integer (numbers).


<?php

$month = $input_month; // should be a number
$day = $input_day; // should be a number
$year = $input_year; // should be a number

if(checkdate($month, $day, $year))
{
echo 'Valid';
}
else
{
echo 'Not valid';
}

?>

Sunday, February 22, 2009

Get width and height of a swf / flash file using php

You can use getimagesize() function to get width and height of a swf /flash file.
getimagesize() is also used to get size of images.
To find out supported formats and return values go to this link
http://www.php.net/getimagesize

Following code is a simple example on how to get the width and height of a swf file.


list($width, $height) = getimagesize("file_name.swf");

Sunday, February 15, 2009

Right date format to store in mysql - YYYY-MM-DD HH:MM:SS

When using the php date function you can format the date in lot of ways.
But when you store date and time in mysql it is really convenient to store date in the mysql friendly format YYYY-MM-DD HH:MM:SS
If you use the above format you can easily use mysql date functions to manipulate and do calculation on stored dates without using php functions.

Example functions
DATEDIFF() - Subtract two dates
ADDDATE() - Add dates
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Tuesday, February 10, 2009

How to create foreign key in mysql (InnoDB)

Yes you can definitely create foreign key constraints on mysql. but it can only be enforced in InnoDB storage engine. The default storage engine is MyISAM, you cannot create foreign key constraints in MyISAM. So make sure you create your tables using InnoDB storage engine.
If you are using a phpMyAdmin to work with mysql, look at the following ScreenShot.



Remember, before making a column available for referencing you should make sure that child column has a Index.





Now using Relation view link you can easily create foreign key constraints



Thursday, February 5, 2009

How to redirect a page in Dropdown (select) onchage

Sometimes you need to redirect or refresh a page when you select a new option in a dropdown box (select box) with some url parameters (query string).
Look at following code and try it yourself.



<html>
<head>

<title>DropDown Redirect OnChange</title>

<script type="text/javascript">

function chng_page()
{
/*
get the selected option index
it start with 0
*/
var select_index = document.getElementById('names').selectedIndex;
// use alert(select_index); see the value

//get the value of selected option
var option_value = document.getElementById('names').options[select_index].value;

//redirect with value as a url parameter
window.location = 'page.php?id=' + option_value;
}
</script>

</head>

<body>

we are using onchange event to
call a javascript function
<br />
<select name="names" id="names" onchange="chng_page()">
<option value="0">select</option>
<option value="1">kuppiya.com</option>
<option value="2">kusal</option>
</select>

</body>
</html>