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.
$Wshshell= new COM('WScript.Shell');
$data= $Wshshell->regRead('HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip\path');
echo "Data is::".$data;
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 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;
}
$folder = "7-ZIP";
$key = "Key_name";
$value = "Key_Value";
registry_write($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.
$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.
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.
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;
}