Page 1 of 1

Run console script with PHP

Posted: Tue Sep 07, 2010 4:45 pm
by bujaman
OK, I am a newbie to PHP. I have been working on a website using WordPress and need to run a small script to generate registration codes for a website. The customer enters a number generated by the downloaded software onto this webpage:

http://www.blindbidpro.com/register/
here is an example of a computer code: BC79-2AAE-9628-8892

and a registration number is supposed to be generated. The problem is the provider of the software has an ASP solution, but my server doesn't support ASP pages. I am trying to change the ASP code into PHP, but I have no experience in either of these languages. Any help would be great! Here is a link to the software providers explanation of what needs to happen:

http://www.LockXLS.com/help/index.asp?topic=ht_keygen

and here is the ASP code I already have:

Code: Select all

<%@ Language="JavaScript" %>

<html>
	<head>
	<style type="text/css">
		.myText {font-family:Verdana; font-size:10px;}
	</style>
	</head>
<body>

<%

var sComputerCode = String(Request.Form("sCC"));
var sActivationCode = "";
 
var commandLine = "http://www.blindbidpro.com/validate/KeyGen.exe 80311927548D0E53 " + sComputerCode;
 
var WshShell = Server.CreateObject("WScript.Shell");

var oExec = WshShell.Exec(commandLine);

while( !oExec.StdOut.AtEndOfStream )
{ 
	sActivationCode += oExec.StdOut.Read(1);
}

WshShell = null;

%>

<div class="myText" style="width:100%;" align="center">

Thank you for generating Activation Code.

<div style="height:30px;"></div>

<table class="myText" style="border:solid 1px gray;">
<tr>
<td>
Computer Code </td><td><b> <% Response.Write( sComputerCode ); %></b></td>
</tr>
<tr>
<td>
Activation Code </td><td><b> <% Response.Write( sActivationCode ); %></b></td>
</tr>
</table>
</div>

</body>
</html>
THANKS SO MUCH!!

Re: Run console script with PHP

Posted: Tue Sep 07, 2010 6:06 pm
by mecha_godzilla
Hi,

You need to look at using shell_exec() in your PHP script - this page shows all the different types of exec-like commands:

http://uk.php.net/manual/en/book.exec.php

Make sure you read the warnings on various pages about escaping any arguments before they're sent to the shell, otherwise you might end up finding half the server has been deleted!

Apologies if this next point is obvious, but this will only work if your server is a Windows platform because the executable KeyGen.exe will have been compiled to run on Windows machines. Assuming you are running Windows though, there shouldn't be any problem converting the ASP code to PHP - I think the PHP code would actually be quicker to write and simpler to understand.

If you need any further help please say so.

HTH,

Mecha Godzilla

Re: Run console script with PHP

Posted: Tue Sep 07, 2010 10:41 pm
by bujaman
Thanks for the quick response. I looked through the documentation you sent me and have it one step closer. The script is executing (no error), but I am having trouble with getting it to display the activation code generated. Here is what I have so far:

Code: Select all

<?@ $Language="JavaScript" ?>

<html>
	<head>
	<style type="text/css">
		.myText {font-family:Verdana; font-size:10px;}
	</style>
	</head>
<body>

<?
$ActivationCode = shell_exec("/.keygen 80311927548D0E53 sComputerCode");
?>

<div class="myText" style="width:100%;" align="center">

Thank you for using Blind Bid Pro.

<div style="height:30px;"></div>

<table class="myText" style="border:solid 1px gray;">
<tr>
<td>
Computer Code </td><td><b> <? echo $sComputerCode; ?></b></td>
</tr>
<tr>
<td>
Activation Code </td><td><b> <? echo $ActivationCode; ?></b></td>
</tr>
</table>
</div>

</body>
</html>
The original ASP code had this:

Code: Select all

var WshShell = Server.CreateObject("WScript.Shell");

var oExec = WshShell.Exec(commandLine);

while( !oExec.StdOut.AtEndOfStream )
{ 
	sActivationCode += oExec.StdOut.Read(1);
}
which I believe took the resulting code from the script, set it equal to "sActivationCode" and then the HTML code later displayed it. How can I accomplish this with PHP? Thanks again for the help, I really appreciate it.

Re: Run console script with PHP

Posted: Wed Sep 08, 2010 12:57 am
by requinix

Code: Select all

<?@ $Language="JavaScript" ?>
Get rid of that.

Code: Select all

/.keygen 80311927548D0E53 sComputerCode
Is that supposed to be "./keygen" instead? Where is this PHP script? The "keygen" program? Windows or Linux?

Also:
1. Use <?php instead of <?.
2. Use

Code: Select all

 tags (or the PHP Code button) instead of [code].

Re: Run console script with PHP

Posted: Wed Sep 08, 2010 2:22 am
by bujaman
OK, I fixed all that. "keygen" is a linux program provided to me by the company that wrote the software I am using. It takes a couple of variables, the long series of numbers is the unique product identifier for the software I will write, and sComputerCode is a code generated for their specific computer. They enter the computer code into the textbox on blindbidpro.com/register. All this gets inputted into keygen, then keygen is supposed to spit out a serial number for them to use in registering the product. Below is what I have already, but still no luck having it display the serial number. Thanks again for the help, I really appreciate it!

Code: Select all

<html>
	<head>
	<style type="text/css">
		.myText {font-family:Verdana; font-size:10px;}
	</style>
	</head>
<body>

<?php
$ActivationCode = shell_exec("./keygen 80311927548D0E53 sComputerCode");
echo $ActivationCode;
?>


<div class="myText" style="width:100%;" align="center">

Thank you for using Blind Bid Pro.

<div style="height:30px;"></div>

<table class="myText" style="border:solid 1px gray;">
<tr>
<td>
Computer Code </td><td><b><?php echo "$ActivationCode";?></b></td>
</tr>
<tr>
<td>
Activation Code </td><td><b> <?php echo $ActivationCode; ?></b></td>
</tr>
</table>
</div>

</body>
</html>

Re: Run console script with PHP

Posted: Wed Sep 08, 2010 3:04 pm
by mecha_godzilla
For the purposes of testing, I suggest you use the system() function and see what happens; more details here:

http://uk.php.net/manual/en/function.system.php

Remember that on UN*X-like systems in general, you don't always get a proper output when a command has been run, so you might need to consider this as a factor as well (IE do you need to pipe the output to a file, etc.)

Also, I can't see in your code where you're capturing the 'computer code' - you've got this line here:

Code: Select all

$ActivationCode = shell_exec("./keygen 80311927548D0E53 sComputerCode");
but sComputerCode will be NULL because it hasn't been defined or contains any values, so you need to capture this value (either from the URL using $_GET or as a $_POST value.) It's possible that the keygen app isn't working properly because you haven't given it the second argument - though I would have expected some kind of error to happen, but then we are talking about Linux here :mrgreen:

HTH,

M_G

Re: Run console script with PHP

Posted: Wed Sep 08, 2010 4:38 pm
by AbraCadaver
Try this out (not tested obviously) and I have no idea where keygen is:

Code: Select all

$sComputerCode = $_REQUEST['sCC'];
$sActivationCode = "";

// the path and the filename are case sensitive and keygen must be marked as executable on linux:  chmod +x keygen 
$commandLine = "/whatever/path/to/keygen 80311927548D0E53 $sComputerCode"; 
$sActivationCode = shell_exec($commandLine);

echo $sActivationCode;