Convert a Perl CGI script to a php script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Convert a Perl CGI script to a php script

Post by phpCCore Brad »

I have this perl script that is for a game. It's rather old and I want to convert it to PHP. It is a CGI script that sets a cookie and boots a java applet.

Code: Select all

#!/usr/bin/perl

use Digest::MD5;

$theFile = "/home/bkelly/conf/cookie";

# Set the nvpairs array to the cookie information
@nvpairs=split(/; /, $ENV{HTTP_COOKIE});

# Break the array into a hash
foreach $pair (@nvpairs) {
        ($name, $value) = split(/=/, $pair);
        $cookie{$name} = $value;
}

print "Content-type: text/html", "\n";

# If either the cookies are gone, make a new set
if (!defined($cookie{'num'}) or !defined($cookie{'hash'})) {

    if (open theData, $theFile and $count = <theData>) {
	++$count;
	$cookie{'num'} = $count;
    }
    else {
        $count = 1;
	$cookie{'num'} = $count;
    }

    system("echo $count > $theFile\n");

# Calculate an MD5 hash with this machine number
    $ctx = Digest::MD5->new;
    $ctx->add("SecretWord");
    $ctx->add($count);
    $hash = $ctx->hexdigest;

    local(@days) = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
    local(@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    local($seconds,$min,$hour,$mday,$mon,$year,$wday) = gmtime(time + 31536000);

# formatting of date variables
    $seconds = "0" . $seconds if $seconds < 10;
    $min = "0" . $min if $min < 10; 
    $hour = "0" . $hour if $hour < 10; 
    $year += 1900; 

    $expires =
	    "$days[$wday], $mday-$months[$mon]-$year $hour:$min:$seconds GMT";

    print "Set-Cookie: num=$count; path=/; expires=$expires\n";
    print "Set-Cookie: hash=$hash; path=/; expires=$expires\n";
}

print "\n";

print "<HTML><HEAD><TITLE>Phantasia</TITLE></HEAD>";
print "<BODY BACKGROUND=\"/graphics/mis6b.jpg\" TEXT=\"#BBBB66\" LINK=\"#996699\" ALINK=\"#BB3333\" VLINK=\"#BB3333\">";
print "<TABLE WIDTH=\"100%\" CELLPADDING=\"0\"><TR><TD WIDTH=90></TD><TD>";

print "<CENTER><IMG SRC=\"/graphics/clients.jpeg\" ALT=\"\" WIDTH=\"196\" HEIGHT=\"22\"></CENTER>";
print "<P>Please choose the phantasia client you'd like to play on and click on it's name below.  Macintosh Netscape users can only use the 1.02 client.  I recommend the 1.18 client for everyone else.</P>";

print "<CENTER><TABLE WIDTH=\"100%\" CELLPADDING=\"0\"><TR ALIGN=CENTER><TD>";
print "<A HREF=\"102_client.cgi?", time, "\"><IMG SRC=\"/graphics/102_client.jpeg\" ALT=\"\" WIDTH=\"224\" HEIGHT=\"17\"></A></TD><TD>";
print "<A HREF=\"118_client.cgi?", time, "\"><IMG SRC=\"/graphics/118_client.jpeg\" ALT=\"\" WIDTH=\"224\" HEIGHT=\"17\"></A></TD></TR></TABLE><HR><BR>";

print "<P>theFlower has created a cool post calculator for Windows.  Click <A HREF=\"/posts.zip\">here</A> to download a copy. <BR>(Updated 12/16/01) </P>";
   
print "<P>A handy monster tool from KillerX for Windows can now be downloaded <A HREF=\"/monsters.zip\">here</A>. <BR>(Updated 01/06/02) </P><HR><BR>";

print "<iframe src=\"http://leader.linkexchange.com/1/X1422082/showiframe?\" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no><a href=\"http://leader.linkexchange.com/1/X1422082/clickle\" target=\"_top\"><img width=468 height=60 border=0 ismap alt=\"\" src=\"http://leader.linkexchange.com/1/X1422082/showle?\"></a></iframe><br><a href=\"http://leader.linkexchange.com/1/X1422082/clicklogo\" target=\"_top\"><img src=\"http://leader.linkexchange.com/1/X1422082/showlogo?\" width=468 height=16 border=0 ismap alt=\"\"></a><BR><HR>";

print "<P><A HREF=\"/index.html\">Home</A> | The Game | <A HREF=\"/intro.html\">The Rules</A> | <A HREF=\"/info.html\">Game Info</A> </P>";

print "</CENTER></TD></TR></TABLE></BODY></HTML>";


As I have never really ever worked with perl I can't convert most of the commands. If anyone has an idea of how to convert this I would really appreciate it. I can convert the obvious ones (IE prints.... Actually that is about it ;) )
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

I also wrote up one with comments on what I think things do, but obvious I don't know. Maybe just some hints would help me greatly. Maybe if someone could clarify to me what something is doing I could rewrite it myself. I am just looking for some sort of help. Simply because I don't want to setup perl on my server to mess around with this. I just noticed it loads another perl script after it loads this one, but I haven't looked at the other one so for all I know it doesn't do much.

This is my commented version of the document though:

Code: Select all

#!/usr/bin/perl
#Declares it as a perl document

use Digest::MD5;
#Sets up an MD5 auto done by php?

$theFile = "/home/bkelly/conf/cookie";
#Sets what file to store the cookie count in (Ie if there are 10 users on count 10) This is a guess


# Set the nvpairs array to the cookie information
@nvpairs=split(/; /, $ENV{HTTP_COOKIE});
#I have no idea what this is


#Not a clue what this does i mean the comment says, but I don't know what nvpairs are
# Break the array into a hash
foreach $pair (@nvpairs) {
        ($name, $value) = split(/=/, $pair);
        $cookie{$name} = $value;
}


#Sets a header possibly?
print "Content-type: text/html", "\n";

#If the user's cookie is gone make a new one? That is a guess
# If either the cookies are gone, make a new set
if (!defined($cookie{'num'}) or !defined($cookie{'hash'})) {

    if (open theData, $theFile and $count = <theData>) {
   ++$count;
   $cookie{'num'} = $count;
    }
    else {
        $count = 1;
   $cookie{'num'} = $count;
    }
	#Add $count to the file listed above? Guess
    system("echo $count > $theFile\n");


#Set up an MD5 with SecretWord and $count? Not sure on that one either
# Calculate an MD5 hash with this machine number
    $ctx = Digest::MD5->new;
    $ctx->add("SecretWord");
    $ctx->add($count);
    $hash = $ctx->hexdigest;

#Set up a date of some sort but no idea
    local(@days) = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
    local(@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    local($seconds,$min,$hour,$mday,$mon,$year,$wday) = gmtime(time + 31536000);

#Set when the cookie expires? Some of these would be headers in php, but really not sure in perl
# formatting of date variables
    $seconds = "0" . $seconds if $seconds < 10;
    $min = "0" . $min if $min < 10;
    $hour = "0" . $hour if $hour < 10;
    $year += 1900;

    $expires =
       "$days[$wday], $mday-$months[$mon]-$year $hour:$min:$seconds GMT";

    print "Set-Cookie: num=$count; path=/; expires=$expires\n";
    print "Set-Cookie: hash=$hash; path=/; expires=$expires\n";
}


#Print out the HTML obvious there! I can convert that ;)
print "\n";

print "<HTML><HEAD><TITLE>Phantasia</TITLE></HEAD>";
print "<BODY BACKGROUND=\"/graphics/mis6b.jpg\" TEXT=\"#BBBB66\" LINK=\"#996699\" ALINK=\"#BB3333\" VLINK=\"#BB3333\">";
print "<TABLE WIDTH=\"100%\" CELLPADDING=\"0\"><TR><TD WIDTH=90></TD><TD>";

print "<CENTER><IMG SRC=\"/graphics/clients.jpeg\" ALT=\"\" WIDTH=\"196\" HEIGHT=\"22\"></CENTER>";
print "<P>Please choose the phantasia client you'd like to play on and click on it's name below.  Macintosh Netscape users can only use the 1.02 client.  I recommend the 1.18 client for everyone else.</P>";

print "<CENTER><TABLE WIDTH=\"100%\" CELLPADDING=\"0\"><TR ALIGN=CENTER><TD>";
print "<A HREF=\"102_client.cgi?", time, "\"><IMG SRC=\"/graphics/102_client.jpeg\" ALT=\"\" WIDTH=\"224\" HEIGHT=\"17\"></A></TD><TD>";
print "<A HREF=\"118_client.cgi?", time, "\"><IMG SRC=\"/graphics/118_client.jpeg\" ALT=\"\" WIDTH=\"224\" HEIGHT=\"17\"></A></TD></TR></TABLE><HR><BR>";

print "<P>theFlower has created a cool post calculator for Windows.  Click <A HREF=\"/posts.zip\">here</A> to download a copy. <BR>(Updated 12/16/01) </P>";
   
print "<P>A handy monster tool from KillerX for Windows can now be downloaded <A HREF=\"/monsters.zip\">here</A>. <BR>(Updated 01/06/02) </P><HR><BR>";

print "<iframe src=\"http://leader.linkexchange.com/1/X1422082/showiframe?\" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no><a href=\"http://leader.linkexchange.com/1/X1422082/clickle\" target=\"_top\"><img width=468 height=60 border=0 ismap alt=\"\" src=\"http://leader.linkexchange.com/1/X1422082/showle?\"></a></iframe><br><a href=\"http://leader.linkexchange.com/1/X1422082/clicklogo\" target=\"_top\"><img src=\"http://leader.linkexchange.com/1/X1422082/showlogo?\" width=468 height=16 border=0 ismap alt=\"\"></a><BR><HR>";

print "<P><A HREF=\"/index.html\">Home</A> | The Game | <A HREF=\"/intro.html\">The Rules</A> | <A HREF=\"/info.html\">Game Info</A> </P>";

print "</CENTER></TD></TR></TABLE></BODY></HTML>";
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

If I would start looking up perl functions.
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

I did do that... This is what I came up with. Some are just guesses some are educated guesses and some I am pretty sure I have right. I still don't have any idea what a chunk of the code does, but this is what I have converted:

Code: Select all

<?PHP


$theFile = "../conf/cookie";

/*
# Set the nvpairs array to the cookie information
@nvpairs=split(/; /, $ENV{HTTP_COOKIE});
#I have no idea what this is


#Not a clue what this does i mean the comment says, but I don't know what nvpairs are
# Break the array into a hash
foreach $pair (@nvpairs) {
        ($name, $value) = split(/=/, $pair);
        $cookie{$name} = $value;
}


#Sets a header possibly?
print "Content-type: text/html", "\n";

#If the user's cookie is gone make a new one? That is a guess
# If either the cookies are gone, make a new set
if (!defined($cookie{'num'}) or !defined($cookie{'hash'})) {

    if (open theData, $theFile and $count = <theData>) {
   ++$count;
   $cookie{'num'} = $count;
    }
    else {
        $count = 1;
   $cookie{'num'} = $count;
    }
*/

    file_put_contents($theFile,$count);
    



    $hash = md5("SecretWord" . $count);



	$time = time() + 31536000;
	$seconds = date("s",$time);
	$min     = date("i",$time);
	$hour    = date("h",$time);
	$mday    = date("d",$time);
	$mon     = date("M",$time);
	$year    = date("Y",$time);
	$wday    = date("D",$time);

	if ($second < 10) {
    	$seconds = "0";
    }
    if ($min < 10) {
    	$min = "0";
    }
    if ($hour < 10) {
    	$hour = "0";
    }

    $expires = "$day, $mday-$month-$year $hour:$min:$seconds GMT";
	
    setcookie("num",$count,$expires);
    hsetcookie("hash",$hash;$expires);
}


?>

	<HTML><HEAD><TITLE>Phantasia</TITLE></HEAD>
	<BODY BACKGROUND="/graphics/mis6b.jpg" TEXT="#BBBB66" LINK="#996699" ALINK="#BB3333" VLINK="#BB3333">
	<TABLE WIDTH="100%" CELLPADDING="0"><TR><TD WIDTH=90></TD><TD>

	<CENTER><IMG SRC="/graphics/clients.jpeg" ALT="" WIDTH="196" HEIGHT="22"></CENTER>
	<P>Please choose the phantasia client you'd like to play on and click on it's name below.  Macintosh Netscape users can only use the 1.02 client.  I recommend the 1.18 client for everyone else.</P>

	<CENTER><TABLE WIDTH="100%" CELLPADDING="0"><TR ALIGN=CENTER><TD>
	<A HREF="102_client.cgi? <?PHP echo time(); ?>"><IMG SRC="/graphics/102_client.jpeg" ALT="" WIDTH="224" HEIGHT="17"></A></TD><TD>
	<A HREF="118_client.cgi?<?PHP echo time(); ?>"><IMG SRC="/graphics/118_client.jpeg" ALT="" WIDTH="224" HEIGHT="17"></A></TD></TR></TABLE><HR><BR>

	<P>theFlower has created a cool post calculator for Windows.  Click <A HREF="/posts.zip">here</A> to download a copy. <BR>(Updated 12/16/01) </P>
   
	<P>A handy monster tool from KillerX for Windows can now be downloaded <A HREF="/monsters.zip">here</A>. <BR>(Updated 01/06/02) </P><HR><BR>

	<iframe src="http://leader.linkexchange.com/1/X1422082/showiframe?" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no><a href="http://leader.linkexchange.com/1/X1422082/clickle" target="_top"><img width=468 height=60 border=0 ismap alt="" src="http://leader.linkexchange.com/1/X1422082/showle?"></a></iframe><br><a href="http://leader.linkexchange.com/1/X1422082/clicklogo" target="_top"><img src="http://leader.linkexchange.com/1/X1422082/showlogo?" width=468 height=16 border=0 ismap alt=""></a><BR><HR>

	<P><A HREF="/index.html">Home</A> | The Game | <A HREF="/intro.html">The Rules</A> | <A HREF="/info.html">Game Info</A> </P>

	</CENTER></TD></TR></TABLE></BODY></HTML>
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

Okay this is my final version. It sets the two cookies (I ran the perl script to see.) It sets them for the right date of experation, but I have no idea if the hash is right. I also think that it writes the file right since I took it as it ups the cookies each time it runs. If anyone can see if I made mistakes somewhere in my conversion please leave me a note. Remember I have never worked with perl, so I just made educated guesses as to what the script does! Thanks :D

Code: Select all

<?PHP

if (!isset($_COOKIE['num']) or !isset($_COOKIE['hash'])) {
	$theFile = "../conf/cookie";
	
	$count = file_get_contents($theFile);
	
	if (!is_numeric($count) and $count > 0) {
		$count = 0;
	} else {
		$count ++;
	}
    file_put_contents($theFile,$count);
    



    $hash = md5("SecretWord" . $count);



	$time = time() + 31536000;
	$seconds = date("s",$time);
	$min     = date("i",$time);
	$hour    = date("h",$time);
	$mday    = date("d",$time);
	$mon     = date("M",$time);
	$year    = date("Y",$time);
	$wday    = date("D",$time);

    $expires = $time;
	
    setcookie("num",$count,$expires);
    setcookie("hash",$hash,$expires);
}


?>

	<HTML><HEAD><TITLE>Phantasia</TITLE></HEAD>
	<BODY BACKGROUND="/graphics/mis6b.jpg" TEXT="#BBBB66" LINK="#996699" ALINK="#BB3333" VLINK="#BB3333">
	<TABLE WIDTH="100%" CELLPADDING="0"><TR><TD WIDTH=90></TD><TD>

	<CENTER><IMG SRC="/graphics/clients.jpeg" ALT="" WIDTH="196" HEIGHT="22"></CENTER>
	<P>Please choose the phantasia client you'd like to play on and click on it's name below.  Macintosh Netscape users can only use the 1.02 client.  I recommend the 1.18 client for everyone else.</P>

	<CENTER><TABLE WIDTH="100%" CELLPADDING="0"><TR ALIGN=CENTER><TD>
	<A HREF="102_client.cgi? <?PHP echo time(); ?>"><IMG SRC="/graphics/102_client.jpeg" ALT="" WIDTH="224" HEIGHT="17"></A></TD><TD>
	<A HREF="118_client.cgi?<?PHP echo time(); ?>"><IMG SRC="/graphics/118_client.jpeg" ALT="" WIDTH="224" HEIGHT="17"></A></TD></TR></TABLE><HR><BR>

	<P>theFlower has created a cool post calculator for Windows.  Click <A HREF="/posts.zip">here</A> to download a copy. <BR>(Updated 12/16/01) </P>
   
	<P>A handy monster tool from KillerX for Windows can now be downloaded <A HREF="/monsters.zip">here</A>. <BR>(Updated 01/06/02) </P><HR><BR>

	<iframe src="http://leader.linkexchange.com/1/X1422082/showiframe?" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no><a href="http://leader.linkexchange.com/1/X1422082/clickle" target="_top"><img width=468 height=60 border=0 ismap alt="" src="http://leader.linkexchange.com/1/X1422082/showle?"></a></iframe><br><a href="http://leader.linkexchange.com/1/X1422082/clicklogo" target="_top"><img src="http://leader.linkexchange.com/1/X1422082/showlogo?" width=468 height=16 border=0 ismap alt=""></a><BR><HR>

	<P><A HREF="/index.html">Home</A> | The Game | <A HREF="/intro.html">The Rules</A> | <A HREF="/info.html">Game Info</A> </P>

	</CENTER></TD></TR></TABLE></BODY></HTML>
Post Reply