Thursday, April 09, 2009

How to implement Complex Password Validity Tester?

How to implement Complex Password Validity Tester?

This can be implemented by using JavaScript code to force passwords to be a combination of alphabets and numbers and force range of characters.

The below code makes sure that user enters combination of alphanumeric data and check for minimum number of chars. It checks for at least one alphabetic char, special char and one numeric chars.

<html>
<head>
<script language="javascript">
function Search_Array(ArrayObj, SearchFor){
var Found = false;
for (var i = 0; i < ArrayObj.length; i++){
if (ArrayObj[i] == SearchFor){
return true;
var Found = true;
break;
}
else if ((i == (ArrayObj.length - 1)) && (!Found)){
if (ArrayObj[i] != SearchFor){
return false;
}
}
}
}

function isAlphabeticSequence (passwdString) {

var i="0",
lower = "abcdefghijklmnopqrstuvwxyz",
upper = lower.toUpperCase(),
numbers = "0123456789",
badSequenceLength = 3,
noQwertySequences = true,
qwerty = "qwertyuiopasdfghjklzxcvbnm",
start = badSequenceLength - 1,
seq = "_" + passwdString.slice(0, start),
flg1=0;

for (i = start; i < passwdString.length; i++) {
seq = seq.slice(1) + passwdString.charAt(i);
if (
lower.indexOf(seq) > -1 ||
upper.indexOf(seq) > -1 ||
numbers.indexOf(seq) > -1 ||
(noQwertySequences && qwerty.indexOf(seq) > -1)
) {
flg1++;
break;
}
}

if (flg1 > 0) {
return true;
}
return false;
}

function isAlphabeticSequenceDesc (passwdString) {

var i="0",
lower_desc = "zyxwvutsrqponmlkjihgfedcba",
upper_desc = lower_desc.toUpperCase(),
numbers_desc = "9876543210",
badSequenceLength = 3,
noQwertySequences = true,
qwerty_desc = "mnbvcxzlkjhgfdsapoiuytrewq",
start = badSequenceLength - 1,
seq = "_" + passwdString.slice(0, start),
flg1=0;

for (i = start; i < passwdString.length; i++) {
seq = seq.slice(1) + passwdString.charAt(i);
if (
lower_desc.indexOf(seq) > -1 ||
upper_desc.indexOf(seq) > -1 ||
numbers_desc.indexOf(seq) > -1 ||
(noQwertySequences && qwerty_desc.indexOf(seq) > -1)
) {
flg1++;
break;
}
}

if (flg1 > 0) {
return true;
}
return false;
}

function isAlphabeticSequenceEvenChar (passwdString) {

var i="0",
evenChars="";

//Even
for(var i=0;i<passwdString.length;i+=2) {
evenChars += passwdString.charAt(i);
}

var lower_even = "abcdefghijklmnopqrstuvwxyz",
upper = lower_even.toUpperCase(),
numbers = "0123456789",
badSequenceAltLength = 3,
noQwertySequences = true,
qwerty = "qwertyuiopasdfghjklzxcvbnm",
start = badSequenceAltLength - 1,
seq = "_" + evenChars.slice(0, start),
flg1=0;

// enforce alphanumeric/qwerty sequence ban rules
for (i = start; i < evenChars.length; i++) {
seq = seq.slice(1) + evenChars.charAt(i);
if (
lower_even.indexOf(seq) > -1 ||
upper.indexOf(seq) > -1 ||
numbers.indexOf(seq) > -1 ||
(noQwertySequences && qwerty.indexOf(seq) > -1)
) {
flg1++;
break;
}
}

if (flg1 > 0) {
return true;
}

if (isAlphabeticSequenceRevChar(evenChars)) {
return true;
}

//Check for characters such as "bab4o1", "212epj"
if (isAlphabeticSequenceRevCharDesc(evenChars)) {
return true;
}

return false;
}

function isAlphabeticSequenceEvenCharDesc (passwdString) {

var i="0",
evenChars="";

//Even
for(var i=0;i<passwdString.length;i+=2) {
evenChars += passwdString.charAt(i);
}

var lower_even_desc = "zyxwvutsrqponmlkjihgfedcba",
upper_desc = lower_even_desc.toUpperCase(),
numbers_desc = "9876543210",
badSequenceAltLength = 3,
noQwertySequences = true,
qwerty_desc = "mnbvcxzlkjhgfdsapoiuytrewq",
start = badSequenceAltLength - 1,
seq = "_" + evenChars.slice(0, start),
flg1=0;

for (i = start; i < evenChars.length; i++) {
seq = seq.slice(1) + evenChars.charAt(i);
if (
lower_even_desc.indexOf(seq) > -1 ||
upper_desc.indexOf(seq) > -1 ||
numbers_desc.indexOf(seq) > -1 ||
(noQwertySequences && qwerty_desc.indexOf(seq) > -1)
) {
flg1++;
break;
}
}

if (flg1 > 0) {
return true;
}
if (isAlphabeticSequenceRevChar(evenChars)) {
return true;
}

//Check for characters such as "bab4o1", "212epj"
if (isAlphabeticSequenceRevCharDesc(evenChars)) {
return true;
}
return false;
}

function isAlphabeticSequenceOddChar(passwdString) {

var i="0",
oddChars="";

//Odd
for(var i=1;i<passwdString.length;i+=2) {
oddChars += passwdString.charAt(i);
}

var lower_odd = "abcdefghijklmnopqrstuvwxyz",
upper = lower_odd.toUpperCase(),
numbers = "0123456789",
badSequenceAltLength = 3,
noQwertySequences = true,
qwerty = "qwertyuiopasdfghjklzxcvbnm",
start = badSequenceAltLength - 1,
seq = "_" + oddChars.slice(0, start),
flg1=0;

for (i = start; i < oddChars.length; i++) {
seq = seq.slice(1) + oddChars.charAt(i);
if (
lower_odd.indexOf(seq) > -1 ||
upper.indexOf(seq) > -1 ||
numbers.indexOf(seq) > -1 ||
(noQwertySequences && qwerty.indexOf(seq) > -1)
) {
flg1++;
break;
}
}
if (flg1 > 0) {
return true;
}

if (isAlphabeticSequenceRevChar(oddChars)) {
return true;
}

//Check for characters such as "bab4o1", "212epj"
if (isAlphabeticSequenceRevCharDesc(oddChars)) {
return true;
}

return false;
}

function isAlphabeticSequenceOddCharDesc(passwdString) {

var i="0",
oddChars="";

//Odd
for(var i=1;i<passwdString.length;i+=2) {
oddChars += passwdString.charAt(i);
}

var lower_odd_desc = "zyxwvutsrqponmlkjihgfedcba",
upper_desc = lower_odd_desc.toUpperCase(),
numbers_desc = "9876543210",
badSequenceAltLength = 3,
noQwertySequences = true,
qwerty_desc = "mnbvcxzlkjhgfdsapoiuytrewq",
start = badSequenceAltLength - 1,
seq = "_" + oddChars.slice(0, start),
flg1=0;

for (i = start; i < oddChars.length; i++) {
seq = seq.slice(1) + oddChars.charAt(i);
if (
lower_odd_desc.indexOf(seq) > -1 ||
upper_desc.indexOf(seq) > -1 ||
numbers_desc.indexOf(seq) > -1 ||
(noQwertySequences && qwerty_desc.indexOf(seq) > -1)
) {
flg1++;
break;
}
}
if (flg1 > 0) {
return true;
}

if (isAlphabeticSequenceRevChar(oddChars)) {
return true;
}

//Check for characters such as "bab4o1", "212epj"
if (isAlphabeticSequenceRevCharDesc(oddChars)) {
return true;
}
return false;
}

function isAlphabeticSequenceRevChar(passwdString) {

var i="0",
lower = "abcdefghijklmnopqrstuvwxyz",
upper = lower.toUpperCase(),
numbers = "0123456789",
badSequenceRevLength = 2,
start = badSequenceRevLength - 1,
seq = "_" + passwdString.slice(0, start),
flg1=0;

for (i = start; i < passwdString.length; i++) {

seq = seq.slice(1) + passwdString.charAt(i);
lower_seq = seq.toLowerCase();
upper_seq = seq.toUpperCase();
lower_passwdString = passwdString.toLowerCase();
upper_passwdString = passwdString.toUpperCase();

if (
(lower.indexOf(lower_seq) > -1 && (lower_passwdString.charAt(i+1)==lower_seq.substring(0,1)) ) ||
(upper.indexOf(upper_seq) > -1 && (upper_passwdString.charAt(i+1)==upper_seq.substring(0,1))) ||
(numbers.indexOf(seq) > -1 && (upper_passwdString.charAt(i+1)==upper_seq.substring(0,1)) )
) {
flg1++;
break;
}
}
if (flg1 > 0) {
return true;
}
return false;
}

function isAlphabeticSequenceRevCharDesc(passwdString) {

var i="0",
lower_desc = "zyxwvutsrqponmlkjihgfedcba",
upper_desc = lower_desc.toUpperCase(),
numbers_desc = "9876543210",
badSequenceRevLength = 2,
start = badSequenceRevLength - 1,
seq = "_" + passwdString.slice(0, start),
flg1=0;

for (i = start; i < passwdString.length; i++) {

seq = seq.slice(1) + passwdString.charAt(i);
lower_seq = seq.toLowerCase();
upper_seq = seq.toUpperCase();
lower_passwdString = passwdString.toLowerCase();
upper_passwdString = passwdString.toUpperCase();

if (
(lower_desc.indexOf(lower_seq) > -1 && (lower_passwdString.charAt(i+1)==lower_seq.substring(0,1)) ) ||
(upper_desc.indexOf(upper_seq) > -1 && (upper_passwdString.charAt(i+1)==upper_seq.substring(0,1))) ||
(numbers_desc.indexOf(seq) > -1 && (upper_passwdString.charAt(i+1)==upper_seq.substring(0,1)) )
) {
flg1++;
break;
}
}
if (flg1 > 0) {
return true;
}
return false;
}

function testPasswd(passwdString)
{
var i="0";
var cch='0';
var nr=0;
var noSequential = true;

// enforce the no sequential, identical characters rule
if (noSequential && /([\S\s])\1/.test(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric identical characters not allowed.";
return true;
}

// Check for normal sequence "abc", "123"
if (isAlphabeticSequence(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}

// Check for normal sequence "cba", "321"
if (isAlphabeticSequenceDesc(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}

/******** Check for alternate characters in a password string Starts here ********/
//Check for alternate characters at Even positions (start position 0 [zero]) e.g. "a3bqc", "1j2d3k"
if (isAlphabeticSequenceEvenChar(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}

//Check for alternate characters at Even positions in descending order (start position 0 [zero]) e.g. "cqb3a", "3k2d1j"
if (isAlphabeticSequenceEvenCharDesc(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}

//Check for alternate characters at Odd positions (start position 0 [zero]) e.g. "3aqb6c", "j1d2k3"
if (isAlphabeticSequenceOddChar(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}

//Check for alternate characters at Odd positions in descending order (start position 0 [zero]) e.g. "qc3bta", "k3d2j1"
if (isAlphabeticSequenceOddCharDesc(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}
/******** Check for alternate characters in a password string Ends here ********/

//Check for characters such as "aba4o1", "121epj"
if (isAlphabeticSequenceRevChar(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}

//Check for characters such as "bab4o1", "212epj"
if (isAlphabeticSequenceRevCharDesc(passwdString)) {
document.getElementById("errorMessages").innerHTML = "Alphabetic/Numeric sequence not allowed.";
return true;
}

if(passwdString.length < 8) {
document.getElementById("errorMessages").innerHTML = "A password should have at least 8 characters";
return true;
}

if(passwdString.length > 12) {
document.getElementById("errorMessages").innerHTML = "A password should have no more than 12 characters";
return true;
}

for(i=0;i<passwdString.length;i++) {
cch=passwdString.charAt(i);
if(cch >= 'a' && cch <= 'z')
nr++;
}

if(nr == passwdString.length) {
document.getElementById("errorMessages").innerHTML = "Password too simple. Please use a mix of upper and lower case letters and numerics.";
return true;
}

//It must contain at least one number character
if (!(passwdString.match(/\d/))) {
document.getElementById("errorMessages").innerHTML = "Password must include at least one number.";
return true;
}

//It must start with at least one letter
if (!(passwdString.match(/^[a-zA-Z]+/))) {
document.getElementById("errorMessages").innerHTML = "Passwords must start with at least one letter.";
return true;
}

//It must contain at least one upper case character
if (!(passwdString.match(/[A-Z]/))) {
//alert("Password must include at least one uppercase letter.");
document.getElementById("errorMessages").innerHTML = "Password must include at least one uppercase letter.";
return true;
}

//It must contain at least one lower case character
if (!(passwdString.match(/[a-z]/))) {
document.getElementById("errorMessages").innerHTML = "Password must include one or more lowercase letters.";
return true;
}

//It must contain at least one special character
if (!(passwdString.match(/\W+/))) {
//alert("Password must include at least one special character - #,@,%,!");
document.getElementById("errorMessages").innerHTML = "Password must include at least one special character - #,@,%,!";
return true;
}

// Password should contain at least 4 different character "A1#dql"
passwordString = passwdString.toUpperCase();

var arr={};
var arrAlphabets = new Array();
var temp=passwordString.split("");
for(key in temp) {
temp1=temp[key];

arr[temp1] = arr[temp1]? arr[temp1] + 1 : 1;

if (!Search_Array(arrAlphabets, temp1)) {
arrAlphabets.push(temp1);
}
}

if (arrAlphabets.length < 4 ) {
document.getElementById("errorMessages").innerHTML = "Please enter at least 4 different alphabetical characters.";
return true;
}

if ( (passwdString.match(/\d/) && passwdString.match(/\d/g).length >= 2) ){
return false;
}
else if ( (passwdString.match(/\W/) && passwdString.match(/\W/g).length >= 2) ){
return false;
}
else if ( (passwdString.match(/\d/) && passwdString.match(/\d/g).length == 1) && (passwdString.match(/\W/) && passwdString.match(/\W/g).length == 1) ){
return false;
}
else {
document.getElementById("errorMessages").innerHTML = "It must contain at least 2 non-alphabetic characters.";
return true;
}

return false;
}

function testall()
{
if(testPasswd(document.form1.txtpassword.value)) {
document.form1.txtpassword.focus();
return false;
}
document.getElementById("errorMessages").innerHTML = "";
return true;
}
</script>
</head>
<body>
<form name=form1>
<span id="errorMessages"></span><br>
<br>
<b>Enter Password:    </b><input type="text" name="txtpassword" onkeyup="return testall();">
</form>
</body>
</html>

Wednesday, April 08, 2009

How websites can detect which mobile phone is viewing the website?

There is a free PHP script available on the net that detects which phone is viewing the website.

Reference URL: http://detectmobilebrowsers.mobi/

- What are the Guidelines to create a website for mobile phone?

A very good article is available on the net. Please check the below URL for details.

Reference URL: http://www.webcredible.co.uk/user-friendly-resources/web-usability/mobile-guidelines.shtml

Session ID not getting propogated in the URL properly?

When you generate URLs yourself using the SID variable, it must come first in the URL parameter list.

Example:
If SID expands to "some_session=123" then this does NOT work:

somefile.php?var1=value&some_session=123

The SID must come first in the query string:

somefile.php?some_session=123&var1=value

If the SID does not come first in the query string then session_start() does not recognise the session variable in the URL and creates a NEW session instead of loading the old one.

How to change the values in the php.ini file dynamically?

Have a look at http://de.php. net/manual/ en/ini.php# ini.list
You will see, that upload_max_filesize and post_max_size are settable by PHP_INI_PERDIR, what means that "Entry can be set in php.ini, .htaccess or httpd.conf"

Steps:
create .htaccess file and type the below code in this file

php_value upload_max_filesize 100M

and upload the file on that directory where u need it

How to capture video thumbnail with php scripting?

The tool is called imagemagick.

You have to use both ffmpeg and imagemagick as well. First get the frames from the video file using ffmpeg and then use the imagemagic command to create the thumbnails.

Refer the following sites for further details.

www. imagemagick. org/
http://ffmpeg. mplayerhq. hu/

How to resize an image to fit in the opened window?

You can use the JavaScript 'availheight' and 'height' properties (http://msdn. microsoft. com/workshop/ author/dhtml/ reference/ objects/obj_ screen.asp) to work out the size of the screen, and then use 'window.open' to open a window of that size.

Beware, not all browsers support these properties - some have there own versions, so you may need to code accordingly.

What is PHP FrameWork? And which one is good?

Frameweork is something which helps you do not reinvent the wheel again. For example, think of a authentication system. Obviously it would be having users table in database and needs the CRUD operations. How it would be if by configuration , a system gives you nearly all the functions to manipulate the user table. Or think a self build caching system, or session handling system up in just a few config settings.

Simphony is something matching ruby on rails framework and is very good. But if you do not intend to build high end applications or is light weight application your aim go for CodeIgnitor.

How can I work with XML in PHP ? What´s the best way ?

You can do it using two ways:
1. xml parser functions
2. xml dom functions

DOM functions are more complex & object oriented but handy. While parsing functions are simple to understand. Choice is yours.

How to call MySQL stored procedure in PHP with output parameter?

Check the below example.

//Say following is the store procedure you have

DELIMITER //
DROP PROCEDURE IF EXISTS yourdbname.gsp_GetCountryList //
CREATE PROCEDURE yourdbname.gsp_GetCountryList()
BEGIN
SELECT Id,strCountryName from youtable;
END//

//the above has to be run on mysql prompt


//change the php variables according to your setting


function iconnect()
{
$mysqli = new mysqli($YOURHOST, $YOURUSERNAME, $YOURPASSWORD, $YOURDATABASE);

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $mysqli;
}

$retval=iconnect();

$sql1 = "call sp1()";
$query1 = $retval->query($sql1);
while ($data1 = $query1->fetch_row()) {
echo "
\n".$data1[0].":".$data1[1];
}

How to access a file on a remote server?

There are 2 simple way to do that. First, using fopen(), second, cURL functions.

Below is a the sample code for servers that support cURL:
------------ --------- --------- --------- --------- -----
//////////// ///////// ///////// ///////// ////////
function url_exists($ strURL) {
$resURL = curl_init();
curl_setopt( $resURL, CURLOPT_URL, $strURL);
// return the result instead of echoing it
curl_setopt( $resURL, CURLOPT_RETURNTRANS FER, 1);
// time out after 5 seconds of no response
curl_setopt( $resURL, CURLOPT_TIMEOUT, 5);

$result = curl_exec ($resURL);
if ($result === false) {
curl_close($ resURL);
return false;
}

$intReturnCode = curl_getinfo( $resURL, CURLINFO_HTTP_ CODE);
curl_close ($resURL);

if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode !=
304) {
return false;
}

return true;
}
//////////// ///////// ///////// ///////// ////////

How to generate a document using php?

There are the following 2 ways to achieve this:

1. MS Office documents can be created on Windows platforms using COM called from PHP :-

http://web.informba nk.com/articles/ technology/ php-office- documents. htm

2. There's a class at http://www.phpclass es.org/browse/ package/2631. html for Word documents that doesn't require COM, but this just uses the XHTML
alternative method.

How to find GMT time for partcular locations (country) using dynamically using php script?

Using Php Script:

function zonedate($layout, $countryzone, $daylightsaving) {

if ($daylightsaving) {

$daylight_saving = date('I');
if ($daylight_saving) {
$zone=3600* ($countryzone+ 1);
}
}
else {
if ($countryzone> >0){$zone= 3600*$countryzon e;}
else {$zone=0;}
}

$date=gmdate( $layout, time() + $zone);
return $date;
}

//I'm from north of spanish state, my GMT zone is -1, put yours and you will have the time,

$zonealDate = zonedate('d- m-y H:i a ',-1,true);

echo $zonealDate;

Using Javascript:

new date();

How to download fonts dynamically, when the user is viewing the site?

Well there is a technique of uploading the font archive that you want to use to the server and use it with php and it renders it into an image the text that you use with that font.

Below is the link to check the site
http://www.alistapart.com/articles/dynatext

Warning: Unknown: Node no longer exists in Unknown on line 0 solution

I got the following error on my php pages using SimpleXMLElement and $_SESSION function.

The solution is to add a session_destroy() function at the end of any file which contains session_start() and it should work!

SEO and Dynamic Site in PHP?

If your web site is designed using PHP that displays a static/same page title from an include file for all the pages of the site, it looks like a good idea, but in a longer run you may loose on SEO ratings since all your pages would have the same page title.

Here is a better and easy way of using a single "include" file for the header while still creating unique page titles for every page. This also works for meta tags. Please check the example below.

Here you would need a header file say "header.php" that would be included at the top of every page on your site - this generates the info contained in <head>.
The page we want to create a unique title tag for will be called "product.php":

Add the below code to "header.php". An "else" statement would display a default Meta tags if you have not specified the values/variable on the website pages:

<title><?php if(isset($title)) { print $title; } else { print "Default title comes here"; } ?></title>

<meta name="keywords" content="<?php if(isset($keywords)) { print $keywords; } else { print "keyword1,keyword2,keyword3,etc"; } ?>" />

<meta name="description" content="<?php if(isset($description)) { print $description; } else { print "Default description tag goes here."; } ?>" />



Add this code to product.php to specify the info you want used in the tags:

<?php

$title = "Add title to product.php here";

$keywords = "keyword1, keyword2, keyword3, etc";

$description = "Specify description of product.php here.";

require_once("header.php");

?>

Thats it! and you are good to go! :-)

How to upload same file to 2 different Servers.

This could be done in 2 simple Steps:

1. First upload to the main server or the one where the php scripts are hosted.

2. Second, either via a cron job running after a specific time or in the upload script itself copy the files to the other server.

The below sample code is to copy the file to second server using FTP functions.

// Details of the second server.
$host = 'host';
$user = 'username';
$pass = 'password';

//Path of the uploaded file i.e. on first server.
$local_file = 'path of file/file.txt' ;

//Path of the second file.
$remote_file = 'path of file/file.txt' ;

// set up basic connection
$conn_id = ftp_connect($host);

// login with username and password
$login_result = ftp_login($conn_id, $user, $pass);

// check connection
if ((!$conn_id) || (!$login_result)) {

echo "Cannot initiate connection to host";
exit;
}
else {

// upload the file
$upload = ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY);

//Close FTP connection
ftp_close($conn_id);
}