Thursday, 17 May 2012

Prevent resubmission of POST contents in PHP


How to prevent POST contents being resubmitted again and again when user refreshes the page?


This is much easier to explain with an example situation instead of generalized statement.

Example Situation

In Filename: input.php
<form action="process.php" method="POST">
Topic Title: <input type="text" name="title" /><br/>
Message: <input type="text" name="message" /><br/>
<input type="submit" />
</form>

In Filename: process.php

<?php


// process the contents of $_POST and then post a topic on user's behalf


...
...


// Show acknowledgement to user


?>

Flow

* (input.php) User submits Topic Title and Message in the input boxes and clicks on submit button.
* (process.php) POST content is processed and topic is posted.

The problem

After the user posted the topic content, she would land on process.php. If she refreshes the page now, the post content will be submitted again. If she does that again... again.... again and again... the topics will keep on getting posted with the same content. This can be done by users by mistake as well as intentionally.

Solutions

* Redirecting the user to other page instantly after POST is submitted.
* Adding Tokens

Redirection on process.php (updated code)

In Filename: process.php
<?php


// process the contents of $_POST and then post a topic on user's behalf


...
...


// Redirect User
header("Location: success.php");
exit;


?>


Further: Adding tokens (MySQL method)


Create a MySQL table called 'tokens' with only 3 fields: id (int(10)), token (varchar(32)), time (int(10))

In Filename: input.php (updated)
<form action="process.php" method="POST">
Topic Title: <input type="text" name="title" /><br/>
Message: <input type="text" name="message" /><br/>
<?php
$token=md5(mictrotime(true).mt_rand(1,9999)); // generate unique token
$res=mysql_query("INSERT INTO tokens SET token='$token', time='".time()."'");
?>
<input type="hidden" name="token" value="<?php echo $token; ?>" />


<input type="submit" />
</form>



In Filename: process.php
<?php


$TOKEN_EXPIRY=1800; //in seconds


$token=preg_replace('/[^w]/', '', $_POST['token']); //sanitize token
$active_tokenid=mysql_fetch_array(mysql_query("SELECT id, time FROM tokens WHERE token='$token'"));


if($active_tokenid[0]!='' && time()-$active_tokenid[1]<$TOKEN_EXPIRY){
$res=mysql_query("DELETE FROM tokens WHERE time<'".(time()-$TOKEN_EXPIRY)."'"); //cleanup old entries
// Redirect User
header("Location: expired.php");
exit;
}


// process the contents of $_POST and then post a topic on user's behalf


...
...


// Redirect User
header("Location: success.php");
exit;


?>

PHP function to add character(s) after every n characters

‎When you need to add character(s) after every n number of characters in a string, you can use the following function to do so. This function will be useful especially when you have something like a credit card number like:
5455471516335477


This function will convert this value to:
5455-4715-1633-5477
or
5455 4715 1633 5477





function addchar($str, $padding_at, $padding_char){
$arr = str_split($str, $padding_at);
$str=implode($padding_char, $arr);
}

Example usage:
$str='5455471516335477';
$str=addchar($str, 4, '-');
echo $str; // output will be 5455-4715-1633-5477

Functions to validate user inputs with PHP

Validating user inputs in PHP is very important from the view of your application's security,  integrity and user experience. If you fail, your applications may be prone to serious security vulnerabilities such as XSS, SQL injection and so on.

First of all make sure you have GPC magic quotes enabled in your php.ini file.

Here are few functions that you can use in your PHP scripts to validate different inputs.

Validating an email address field
// This function will return true if $email is not a valid email address


function NotValidEmail($email) {
 if (preg_match("/^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3}))$/i", $email)) {
  return FALSE;
 }
 else {
  return TRUE;
 }
}


Validating a link
//returns true if $link is a valid looking link

function linkis_valid($link){
$link=strtolower($link);
if(preg_match("|http://[a-z_1-9\:\/\-\.]+|",$link, $array)){
return true;
}
return false;
}

Allowing only numbers
// returns only numbers from $text

function only_numbers($text){
$text=preg_replace("|[^0-9]|","",$text);
return $text;
}

Allowing only alphanumeric characters

// returns only alphanumeric characters from $text
function cleanalpha($text){
$text=preg_replace("|[^0-9a-zA-Z_\-\.]|","",$text);
return $text;
}





Monday, 14 May 2012

Sidebar applications for Windows 7

Sidebar applications are intended to grab your favorite (customizable) information which would be consolidated into a dynamic view. This does makes your life better!

7 Sidebar - Dock your Windows Desktop Gadgets!





You may be aware about Windows Gadgets. This little Gadget will make all other gadgets to be presented in a neat and clean sidebar view. I really like this one because it is native to Windows and the project is active. There are numerous possibilities for its use.








Download link for 7 Sidebar:
http://nes.bplaced.net/sidebar7.html

Download link for Windows Desktop Gadgets:
Download Windows Desktop Gadgets

Do not feel restricted by the number of the gadgets Microsoft's official gadgets page. There are many other gadgets which are highly useful by they may not be catalogued on this page. Hence you can be wise and Google for what you want. :)


Google Desktop - Google Desktop is much more than a sidebar application



Google Desktop enhances your style of using Windows! It's a single solution to full text search over your email, files, music, photos, chats, Gmail, web pages that you've viewed, and more. By making your computer searchable, Desktop puts your information easily within your reach and frees you from having to manually organize your files, emails and bookmarks.

To launch search box, simply click Ctrl key two times. Voila! Now search anything on your computer!

Google Desktop doesn't just help you search your computer; it also helps you gather new information from the web and stay organized with gadgets and sidebar. Google Gadgets can be placed anywhere on your desktop to show you new email, weather, photos, personalized news, and more. Sidebar is a vertical bar on your desktop that helps you keep your gadgets organized.

Other features include:

  • Quick search box
  • Smart indexing
  • Multiple file types
  • Find deleted files
  • Deskbar and floating deskbar
  • Lock search
  • Microsoft Outlook integration

Download link for Google Desktop:

Note: Google has discontinued further development of this project. On the other hand, the application is still an ace for its utility.



Desktop Sidebar - Another powerful innovation towards organizing gadgets!


They say: Breaking News... Critical Appointments... Personal Photo Gallery... Convenient Toolbars... Favorite Cartoon... Instant Web Searches... Task Tracking... Music Library... Notes to File... Weather in Tokyo...

Everything can be consolidated in a sidebar!

It can fetch desired information from your system as well as from internet to present in a very efficient manner.  This project is quite active too.  There are numerous gadgets available for this application which makes it highly useful.




Download link for Desktop Sidebar:
http://www.desktopsidebar.com/

Wednesday, 9 May 2012

Lyrics of ‘Emptiness’ (tune mere jana kabhi nahi janaa ishq mera dard mera) ..


Ho love of mine..

with a song and a whine..

You’re harsh and divine..

like truths and a lie..

but the tale end is not here..

I’ve nothing to fear..

for my love is yell of giving and hold on…

in the bright emptiness..

in a room full of it..

is the cruel mistress ho ho o…

I feel the sunrise..

that nest all hollowness..

for i have the way to go.. not come…

And i feel so lonely yea..

There’s a better place from this emptiness..
And i’m so lonely yea..

There’s a better please from this emptiness.. yei yei yei ya….

Aa.. aa.. aa…..

Tune mere jaana..

Kabhi nahi jana..

Ishq mera dard mera.. haaye…

Tune mere jaana..

Kabhi nahi jana..

Ishq mera dard mera …

Aashiq teraaa..

Bheed mein khoya rehta hai..

Jaane jahaan a..

Puchho toh itna kehta hai..

And i feel so lonely yea..

There’s a better place from this emptiness..

And i’m so lonely yea..

Functions of SEBI


Securities and Exchange Board of India (SEBI) is a regulatory authority in India established under Section 3 of SEBI Act, 1992. SEBI Act, 1992 provides for establishment of Securities and Exchange Board of India (SEBI) with statutory powers for (a) protecting the interests of investors in securities (b) promoting the development of the securities market and (c) regulating the securities market.

1. Regulatory Functions


 a). Registration of brokers and sub-brokers and other players in the market

 b). Registration of collective investments schemes and Mutual Funds

 c). Regulation of stock exchanges and other self-regulatory organisations (SRO) merchant banks etc

 d) Prohibition of all fraudulent and unfair trade practices

 e) Controlling Insider Trading and take over bids and imposing penalties for such practices


2. Developmental Functions


 a) Investor education

 b) Training of intermediaries.

 c) Promotion of fair practices and Code of conduct for all S.R.O.s

 d). Conducting Research and Publishing information useful to all market participants