Wednesday, September 4, 2013

Very Simple PHP Captcha

Very Simple PHP Captcha :
Very easy and it requires only a couple lines of code. Please follow the below steps.

Step 1
The first step is to create a HTML form which is later submitted.
I assume you already have a HTML form, so I'll only focus on the captcha code and not all the other fields.
 
<?php
session_start();
echo '<form action="something.php" method="post">';
$rand_int1 = substr(mt_rand(),0,2);
$rand_int2 = substr(mt_rand(),0,1);
$rand_int3 = substr(mt_rand(),0,1);
$captcha_answer = $rand_int1 + $rand_int2 - $rand_int3;
$_SESSION['captcha_answer'] = $captcha_answer;
echo 'What is '.$rand_int1.' + '.$rand_int2.' - '.$rand_int3.'?<br>
<input type="text" name="captcha">
<input type="submit" value="Do captcha">
</form>';
?>


Step 2
The second step is collecting the form data and check if captcha is correct.
 
<?php
session_start();
$captcha = $_POST['captcha'];
$captcha_answer = $_SESSION['captcha_answer'];
 
if($captcha != $captcha_answer) {
    echo 'Captcha is incorrect!';
}
else {
    echo 'Captcha is correct, congratulations! :)';
}
?>


Thanks : ITDB

Tuesday, September 3, 2013

Function for Date Selection


Function for Date Selection. Its simply superb & cool...



<?PHP

    FUNCTION DateSelector($inName, $useDate=0)
    {
        /* create array so we can name months */
        $monthName = ARRAY(1=> "January", "February", "March",
            "April", "May", "June", "July", "August",
            "September", "October", "November", "December");

        /* if date invalid or not supplied, use current time */
        IF($useDate == 0)
        {
            $useDate = TIME();
        }

        /* make month selector */
        ECHO "<SELECT NAME=" . $inName . "Month>\n";
        FOR($currentMonth = 1; $currentMonth <= 12; $currentMonth++)
        {
            ECHO "<OPTION VALUE=\"";
            ECHO INTVAL($currentMonth);
            ECHO "\"";
            IF(INTVAL(DATE( "m", $useDate))==$currentMonth)
            {
                ECHO " SELECTED";
            }
            ECHO ">" . $monthName[$currentMonth] . "\n";
        }
        ECHO "</SELECT>";

        /* make day selector */
        ECHO "<SELECT NAME=" . $inName . "Day>\n";
        FOR($currentDay=1; $currentDay <= 31; $currentDay++)
        {
            ECHO "<OPTION VALUE=\"$currentDay\"";
            IF(INTVAL(DATE( "d", $useDate))==$currentDay)
            {
                ECHO " SELECTED";
            }
            ECHO ">$currentDay\n";
        }
        ECHO "</SELECT>";

        /* make year selector */
        ECHO "<SELECT NAME=" . $inName . "Year>\n";
        $startYear = DATE( "Y", $useDate);
        FOR($currentYear = $startYear - 5; $currentYear <= $startYear+5;$currentYear++)
        {
            ECHO "<OPTION VALUE=\"$currentYear\"";
            IF(DATE( "Y", $useDate)==$currentYear)
            {
                ECHO " SELECTED";
            }
            ECHO ">$currentYear\n";
        }
        ECHO "</SELECT>";

    }
?>

<HTML>
<BODY>
<FORM>
Choose a Date: <?PHP DateSelector( "Sample"); ?>
</FORM>
</BODY>
</HTML>

Thanks : Psoug

Tuesday, June 4, 2013

Peke Upload :- File Uploading with progress Bar


PekeUpload

It is a jQuery plugin that allows you to easily add multiple or single file upload functionality to your website. This plugin uses html5 only.

Some Features include:

Theming (Twitter Bootstrap ready)
Set File size limit
Set File extensions restrictions
Set custom error messages
Real-Time Progress Indicators
And others more...

Requirements

jQuery 1.4.x or greater
Web browser capable to render HTML5
If you're using Bootstrap, you will need bootstrap.css from v2.2.x or greater
A server capable of parsing PHP, ASP.Net, Cold Fusion, or similar server-side language

Implementation

Download pekeUpload zip from https://github.com/pekebyte/pekeUpload
Unzip pekeUpload zip and install it as per the documentation given.

This is a simple one which i have used till this time. This will work at the time of selecting a file. This will work without any errors in server. The only issue i faced is the jQuery conflit. You can fix it easily.


Tuesday, May 14, 2013

Form submission to 2 tables using forign key


Form submission

Module -> phpfox_sample -> Template -> Default -> Controller
usersubmit.html.php

<form name="form" method="post" action="{url link='product.usersubmit'}" enctype="multipart/form-data">
Name : <input type="text" name="val[name]"> <br><br>
Content : <input type="text" name="val[content]"><br><br>
<input type="submit" name="val[submit]" > </br>
</form>
{if isset($return)}
<br><br>
successfully Added<br><br>
User id : {$return.id}<br>
Privacy : {$return.privacy}
{/if}<br><br>


Module -> phpfox_sample -> Include -> Component -> Controller
userdisplay.class.php

class Product_Component_Controller_Usersubmit extends Phpfox_Component
{
public function process()
{
if($aVals = $this->request()->get('val')){
unset($aVals['submit']);
// d($aVals);
// exit();
$return = Phpfox::getService('product.usersubmit')->insert($aVals);
// $id = $return['id'];
// $privacy= $return['privacy'];
//$this->template()->assign(array('id'=>$id,'privacy'=>$privacy));
$this->template()->assign(array('return'=>$return));
}
}
}


Module -> phpfox_sample -> Include -> Service
userdisplayservice.class.php

class Product_Service_Usersubmit extends Phpfox_Service
{
/**
*function to insert value to database (two tables)
* @param type $argument
* @return type id, privacy
* @author Arun george <arun@arun-g.in>
* @since 22-11-2012
*
*/
function insert($argument)
{
$nameArray['name'] = $argument['name'];
$id =
$this->database()->insert(Phpfox::getT('newuser'),$nameArray);
$contentArray['content'] = $argument['content'];
$contentArray['privacy'] = $id;
$privacy = \
$this->database()->insert(Phpfox::getT('usercontent'),$contentArray);
$return['id'] = $id;
$return['privacy'] = $privacy;
return $return ;
}
}

Displaying values from DataBase based on some Conditions...


View

module -> phpfox_sample -> Template -> Default -> Controller
userdisplay.html.php
<form name="form2" method="post" action="{url link='product.userdisplay'}"
enctype="multipart/form-data">
Name : <select name="val[user_id]">
<option value="0">select </option>
{foreach from=$userdetails name=userdetail item=detailName}
<option value="{$detailName.u_id}">{$detailName.name} </option>
{/foreach}
</select>
<input type="submit" name="val[submit]" > </br>
</form>
<table style="border: 1px solid #0088CC; width: 500px; ">
{foreach from=$details name=detail item=detailValue}
<tr style="border: 1px solid #0088CC;">
<td style="border-right: 1px solid #0088CC;">{$detailValue.content}</td>
<td style="border-right: 1px solid #0088CC;">{$detailValue.privacy}</td>
</tr>
{/foreach}
</table>

module -> phpfox_sample -> Include -> Component -> Controller
userdisplay.class.php

class Product_Component_Controller_Userdisplay extends Phpfox_Component
{
$userdetails =
Phpfox::getService('product.userdisplayservice')->getUser();
if($userdetails)
$this->template()->assign(array('userdetails'=>$userdetails));
public function process()
{
if($aVals = $this->request()->get('val'))
{
unset($aVals['submit']);
$details = Phpfox::getService('product.userdisplayservice')->get($aVals);
if($details)
$this->template()->assign(array('details'=>$details));
}
}
}

module -> phpfox_sample ->Include -> Service
userdisplayservice.class.php

class Product_Service_Userdisplayservice extends Phpfox_Service
{
/**
*Function to get rows from table
* @return type
* @author Arun George <arun@arun-g.in>
* @since 20-11-2012
*/
function getUser()
{
$uname = $this->database()->select('*')
->from(Phpfox::getT('newuser'))
->execute('getSlaveRows');
// d($row);
return $uname;
}
/**
*Function to get rows from table
* @return type
* @author Arun George <arun@arun-g.in>
* @since 20-11-2012
*/
function get($aVals)
{
// d($aVals);
// exit();
//
$cond = '';
if($aVals['user_id']==1)
{
$cond = ' r.privacy IN(1,2,3)';
}
elseif($aVals['user_id']==2)
{
$cond = ' r.privacy IN(1,2)';
}
elseif($aVals['user_id']==3)
{
$cond = ' r.privacy IN(3)';
}
else{
$cond = ' r.privacy IN(0)';
}
$row = $this->database()->select('r.*')
->from(Phpfox::getT('usercontent'), 'r')
->where($cond)
->execute('getRows');
return $row;
}
}