PHP in Browser Write , Read and Update . txt , Locally

Asked

Viewed 446 times

0

In Client Browser ! Locally...

How can I create and save to a file . txt local on the client machine the value of two variables String .

This script is in the login and whenever the login is done this file is updated "overwritten" locally in the client’s machine passing updated values of these variables.

And then at another point in the script, read what’s in that file. txt "the value of the two variables previously entered and updated" and compare each of these values with two other variables in my script.

How can I accomplish this process ?

.TXT

$strGuarda01
$strGuarda02

$strCompara01 == $strGuarda01
$strCompara02 == $strGuarda02
  • 1

    You can’t. Your statement "my PHP runs on the client’s browser" is wrong. PHP is a language server-side and runs only on the server. What comes to the client is nothing more than the HTTP response created by PHP in conjunction with the web server. That is, you won’t be able to handle client-side files with PHP and it doesn’t even make much sense. What exactly do you want to do?

  • can be using another type of method I thought this way, but I know that it runs on the server but I need to store and compare values locally on the client’s machine

  • And why does it need to be on file? What is this information? What do you want to do with it? What kind of check will it be? Store on cookies would not be enough?

  • ------> * 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 Text Data / I believe that in the Windows Registry it is safer because it is not lost and is well saved / in case It may be a Txt yes

  • @Anderson Carlos Woss It gave to understand ?

  • you would have to create a client application (that will make changes to the file/folder ) that communicates with your web application. I see as the best way to solve your problem

  • @teliz Maybe ..... " socket" ordering your X service in X time, Another solution will be to use cookies or the browser’s own Localstorage in javascript //// Your idea is good but neither the ones I mentioned nor the one you mentioned can do and/ or create !

  • But as far as I know you can’t change a file using script in the browser. Eletron link can make this desktop application quickly, after it creates the API for it to communicate with your server. (sorry if it is being redundant)

  • @teliz OK, however how to do ,,,,, I do not know ever did anything of the kind or the like ! can help me create and put to work ?

  • I’m a little jogged, but I can help you :)

  • @teliz I Appreciate, how can we interact in this help ? right here ? or in chat ? or otherwise , Skype etc....

  • Can’t store this information with Javascript? with localStorage?

  • @user | It can be any way as long as it works and help me create and by to work ! Grateful.

  • You use ajax in your login or direct in php?

  • @usuario | Direct in php !

  • @teliz like this your photo, or how to identify you in skype appeared a lot .....

Show 13 more comments

2 answers

4

I believe that is not possible , because your PHP code is on a server outside the directory where you want to make file modifications, so the browser cannot make the change.

1


I’ll try to explain half on top. I hope you understand:

To Save the Data:

<script>
    localStorage.setItem('strGuarda01', '<?php echo $strGuarda01;?>');  
    localStorage.setItem('strGuarda02', '$strGuarda02');
</script>

In order to recover this data you can send it to php in the following way:

<script>
    var strGuarda01 = localStorage.getItem('strGuarda01'), strGuarda02= localStorage.getItem('strGuarda02');
    $.POST('login.php', {'strGuarda01':strGuarda01,'strGuarda02':strGuarda02}, function(data){
      ...
    }
</script>

And recovers on page . php:

<?php
    $strGuarda01 = $_POST['strGuarda01'];
    $strGuarda01= $_POST['strGuarda02'];
    ...
?>

And to update:

<script>
    localStorage.clear();
    localStorage.setItem('strGuarda01', '<?php echo $strGuarda01;?>');  
    localStorage.setItem('strGuarda02', '$strGuarda02');
</script>

localStorage

localStorage saves data on the visitor’s computer, which is linked to (and only accessible by) their domain.

This tutorial may help you: https://zenorocha.com/html5-local-storage/

  • | If I understood localStorage is an absolute path and is not alterable , that is not something I say --- ex. my variables will be saved in the path c:/tal/tal/file.txt né ? | these variables data get lost when closing the browser ? or to the client user do a disk wipe or clear the history ?

  • localStorage does not lose the user to close the browser or shut down the PC. But they are deleted if the user clears the cache and application data hosted in the browser.

  • I don’t think you can save a file . txt as our friend teliz spoke in another reply. The only way you can do what you want will be by saving in localStorage.

  • | So if the user cleans up the history, if they lose everything ! that’s problem.

  • Will the code be on a server? or on the user’s machine locally?

  • There can be both cases, where I install a local server on the client machine to run locally , and another case where the user has a web server and asks me to host there on his server !

  • If the code is on a local server on the client machine you can save a txt file inside that server. now if you are on a sessionStorage-only web server.

  • Why a line like this; localStorage.setItem('strGuarda01', '<?php echo $strGuarda01;?>'); and so on? localStorage.setItem('strGuarda02', '$strGuarda02'); ?

  • | I prefer to have a Cod. script that works in both situations ! safer.

  • two codes each saving a variable the ones you mentioned in your reply. Or you can save the two variables in only one if you prefer and yes you insert in the body of the file

  • localStorage.setItem('$value to be saved', '$my variable with the value');

  • , E... For these scripts to be executed automatically just insert them in the way you mentioned right in the body of the script ?

  • Yes. That’s right.

  • the understanding of the properties is like this ? localStorage.setItem('$value to be saved', '$my variable with the value');

  • localStorage.setItem('one-name-any', 'variable value'); EX: localStorage.setItem('email', '<?php echo $email; ?>'); => localStorage.setItem('email', '[email protected]');

  • I understood , just not understood by two lines one to indicate and another to instantiate ?

  • Take a look at this link: http://blog.thiagobelem.net/storing-information-no-computers-visitant-com-localstorage-e-sessionstorage

  • OK , I got it ! Thank you...... I think I can move on here . Any doubt call here ! Thank you.

Show 13 more comments

Browser other questions tagged

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