homepage Welcome to WebmasterWorld Guest from 54.205.193.39
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PERL to PHP
How to convert this from PERL to PHP
branmh




msg:1294335
 5:55 pm on Aug 20, 2003 (gmt 0)

I would like to convert this from PERL to PHP.... It is a small statistics script that save to a simple text file.


#
# First Lets Define the DATE

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon++; $year+=1900;

if ($sec < 10) {
$sec = "0$sec";
}
if ($min < 10) {
$min = "0$min";
}
if ($hour < 10) {
$hour = "0$hour";
}
if ($mon < 10) {
$mon = "0$mon";
}
if ($mday < 10) {
$mday = "0$mday";
}
if ($wday < 7) {
$week = $mday - $wday;
}
if ($week < 10) {
$week = "0$week";
}

$month = ($mon + 0);
@months = ("January","February","March","April","May","June","July","August","September","October","November","December");
$date = "$year-$mon-$mday";
$time = "$hour\:$min\:$sec";

########## Set Variables ############
# Define Base Location
$base = "/path/to/statistics/data/$mon";

# 0 if not used as a SSI - 1 if used as a SSI
$SSI = 0;

# change the directory path, silly!
$logfile = "$base/$ENV{'DOCUMENT_NAME'}.$year$mon$mday";

########## So much for that.. On with the show! #######

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# Stop people from using subshells to execute commands
# Not a big deal when using sendmail, but very important
# when using UCB mail (aka mailx).
$value =~ s/~!/ ~!/g;

# Uncomment for debugging purpose
# print "Setting $name to $value<P>";
$FORM{$name} = $value;
}

if ($SSI == 1) {
&log;
exit;
}

if ($SSI == 0) {
&log;
&redir;
exit;
}

sub log {

if (! open(LOG,">>$logfile")) {
print "Content-type: text/html\n\n";
print "Couldn't open $logfile so I'm bugging out..\n";
exit;
}
print LOG "$date¦$time¦$ENV{'REMOTE_ADDR'}¦$ENV{'REMOTE_HOST'}¦$ENV{'HTTP_USER_AGENT'}¦$ENV{'HTTP_REFERER'}¦$ENV{'DOCUMENT_NAME'}?$ENV{'QUERY_STRING'}\n";
close (LOG);
}

sub redir {
print "Location: $ENV{'QUERY_STRING'}\n\n";
}


 

mavherick




msg:1294336
 4:30 am on Aug 21, 2003 (gmt 0)

What are you having problem with? Any specific section of the code? I don't want to sound rude but we don't do homework here, try to be more specific, like I'm having trouble with so and so.

mavherick

branmh




msg:1294337
 12:49 pm on Aug 21, 2003 (gmt 0)

Where to start converting it at?

mavherick




msg:1294338
 3:36 pm on Aug 21, 2003 (gmt 0)

From the first line!

I would use the comments, try to understand the purpose of a specific line of codes, then convert it to its equivalent in php.

You can always cut'n paste the whole thing in a php file and try to debug your way down.

So for your first line, this page on dates [ca.php.net] from the manual should give you a good idea to start.

HTH

mavherick

branmh




msg:1294339
 5:27 pm on Aug 21, 2003 (gmt 0)

Okay, How to do to sub log, it needs to be able to write the file it not there..


sub log {

if (! open(LOG,">>$logfile")) {
print "Content-type: text/html\n\n";
print "Couldn't open $logfile so I'm bugging out..\n";
exit;
}
print LOG "$date¦$time¦$ENV{'REMOTE_ADDR'}¦$ENV{'REMOTE_HOST'}¦$ENV{'HTTP_USER_AGENT'}¦$ENV{'HTTP_REFERER'}¦$ENV{'DOCUMENT_NAME'}?$ENV{'QUERY_STRING'}\n";
close (LOG);
}

For the print LOG line I have this

$s = $date . '¦' . getenv('REMOTE_ADDR') . '¦' . getenv('REMOTE_HOST') . '¦' . getenv('HTTP_USER_AGENT') . '¦' . getenv('HTTP_REFERER') . '¦' . getenv('SCRIPT_NAME') . '?' . getenv('QUERY_STRING') . "\n";

Is this correct:


if ($SSI == 1) {
log;
exit;
}

if ($SSI == 0) {
log;
redir;
exit;
}

Function redir {
header("Location: getenv('QUERY_STRING')");
}


Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved