Tuesday, August 18, 2009

How to force open a window that asks to "Open" or "Save As" the file?

Below is the working example of a code that would ask the user to "Open" or "Save" the file.

Note: There should not be any output displayed above the below mentioned code i.e. the rules for "headers" in php applies here.

<?
$contentFilePath = "/docs/xyz.pdf"; // Specify the path to the file that needs to be downloaded. This could also be a file that is generated dynamically.

if ($fd = fopen ($contentFilePath, "rb")) {
$fsize = filesize($contentFilePath);
$fname = basename ($contentFilePath);

// Specify the required headers
header("Pragma: ");
header("Cache-Control: ");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$fname."\"");
header("Content-length: $fsize");

fpassthru($fd);
}
?>

Generate random alphanumeric password in Php

A simple function which returns an autogenerated random alphanumerical password with the specified length.


Code:

<?
function NewPassword($len = 8)
{
$chars=array(
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","W","X","Y","Z", //upper-case
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x","y","z", //lower-case
"0","1","2","3","4","5","6","7","8","9" //numbers
);

srand((float) microtime() * 10000000);
$array=array_rand($chars, $len);

$password="";
foreach ($array as $key => $val)
$password.=$chars[$val];

return $password;
}
?>

Random password generation in Php

A simple random password generation function which generates various length (up to 32 characters) of password with numerals and alphabets.


Code:

<?php
function genPass($length = 8) //max length = 32 characters
{
return substr(md5(uniqid(rand(), true)), 0, $length);
}
?>

JavaScript code to strip HTML tags

This script removes any HTML tags from user entered data such as a TEXTAREA before the form is submitted. This is generally a good idea, as HTML tags can corrupt the rest of your form contents or even pose a security risk.

<script type="text/javascript">

// Strip HTML Tags (form) script- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use

function stripHTML(){
var re= /<\S[^><]*>/g
for (i=0; i<arguments.length; i++)
arguments[i].value=arguments[i].value.replace(re, "")
}

</script>

<form onSubmit="stripHTML(this.data1, this.data2)">

<textarea name="data1" style="width: 400px; height: 100px"></textarea><br />
<textarea name="data2" style="width: 400px; height: 100px"></textarea><br />
<input type="submit" value="submit">
</form>

Javascript Dynamic Text Area Counter

Below is the simple code to restrict the number of characters from being entered in the Text Area.


<SCRIPT LANGUAGE="JavaScript">
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) { // Trim it if too long
field.value = field.value.substring(0, maxlimit);
}
else { // otherwise, update 'characters left' counter
cntfield.value = maxlimit - field.value.length;
}
}
</script>

<form name="myForm" method="post">
<textarea name="textArea1" wrap="physical" cols="30" rows="5" onKeyDown="textCounter(document.myForm.textArea1,document.myForm.charLeft,100)" onKeyUp="textCounter(document.myForm.textArea1,document.myForm.charLeft,100)"></textarea>
<br>
<input readonly type="text" name="charLeft" size="3" maxlength="3" value="100"> characters left
<input type="Submit" name="Submit" value="Submit">
<br>
</form>

Regular Expressions Examples

Regular expression examples for decimals input

Positive Integers --- ^\d+$
Negative Integers --- ^-\d+$
Integer --- ^-{0,1}\d+$
Positive Number --- ^\d*\.{0,1}\d+$
Negative Number --- ^-\d*\.{0,1}\d+$
Positive Number or Negative Number - ^-{0,1}\d*\.{0,1}\d+$
Phone number --- ^\+?[\d\s]{3,}$
Phone with code --- ^\+?[\d\s]+\(?[\d\s]{10,}$
Year 1900-2099 --- ^(19|20)[\d]{2,2}$
Date (dd mm yyyy, d/m/yyyy, etc.) --- ^([1-9]|0[1-9]|[12][0-9]|3[01])\D([1-9]|0[1-9]|1[012])\D(19[0-9][0-9]|20[0-9][0-9])$
IP v4 --- ^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]){3}$

Regular expression examples for Alphabetic input

Personal Name --- ^[\w\.\']{2,}([\s][\w\.\']{2,})+$
Username --- ^[\w\d\_\.]{4,}$
Password at least 6 symbols --- ^.{6,}$
Password or empty input --- ^.{6,}$|^$
email --- ^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$
domain --- ^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$

Other regular expressions
Match no input --- ^$
Match blank input --- ^\s[\t]*$
Match New line --- [\r\n]|$

Generate a random password

Function will generate a random password with a default length of 8 or a user supplied length. Password will contain special characters, integers, and upper and lowercase characters.


Code:

<?
function newPass($maxLen=8,$pass='') {
srand((float) microtime() * 10000000);
shuffle($params=array_merge(range(0,9),range('a','z'),range('A','Z'),
array('$','_','+','*','!','%','#','@','&','-')));
for($i=0;$i<$maxLen;$i++) { $pass.=$params[rand(0,count($params)-1)]; }
unset($params);
return $pass;
}

$myNewPassword = newPass(15) # create and store a password of length 15
# Result => QEc*I#j5#cueUcZ

$myNewPassword = newPass() # create and store a password of default length 8
?>