Sunday, January 27, 2013

How to Create CAPTCHA Protection using PHP

there are following stpes
1. html code
<img src="/captcha.php/?rand=0.608989860869691" id="captcha_image"/>
<a href="javascript:void(0);" onClick="refreshcaptcha();" >Refresh</a>
<input type="text" name="norobot" id="norobot" value="Enter Image Code" />

2. js
function refreshcaptcha(){
var capimage = document.getElementById('captcha_image').src;
var full_img=capimage.split('rand=');
document.getElementById('captcha_image').setAttribute('src', full_img[0]+'rand='+Math.random());
}

3.captcha.php
$randomnr = rand(1000, 9999);
$session["randomnr"] = md5($randomnr));


$im = imagecreatetruecolor(100, 38);

$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 150, 150, 150);
$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 200, 35, $black);

//path to font - this is just an example you can use any font you like:

$font = '/font/karate/Karate.ttf';

imagettftext($im, 20, 4, 22, 30, $grey, $font, $randomnr);

imagettftext($im, 20, 4, 15, 32, $white, $font, $randomnr);

//die(111);
//prevent caching on client side:
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header ("Content-type: image/gif");
imagegif($im);
imagedestroy($im);
exit;

4. On post action file
if (md5($data['norobot']) == $session["randomnr"]) {
// here you place code to be executed if the captcha test passes
} else {
// here you place code to be executed if the captcha test fails
echo "Please enter correct code.";
return;
}


Thursday, January 24, 2013

Magento : Create an attribute for customer mobile number

Consider package : Bd_Customermobile

1. Bd_Customermobile.xml

<?xml version="1.0"?>
<config>
<modules>
<Bd_Customermobile>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Bd_Customermobile>
</modules>
</config>

2. config.xml
<?xml version="1.0"?>
<config>
<modules>
<Bd_Customermobile>
<version>0.1.0</version>
</Bd_Customermobile>
</modules>
<global>
<helpers>
<customermobile>
<class>Bd_Customermobile_Helper</class>
</customermobile>
</helpers>
<models>
<customermobile>
<class>Bd_Customermobile_Model</class>
<resourceModel>customermobile_mysql4</resourceModel>
</customermobile>
</models>
<resources>
<customerattribute_setup>
<setup>
<module>Bd_Customermobile</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customerattribute_setup>
<customerattribute_write>
<connection>
<use>core_write</use>
</connection>
</customerattribute_write>
<customerattribute_read>
<connection>
<use>core_read</use>
</connection>
</customerattribute_read>
</resources>
</global>
</config>

3. mysql4-install-0.1.0.php
<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "bd_customer_mobile", array(
"type" => "varchar",
"backend" => "",
"label" => "Customer Mobile",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => "Additional attribute for mobile number"

));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "bd_customer_mobile");


$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();



$installer->endSetup();

Tuesday, January 15, 2013

Magento: Mobile number validation using custom class

HTML code

<input type="text" name="telephone" id="telephone" value="<?php echo $this->htmlEscape($this->getFormData()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text validate-custommobile" />



Js code

<script type="text/javascript">
//<![CDATA[

if(Validation) {
Validation.addAllThese([
['validate-custommobile','Enter correct mobile number',
function(v){
var timePat ="^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$";
// var matchArray = v.match(timePat);
if(v.length > 0){
if(v.length !=10){
return false;
}else if(v[0] != 9 || v[1] != 1 ){

//return false;
}else if(v[2]!=9 && v[2]!=8 && v[2]!=7){

return false;
}


return true;

}else {
return false;
}

}
]])};
var dataForm = new VarienForm('form-id', true);

//]]>

</script>
Subscribe to: Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /