Tuesday, December 13, 2011

Generate random string of certain length

function genRandomString()

{
$length = 10;
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$string = null;
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}



$x=genRandomString();
echo substr($x,0,6);

?>

Monday, December 5, 2011

XML Parser in JSP

/**
*
* @author yashx1@gmail.com
*/

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.InputStream;
import java.net.URL;
import com.ahoy.ubid.utils.ParseUtil;

public class XmlParserHelp {

public static void main(String argv[]) {
try {
ParseUtil p = new ParseUtil();
URL xmlURL = new URL("http://www.newsletter.uahoy.com:8081/WeBid/api.auctions.php");
InputStream xml = xmlURL.openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
xml.close();
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("result");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("auctionTitle : " + p.getTagValue("auctionTitle", eElement));

//System.out.println("auctioneerId : " + p.getTagValue("auctioneerId", eElement));
}
}
} catch (Exception e) {
System.out.println(e);
}
}

}

Tuesday, November 22, 2011

Validation For Checking If User Is Above 18 Years Of Age

This code block returns if user is above 18 years of age. it also have checks for leap year and all. Although it is written in php but logic can be used in other platforms also.


//for calculating birthdate.
$birth_day = $_GET["day"];
$birth_month = $_GET["month"];
$birth_year = $_GET["year"];
$DATE = $birth_year . $birth_month . $birth_day;
function CheckAge($day, $month, $year)
{
$QCPASS=0;
if((empty($day))&&(empty($month))&&(empty($year))){
echo "atleast put the date, man!! :@";
$QCPASS=1;
}

elseif((!empty($day))&&(!empty($month))&&(!empty($year))){
if($day<1||$day>31){
echo "day is not valid. R u nuts ?? ";
$QCPASS=1;
}
elseif(($month<1||$month>12)&&$month!=2){
echo "month is not valid. may god help u.....";
$QCPASS=1;
}
elseif($month==2){
if($year%4==0&&$day>29)
{
echo "only till 29 in feb";
$QCPASS=1;
}
elseif($year%4!=0&&$day>28){
echo "only till 28 not a leap year bro...";
$QCPASS=1;
}
}
elseif($month==4||$month==6||$month==9||$month==11){
if($day>30){
echo "some months have 30 days only. sorry i cant change my calendar for u.";
$QCPASS=1;
}
}
if($QCPASS==0){

{
$NOW_year = gmdate('Y');
$NOW_month = gmdate('m');
$NOW_day = gmdate('d');

if (($NOW_year - $year) > 18){
echo "fine,u r above 18. do watever u lyk dude.....";
}
elseif ((($NOW_year - $year) == 18) && ($NOW_month > $month)){
echo "fine,u r above 18. do watever u lyk dude.....";
}
elseif ((($NOW_year - $year) == 18) && ($NOW_month == $month) && ($NOW_day >= $day)){
echo "fine,u r above 18. do watever u lyk dude.....";
}
else{
echo "mummy se bolo complan pilayein"; //if err exist
}
}
}
}
else if((!empty($day))||(!empty($month))||(!empty($year))){
echo "LOL......u missed some fields above. :D ";
}


}

CheckAge($birth_day, $birth_month, $birth_year);

?>

Generating MD5 Encrypted Password For Storing In Database


This code block generates hash code for an input such as password. But this must be kept in mind that this uses MD5 encryption method,which is only one way encryption method. Means if u want to decrypt the stored password in database it is not feasible. So you can also use reset passord method for setting new password:-

[This code is written in php,butt this can be used in any other desired language using the same function.]


function get_hash(){
$string = '0123456789abcdefghijklmnopqrstuvyxz';
$hash = '';
for ($i = 0; $i < 5; $i++){
$rand = rand(0, (34 - $i));
$hash .= $string[$rand];
$string = str_replace($string[$rand], '', $string);
}
return $hash;
}

Validation For e-mail Id

This code generates the valid output for an email. It is written for php but can be used in any language :-

function getmailvalid(){
$x=$_GET['email'];
$atpos=strpos($x,"@");
$dotpos=strripos($x,".");

if ($atpos<1 || $dotpos<$atpos+2 || $dotpos+2>=strlen($x)){
return 1;
}
else{
return 0;
}
}
$valideid=getmailvalid();