Click here to Skip to main content
Click here to Skip to main content

Tagged as

Registry Key Handling Through PHP

, 10 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 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.

$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;
}

License

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

Share

About the Author

amitjha1611
C-DAC Hyderabad
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionBut Why? Pinmemberpandu web dev12-Aug-14 2:28 
Questioncannot open the regisry PinmemberMember 105671662-Feb-14 17:52 
Questionthank you Pinmembervico917-Feb-13 18:14 
QuestionProblem on Windows 7 64-bit PinmemberS.M. Zamshed Farhan15-Jan-13 19:09 
AnswerRe: Problem on Windows 7 64-bit Pinmemberamitjha161117-Jan-13 16:21 
GeneralRe: Problem on Windows 7 64-bit PinmemberS.M. Zamshed Farhan20-Jan-13 15:31 
No doubt, you are 100% right. Let me try to check now whether I have mistaken.
GeneralMy vote of 5 PinmemberManish080327-Dec-12 0:05 
GeneralMy vote of 5 Pinmemberkriti12212-Aug-12 8:29 
QuestionMy Vote of 5 PinmemberSZenah11-Jul-12 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 10 Jul 2012
Article Copyright 2012 by amitjha1611
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid