Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Registry Key Handling Through PHP

4.56/5 (8 votes)
10 Jul 2012CPOL1 min read 39.4K  
This article is basically helpfull in handling Registry Key through PHP code.

Introduction

The registry is made up of "Keys". Each key is similiar to branch of a tree. Each key has one parent key, and zero or more child keys. Each key can contain zero or more "Values", each of which contains a single piece of data.

To make navigating the registry a bit easier, you can think of the registry's construction like your hard drives.

  • Hard drive <-> Registry
  • Folders <-> Keys
  • Files <-> Values

Modifying the registry can potentially make Windows unbootable. Be careful! Always make a backup of the registry before making changes.

To edit a registry value, first navigate the tree until the value is displayed. Usually, you will know where you are going.

This article explain you basically how to read, write and delete the entry from registry with the help of PHP code. Please don't modify any Windows specific Registry entry, that might cause windows unbootable.

Using the code

This is an ready to use code. You just need to copy this code into a PHP file.

PHP
//php Code starts from here......

//Reading the Registry

$Wshshell= new COM('WScript.Shell');
$data= $Wshshell->regRead('HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip\path');
echo "Data is::".$data;

//Function for writing to the registry.

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

  $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
  try{
    $result = $WshShell->RegWrite($registry, $value, $type);
    echo "Entry is Successfully written at:".$registry; 
    return($result);
  }
  catch(Exception $e){
    echo "Some Exception in Registry writing".$e;
  }

  return false;
}

// Function to deleting from the Register Entry.


function registry_delete($folder, $key, $value, $type="REG_SZ")
{
    $WshShell = new COM("Wscript.shell");
    $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
    try{
    $result = $WshShell->RegDelete($registry);
    echo $key." is Successfully deleted from HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder ; 
    return($result);
    }
    catch(Exception $e){
        echo "Some Exception with the code::".$e;
    }
    return false;
    
}

//Here 7-ZIP is taken as a example.
 
$folder = "7-ZIP";
$key = "Key_name";
$value = "Key_Value";

registry_write($folder,$key,$value);
//registry_delete($folder,$key,$value);
?>

...

Reading the Registry entry

While reading the Registry entry,we simply need to initialize the com object as shown below. with the help of this new com object, simply call the regread() function. It will return the value i.e. stored in the place for whom you are searched for.

PHP
$Wshshell= new COM('WScript.Shell');
$data= $Wshshell->regRead('HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip\path');
echo "Data is ::".$data;

Writing to the Registry

The registry_write() function will write an entry in to the registry. For this you need to give "key" name and "value" corresponding to the key.

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

  $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
  try{
    $result = $WshShell->RegWrite($registry, $value, $type);
    echo "Entry is Successfully written at:".$registry; 
    return($result);
  }
  catch(Exception $e){
    echo "Some Exception in Registry writing".$e;
  }

  return false;
}

Delete The Registry Entry

The registry_delete() function will delete the entry from the Registry in accordance with the given "key" and "value" which you want to delete.

PHP
function registry_delete($folder, $key, $value, $type="REG_SZ")
{
    $WshShell = new COM("Wscript.shell");
    $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
    try{
    $result = $WshShell->RegDelete($registry);
    echo $key." is Successfully deleted from HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder ; 
    return($result);
    }
    catch(Exception $e){
        echo "Some Exception with the code::".$e;
    }
    return false;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)