File . reg or . txt in windows

Asked

Viewed 129 times

1

How can I in PHP create a . reg or . txt file in a location e.g. ( HKEY_LOCAL_MACHINE ) and insert into it the value of two string variables $minhaVarReg01 and $minhaVarReg02 .

And later open this same file via PHP also and compare the values within that file with another string variables $minhaVarComparaReg01 and $minhaVarComparaReg01 .

?

1 answer

1


Unfortunately you cannot do it through PHP. Try Vbscript.

Idea:

  • Vbscript will create a task (if it does not exist) in the "Task Scheduler" that will execute the same of X in X (hours/minutes/seconds)

  • In the same file will be made a Httprequest with your service.

    • If this is the first time running this request it will return the values to be entered in the Registry or its file . txt.
    • Otherwise this request will pass the Registry or . txt data, which will be processed later in php.

I found below a very basic version:

Note: This code has not been tested.

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Function post(url, data)
    'Faz o request para o servidor com parametros POST e retorna a resposta
    Dim restReq
    Set restReq = CreateObject ("Msxml2.XMLHttp.6.0")
    restReq.open "POST", url, False
    restReq.setRequestHeader "content-type", "application/x-www-form-urlencoded"
    restReq.send(data)
    post = restReq.responseText
End Function

Function cwrite(path, filename, data)
    'Escreve um ficheiro numa determinada pasta, caso nao exista a pasta ou o ficheiro cria os dois
    Dim objFile, file
    Set objFSO=CreateObject("Scripting.FileSystemObject")

    file = path & filename
    If Not objFSO.FolderExists(path) = True Then  objFSO.CreateFolder path
    If objFSO.FileExists(file) = True Then objFSO.deletefile file, True
    Set objFile = objFSO.CreateTextFile(file,True)
    objFile.Write data
    objFile.Close
End Function

Function existsFile(file)
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    existsFile = objFSO.FileExists(file)
End Function

Function MakeTask(schedule, modifier, title, action)
    'Cria uma tarefa usando SchTasks 
    Set wShell = CreateObject("Wscript.Shell")
    wShell.Run "SchTasks /Create /SC " & schedule & " /MO " & modifier & " /TN """ & title & """ /TR """ & action & """ /F", 0
End Function

Function getRegistryValue(strKeyPath, strValueName)
    'Busca o valor de um determinado registo
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

    oReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,regValue
    Set oReg = Nothing
    If Not regValue = 0 Then
        getRegistryValue = regValue
    Else
        getRegistryValue = Nothing
    End If
End Function

'###
'INICIO DA APLICAÇÃO

Set wShell = CreateObject("Wscript.Shell")

Sys_Drive = wShell.ExpandEnvironmentStrings("%SystemDrive%")
SERVICE_URL = "http://url_do_serviço.com/serviço.php"
FILE_PATH = Sys_Drive & "\"

If Not existsFile(FILE_PATH & "ficheiro.txt") Then
    'EFECTUA O REQUEST PARA O SERVIÇO
    responseBody = post(SERVICE_URL, "data1=value1&data2=value2")
    'ESCREVE O FICHEIRO "ficheiro.txt" COM O VALOR "responseBody"
    cwrite FILE_PATH, "ficheiro.txt", responseBody
    ' CRIA A TAREFA PARA SER EXECUTADA DE 15 EM 15 MINUTOS MakeTask([HOURLY|MINUTE|SECOND], [NUMERIC], [TITULO], [AÇAO])
    MakeTask "MINUTE", 15, "A MINHA TAREFA", "caminho_para_o_ficheiro_vbs/ping.vbs"

Else
    'CASO O FICHEIRO EXISTA ....
End If

DO NOT EXECUTE THE ABOVE CODE HERE BECAUSE THIS IS VBSCRIPT

  • OK, but this file will be separated along with my other files . php, that ? what is the extension to save this file ? and how to call this procedure in PHP by passing the parameters to save and check later ?

  • I liked that idea, but I never applied, I need help to apply it here in practice.

  • how can I do ?

  • Someone knows a solution, because nothing yet, ?

  • Articuno & Gonsalo Souza / Still nothing here , I could not solve this question. It would be better to open another question ?

  • @Paulomaia First step to forgive me for not explaining this part, this file will have to be saved with extension vbs. As I noticed that you opened another question, has the problem been solved? I also saw that you are using only PHP as suggested in the reply you received, but you cannot make any changes to a file that is stored in a location on the machine Desktop for example. If you need more information I can post a full version that will help you get the requested.

  • , I’m still in the dark !

  • @Paulomaia Just a few more details before creating a stable version for the requested, 1 - Do you need to run on client machines or just on your local machine? (client case: the same will have to run the file vbs only once). 2 - What kind of data will be saved only text or binary/hexadecimal? 3 - You will have to save this information in the Registry or it can be in a file txt in a specific location?

  • , See https://answall.com/questions/226338/como-writing-e-ler-dados-registro-do-windows-com-php/226371#226371 and also https://answall.com/questions/226381/como-manipular-a-string-vinda-de-um-arquivo-exporteddo-regedit#226385

  • ------> * I have a login system that receives data from a web service, but the user access the login while not connected to the internet, it is not possible to access the webservice in question, for this reason I intend to save in an external file for ex. in the windows registry of that same machine where the login system is installed PHP, save the value of 2 variables one is the serial number of the system that releases the entry to the system through login and another is a number that can vary from 0-99 .......... continue in the Prox. comment .....

  • which is a security number that I make available to the case of the user is without internet for a while the system goes discounting from this quota made available for off accesses and go decreasing this number in the register until arriving at a point where it has no exit because they ran-if the execution quotas off and it has to log in connected for the system to update the data saved in the windows registry

  • I need the full php Routine, I don’t have the . reg it needs to be created via php routine and written/stored in it the value of 2 variables and then in another routine search it and compare the values stored in it with the local variable controllers 2 to see if they are equal values................... It will be run in the client / String data Strings / I believe that in the Windows Registry it is safer because it is not lost and is well saved / It can be a Txt yes.

  • @Paulomaia I understood the idea perfectly, however as I explained above the client will have to execute the file vbs that will create a task that will function as "socket" ordering your X service in X time, Another solution will be to use cookies or the very Localstorage of the browser in javascript. Will the client be able to execute such a file? and how do you intend to distribute it? PHP does not have any access to the local machine, because it only runs on the server, javascript is also unable to perform actions outside the browser.

  • 1
  • Got some results in Adapted Code ?

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.