views:

350

answers:

1

How do I create a new registry value using PHP?

The following code doesn't work:

function registry_write($folder, $key, $value, $type="REG_SZ")
{
  $WshShell = new COM("WScript.Shell");

  $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
  //$result = $WshShell->RegRead($registry);
  $result = $WshShell->RegWrite($registry, $value, $type);

  return($result);
}

$folder = "7-ZIP";
$key = "Mac";
$value = "123";

registry_write($folder,$key,$value);

There already is a key named 7-ZIP inside HKEY_LOCAL_MACHINE\SOFTWARE.

There is no entry in error.log inside apache/logs.

To : Tomalak How do I modify those permissions/identities? Is there a way to do it with php?

What am I trying to do : I have an Adobe AIR application which I am bundling with MySql and PHP. When this application is installed on a machine I want to put some information about the machine in the registry, so that every time the application runs I can verify if it has not been pirated.

+1  A: 

Unless you made modifications to the default permissions/identities, your web server process (the one PHP runs in) does not have write permission to HKEY_LOCAL_MACHINE.

Other than that, you got your registry terminology a bit mixed up.

The registry consists of "keys" and "values". Each value has a name (and a type), each key can contain multiple values and keys. There are no folders.

In that light, I would suggest refactoring your function like this:

function registry_write($key, $name, $value, $type="REG_SZ") {
  $WshShell = new COM("WScript.Shell");

  $path = "HKLM\\SOFTWARE\\$key\\$name";

  try {
    $WshShell->RegWrite($path, $value, $type);
    return true;
  }
  catch (Exception $e) {
    // echo or log the error
  }

  return false;
}
Tomalak
I think you could even do the RegRead in the try block, and I think it will return an exception if the key doesn't exist.
Mladen Mihajlovic
Since the RegRead was commented out, I did not include it in my code. When this function returns true, everything went well - there is no need to read the value you have just written.
Tomalak
How do I modify those permissions/identities?Is there a way to do it with php?
dta
What? Modify permissions of something you don't have permissions to? Not possible. You should ask yourself if it is even *desirable* to allow registry writes to HKEY_LOCAL_MACHINE from withing PHP. It is denied for a reason, after all.
Tomalak
Some info on what you are actually trying to do might help.
Tomalak
I have an Adobe AIR application which I am bundling with MySql and PHP. When this application is installed on a machine I want to put some information about the machine in the registry, so that every time theapplication runs I can verify if it has not been pirated.
dta
And how does that relate to PHP? (Sorry if I don't get the connection, I've never used Adobe AIR.)
Tomalak