Friday, April 13, 2007

md5 Checking

<html>
<head><title>Muntenari.Com: md5 Checking</title>
</head>
<body>
String:
<form action=? method=post>
<input type=text name=str>
<input type=submit value=Submit>
</form>
<p>
<?
if($_POST['str']) echo "md5('".$_POST['str']."') : ".md5($_POST['str']);
?>
</body>
</html>

To try the script above please visit http://muntenari.com/exercise/md5.php

Thursday, April 12, 2007

Fatal error: Call to undefined function: imagecreatetruecolor

Here is the solution.

1. Open php.ini file.
2. Enable extension=php_gd2.dll by deleting semicolon ';'

Create Random String Image

<?

$string = stringGenerator();
//setcookie("ivstrcookie", $string, time()+3600);

function imageGenerator($string) {
header("Content-type: image/png");

$x = 100;
$y = 30;
$im = imagecreatetruecolor($x,$y);
$putih = imagecolorallocate($im, 255, 255, 255);
$orange = imagecolorallocate($im, 220, 210, 60);
$hitam = imagecolorallocate($im, 0, 0, 0);
$abu2 = imagecolorallocate($im, 200, 200, 200);
imagefill($im,0,0,$putih);
// Border
imagerectangle($im,0,0,$x-1,$y-1,$hitam);

imagestring($im, 6, $x - 78, $y - 24, $string, $hitam);

imagepng($im);
imagedestroy($im);
}

function stringGenerator() {
$strlen = 6;
$string = '';
for ($i=1;$i<=$strlen;$i++) {
$rand2 = round(rand (0, 2)) + 1;

if ($rand2 == 1) {
$bottomlimit = 48;
$toplimit = 57;
}
elseif ($rand2 == 2) {
$bottomlimit = 65;
$toplimit = 90;
}
else {
$bottomlimit = 97;
$toplimit = 122;
}

$rand1 = round(rand (0,$toplimit-$bottomlimit)) + $bottomlimit;
$string .= chr($rand1);
}

return $string;
}

imageGenerator($string);
?>

Save as image.php. Example available at http://muntenari.com/imageverification.php

Wednesday, February 14, 2007

BASH Script To Download File

#!/bin/bash
#
# filename: download.sh
# desciption: script to download log files from other server
# how to run: shell>./download.sh

BASE_DIR="/localserver/muntenari/log"
BASE_URL="http://www.muntenari.com/target-logs"

USER="username"
PASS="password"

# verbose, for debuging only
WGET="/usr/bin/wget --http-user=${USER} --http-passwd=${PASS} -c"


LOG_DATE=`cat ${BASE_DIR}/latihan/webalizer/lastdate`
LOG_DIRS=${BASE_DIR}/`echo "${LOG_DATE}" | awk '{print substr($_,0,6)}'`

ACCESS_LOG="${BASE_URL}/access_log.${LOG_DATE}.bz2"

# check logs dir. if not exist, create now
if [ ! -d ${LOG_DIRS} ]; then
mkdir -p ${LOG_DIRS}
fi

cd ${LOG_DIRS}

# start download
${WGET} ${ACCESS_LOG}

Module Net::FTP To Download File

Code below use to download a file from other server and work out on linux command line. I use cronjob to run this script to download daily log of our apache web server.

#!/usr/local/bin/perl

use Net::FTP;

$ftpSite = "www.muntenari.com";
$ftpUsername = "username";
$ftpPassword = "password";
$ftpDirectory = "/var/log/httpd";
$logFilesDirectory = "/localserver/home/";
$ftpFilename = "data.tar.gz";

unless (-d $logFilesDirectory) {
print STDERR "Creating directory " . $logFilesDirectory . "\n";
mkdir $logFilesDirectory || die "Cannot create " . $! . "\n";
}

$ftp = Net::FTP->new($ftpSite) || die "Can't connect to $ftpSite : $@\n";
$ftp->login($ftpUsername, $ftpPassword) || die "Couldn't login to $ftpSite \n";
$ftp->cwd($ftpDirectory) || die "Couldn't change directory\n";
$ftp->binary() || die "Couldn't change to binary\n";

print STDERR "copying $ftpFilename from $ftpSite .....\n";
$noGetFile = 0;
$ftp->get($ftpFilename,"$logFilesDirectory/$ftpFilename") || ($noGetFile = 1);

if ($noGetFile == 1) {
print "Couldn't get $ftpFilename\n";
}
else {
print "success\n";
}

1;

Tuesday, February 13, 2007

Login Session Use Cookie

#!/usr/bin/perl -w

use CGI qw/:standard/;
use strict;

my $h = CGI->new;

our $mode = param("mode") if (defined(param("mode")));

our $userlog = "";
our $pswdlog = "";
our $cexprd = "+5m";

if ($mode eq "login") {
if (defined(param("username")) && defined(param("password"))) {
$userlog = param("username");
$pswdlog = param("password");
}
else {
$userlog = "";
$pswdlog = "";
}
}
elsif ($mode eq "logout") {
$userlog = "";
$pswdlog = "";
$cexprd = "-1";
}
else {
$userlog = $h->cookie(-name=>'username') || "";
$pswdlog = $h->cookie(-name=>'password') || "";
}

our $cuserlog = $h->cookie( -name=>'username',
-value=>$userlog,
-expires=>$cexprd,
-path=>'/',
-domain=>'',
-secure=>0);
our $cpswdlog = $h->cookie( -name=>'password',
-value=>$pswdlog,
-expires=>$cexprd,
-path=>'/',
-domain=>'',
-secure=>0);

our $loginstatus = &isLogin($userlog,$pswdlog);

sub isLogin {
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
my $username = "0004af1d29b20c565e608b40905f010f88b26004";
my $password = "a137e3c6315986e13d4ba3961f4fca07476e4b71";
my $status = 0;
if ($username eq sha1_hex($_[0]) && $password eq sha1_hex($_[1])) {
$status = 1;
}
return $status;
}

print header({type => "text/html", -cookie=>[$cuserlog,$cpswdlog]});

if ($loginstatus == 1) {
print "You are login now";
}
else {
print "You are logout now";
}

Monday, February 12, 2007

MySQL Query Use DBI Module

#!/usr/bin/perl -w

use DBI;
use strict;

my $database = "jolie_fans_db";
my $user = "angelina_jolie";
my $password = "anGelina_Jolie";


my $dbh = DBI->connect( "DBI:mysql:$database", "$user", "$password");

#insert query

my $q = "INSERT INTO mytable ";
$q .= " (id, name) ";
$q .= " values (?,?) ";
my @insertbind = (1,"angelina jolie");
my $sth = $dbh->prepare($q);
$sth->execute(@insertbind);

#select query

$q = "SELECT name FROM mytable WHERE id = ? ";

my @selectbind = (4);
my $status;

$sth = $dbh->prepare($q);
$sth->execute(@selectbind);
if (my $row = $sth->fetchrow_array()) {
$status = 1;
}
else {
$status = 0;
}

Thursday, February 8, 2007

Javascript Local Date Function

function local_date () {

startoffset = 3;
duration = 7;
servertz = -5;

theday = new Date ();
theday2 = new Date ();

theyearfirst = document.form1.gciy.options[0].value;
thetz = theday.getTimezoneOffset ();

epoch = theday.getTime () + (24 * 3600 * startoffset + 60 * thetz + 3600 * servertz) * 1000;

theday.setTime (epoch);
thedate = theday.getDate ();
themonth = theday.getMonth () + 1;
theyear = theday.getFullYear ();

theyearoffset = theyear - theyearfirst;

document.form1.gcid.options[thedate].selected = true;
document.form1.gcim.options[themonth].selected = true;
document.form1.gciy.options[theyearoffset].selected = true;

epoch2 = epoch + 24 * 3600 * duration * 1000;

theday2.setTime (epoch2);
thedate2 = theday2.getDate ();
themonth2 = theday2.getMonth () + 1;
theyear2 = theday2.getFullYear ();

theyearoffset2 = theyear2 - theyearfirst;

document.form1.gcod.options[thedate2].selected = true;
document.form1.gcom.options[themonth2].selected = true;
document.form1.gcoy.options[theyearoffset2].selected = true;

}

Copy and paste code above into text editor, save it with name local_date.js.
Do the same thing with code below but name it with local_date.html and put at the same directory with local_date.js.

<html>
<head>
<title>Javascript Local Date Function</title>
<script language="javascript" src="local_date.js"></script>
</head>
<body>
<form name="form1">
<table border=0 cellpadding=0 cellspacing=0 cols=3>
<tr>
<td>Check in three days after now</td>
<td width="5px">:</td>
<td>
<select name=gcid id=gcid>
<option>------ </option>
<option value=1>1 </option>
<option value=2>2 </option>
<option value=3>3 </option>
<option value=4>4 </option>
<option value=5>5 </option>
<option value=6>6 </option>
<option value=7>7 </option>
<option value=8>8 </option>
<option value=9>9 </option>
<option value=10>10 </option>
<option value=11>11 </option>
<option value=12>12 </option>
<option value=13>13 </option>
<option value=14>14 </option>
<option value=15>15 </option>
<option value=16>16 </option>
<option value=17>17 </option>
<option value=18>18 </option>
<option value=19>19 </option>
<option value=20>20 </option>
<option value=21>21 </option>
<option value=22>22 </option>
<option value=23>23 </option>
<option value=24>24 </option>
<option value=25>25 </option>
<option value=26>26 </option>
<option value=27>27 </option>
<option value=28>28 </option>
<option value=29>29 </option>
<option value=30>30 </option>
<option value=31>31 </option>
</select>
-
<select name=gcim id=gcim>
<option>------ </option>
<option value=1>January </option>
<option value=2>February </option>
<option value=3>March </option>
<option value=4>April </option>
<option value=5>May </option>
<option value=6>June </option>
<option value=7>July </option>
<option value=8>August </option>
<option value=9>September </option>
<option value=10>October </option>
<option value=11>November </option>
<option value=12>December </option>
</select>
-
<select name="gciy" id="gciy">
<script type="text/javascript">
var date = new Date();
var year = date.getFullYear();
var option = "";
for (i=year; i<=year+4; i++) {
option += "<option value=\""+i+"\">"+i+"</option>";
}
document.write(option);
</script>
</select>
</td>
</tr>
<tr>
<td>Check out seven days after check in</td>
<td width="5px">:</td>
<td>
<select name=gcod id=gcod>
<option>------ </option>
<option value=1>1 </option>
<option value=2>2 </option>
<option value=3>3 </option>
<option value=4>4 </option>
<option value=5>5 </option>
<option value=6>6 </option>
<option value=7>7 </option>
<option value=8>8 </option>
<option value=9>9 </option>
<option value=10>10 </option>
<option value=11>11 </option>
<option value=12>12 </option>
<option value=13>13 </option>
<option value=14>14 </option>
<option value=15>15 </option>
<option value=16>16 </option>
<option value=17>17 </option>
<option value=18>18 </option>
<option value=19>19 </option>
<option value=20>20 </option>
<option value=21>21 </option>
<option value=22>22 </option>
<option value=23>23 </option>
<option value=24>24 </option>
<option value=25>25 </option>
<option value=26>26 </option>
<option value=27>27 </option>
<option value=28>28 </option>
<option value=29>29 </option>
<option value=30>30 </option>
<option value=31>31 </option>
</select>
-
<select name=gcom id=gcom>
<option>------ </option>
<option value=1>January </option>
<option value=2>February </option>
<option value=3>March </option>
<option value=4>April </option>
<option value=5>May </option>
<option value=6>June </option>
<option value=7>July </option>
<option value=8>August </option>
<option value=9>September </option>
<option value=10>October </option>
<option value=11>November </option>
<option value=12>December </option>
</select>
-
<select name="gcoy" id="gcoy">
<script type="text/javascript">
var date = new Date();
var year = date.getFullYear();
var option = "";
for (i=year; i<=year+4; i++) {
option += "<option value=\""+i+"\">"+i+"</option>";
}
document.write(option);
</script>
</select>
</td>
</tr>
</table>
</form>
<script>local_date();</script>
<p>
<center>
© <script type="text/javascript">document.write(new Date().getFullYear())</script> <a href="http://muntenari.com">muntenari.com</a>
</center>
</body>
</html>

The code above will look like form in my website muntenari.com

Happy Coding,
muntenari.com

Tuesday, February 6, 2007

Print Date with Offset

#!/usr/bin/perl

use Date::Calc qw/Today Add_Delta_Days/;

$offset = -3;
@logdate = Add_Delta_Days(Today(), $offset);

$logdate[1] = sprintf("%02d", $logdate[1]);
$logdate[2] = sprintf("%02d", $logdate[2]);

print join("-",@logdate)."\n";

Code above works out on linux shell command.
To run in browser need to add header after use declaration: print "Content-type: text/html\n\n";