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;
}