Product/License Validation

ajetrumpet

Banned
Local time
Today, 01:01
Joined
Jun 22, 2007
Messages
5,638
All,

I had a really hard time doing this, but I finally figured out something that worked for me, so I'm posting it here so others can use it too if they wish. This code is meant to prevent a customer from using an application in more than one place, on more than one computer, or copying or moving an application. Basically, I am using it to prevent unauthorized use after distribution. The attached file creates validation keys for use one time. If you press the Validation button, the product license is activated via the internet and an internet explorer object. You will also see then, that if you move the app to another computer it will recognize this and shut down to prevent unauthorized use. To do this, I needed a PHP webpage, a text file and HTML references in the VBA project. Here is the code I used:


The webpage has two textboxes on it. One for entering the validation key, and the other one for returning a boolean value to help identify authorization.

WebPage Code:
PHP:
<?php

//If the key is blank, we just got here.  go on to the HTML section
if (! $_POST["key"]) {
	//DO NOTHING
					 }
else
{

$validation = "false";

$logfile = "validation.txt";

//Read file for keys already entered
$handle = fopen($logfile, "r");

//If there is no information in the log, no need to check validation
if (filesize($logfile) < 1) {
	$validation = "false";
	fclose ($handle); }
	
else {

$log = fread($handle, filesize($logfile));
fclose ($handle);

// Seperate each logline
$log = explode("|", trim($log)); 

// Seperate each part in each logline
for ($i = 0; $i < count($log); $i++) {
	if ($log[$i] == $_POST["key"])	 {
		$validation = "true"; }
									 }
									 
	 }

if ($validation == "false") {
    
	$handle = @fopen("Validation.txt", "a");
    fputs($handle, $_POST["key"] . "|");
    fclose ($handle);
	
							}
	
}

?>

//BEGIN HTML SECTION HERE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validation Log</title>

</head>

<body>

<form action="" method="post" name="frm">
  
    <center><br />
      Enter Your Product Key:<br />
        <input type="text" id="key" name="key" /><br />
        <input type="text" id="Confirm" name="Confirm" value="<?php echo $validation; ?>" />
    </center>
  <p>
    <label>
    <center><input type="submit" id="valcheck" value="Validate" /></center>
    </label>
</p>
</form>

</body>
</html>
Here is the validation button's code:
PHP:
Private Sub ValidateButton_Click()

On Error GoTo cleanup

DoCmd.Hourglass -1
DoCmd.SetWarnings False

Dim db As Database
Set db = CurrentDb()
Dim rs As Recordset
Set rs = db.OpenRecordset("validate", dbOpenDynaset)

Dim ie As Object
Dim address As String
Dim StopTime As Variant, start As Variant

Set ie = CreateObject("internetexplorer.application")

address = "http://www.mydomain.com/logs/validation.php"

      ie.navigate address
      
         While ie.busy And ie.readystate <> 4
            DoEvents
         Wend

start = 0
StopTime = 10000000
   While start < StopTime
      start = start + 1
   Wend

      ie.Document.all("key").Value = Me.TXT1 & _
                                     Me.TXT2 & _
                                     Me.TXT3 & _
                                     Me.TXT4 & _
                                     Me.TXT5
      ie.Document.Forms(0).submit

         While ie.busy And ie.readystate <> 4
            DoEvents
         Wend

start = 0
StopTime = 10000000
   While start < StopTime
      start = start + 1
   Wend

If ie.Document.all("Confirm").Value = "true" Then
    MsgBox "This license has already been registered." & vbCr & vbCr & _
           "You may only register a license one time...", vbCritical, _
           "Validation Error"
                Application.Quit
Else
    
    Dim lpBuff As String * 1314
    GetComputerName lpBuff, Len(lpBuff)
    
    rs.AddNew
    rs!computerid = lpBuff
    rs!needvalidation = "no"
    rs.Update
        MsgBox "Product Now Validated.  Thank you!"
            DoCmd.Close

End If

cleanup:

ie.Quit
rs.Close

Set ie = Nothing
Set rs = Nothing
Set db = Nothing

DoCmd.SetWarnings True
DoCmd.Hourglass 0

End Sub
An external function I used to identify the computer:
PHP:
Declare Function GetComputerName& Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long)
Here is the key generation code that I used (not efficient, but it does the job!):
PHP:
Private Sub Command14_Click()

DoCmd.Hourglass -1

i = 1

options(1) = "A"
options(2) = "B"
options(3) = "C"
options(4) = "D"
options(5) = "E"
options(6) = "F"
options(7) = "G"
options(8) = "H"
options(9) = "I"
options(10) = "J"
options(11) = "K"
options(12) = "L"
options(13) = "M"
options(14) = "N"
options(15) = "O"
options(16) = "P"
options(17) = "Q"
options(18) = "R"
options(19) = "S"
options(20) = "T"
options(21) = "U"
options(22) = "V"
options(23) = "W"
options(24) = "X"
options(25) = "Y"
options(26) = "Z"
options(27) = "1"
options(28) = "2"
options(29) = "3"
options(30) = "4"
options(31) = "5"
options(32) = "6"
options(33) = "7"
options(34) = "8"
options(35) = "9"
options(36) = "0"

'CLEAR THE PREVIOUS VALUES
block1 = ""
block2 = ""
block3 = ""
block4 = ""
block5 = ""

    'COMPRISE THE FIRST BLOCK HERE
    While i < 5
        Randomize
        block1 = block1 & options(Int((36 - 1 + 1) * Rnd + 1))
            i = i + 1
    Wend

    'COMPRISE THE SECOND BLOCK HERE
    While i < 9
        Randomize
        block2 = block2 & options(Int((36 - 1 + 1) * Rnd + 1))
            i = i + 1
    Wend

    'COMPRISE THE THIRD BLOCK HERE
    While i < 13
        Randomize
        block3 = block3 & options(Int((36 - 1 + 1) * Rnd + 1))
            i = i + 1
    Wend

    'COMPRISE THE FOURTH BLOCK HERE
    While i < 17
        Randomize
        block4 = block4 & options(Int((36 - 1 + 1) * Rnd + 1))
            i = i + 1
    Wend

    'COMPRISE THE FIFTH BLOCK HERE
    While i < 21
        Randomize
        block5 = block5 & options(Int((36 - 1 + 1) * Rnd + 1))
            i = i + 1
    Wend

'CLEAR THE CURRENT KEY
Me.TXT1.ControlSource = ""
Me.TXT2.ControlSource = ""
Me.TXT3.ControlSource = ""
Me.TXT4.ControlSource = ""
Me.TXT5.ControlSource = ""

'MAKE A NEW KEY HERE
Me.TXT1.ControlSource = "=" & """" & block1 & """"
Me.TXT2.ControlSource = "=" & """" & block2 & """"
Me.TXT3.ControlSource = "=" & """" & block3 & """"
Me.TXT4.ControlSource = "=" & """" & block4 & """"
Me.TXT5.ControlSource = "=" & """" & block5 & """"

key = Me.TXT1 & Me.TXT2 & Me.TXT3 & Me.TXT4 & Me.TXT5


DoCmd.Hourglass 0

End Sub
I've attached an MDE as an example of how it works. The key generator is just there for show, so if you use this to validate your own applications, you will of course need to use your own key generation technique to verify uniqueness.
 

Attachments

Users who are viewing this thread

Back
Top Bottom