Forum Moderators: coopster

Message Too Old, No Replies

Advice on tackling this please.

Not sure on the best approach for this

         

Matthew1980

6:25 pm on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there people of WebmasterWorld,

In a config file I have this sort of setup:-

$users['0']['UserPass'] = "phpdotnet";


and this is part of a multi dimensional array, my question is, how would I open this file, find this line (or similar depending on what index/UserPass was being supplied from the cookie I have set..) and then alter/change the value that is being assigned to it, before saving that data back to the file so that if I opened it, I could only see the value changing?

I guess as you could do a combination of preg_match()/Str_replce() with the entire line, but dynamically alter the data between the quotes, but in truth, I am drawing a blank on this, and to be perfectly honest I don't want to open this file manually to do this.

I am sure as this can be done, just not sure how...

Thoughts please!

Cheers,
MRb

enigma1

7:24 pm on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the file isn't huge and it is trusted PHP code, then create a function and do a require for the file in it. It will be executed and the $users will become a local var in the function's scope, which you can then change it and write it back.

Now if its a huge one you may have to read part of the file (see fgets and fseek). Once you extract the line and edit it you need to create another file and write to it. The first section up to the line in question then the changed line and then the rest of the file. Finally delete the old and rename the new one.

If that happens during visitor requests then it maybe problematic because multiple request can create a mess and I would suggest to use a database storage.

Matthew1980

7:42 pm on Oct 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Enigma1,

Well the file is certainly trusted, and I think that there will only ever be 40 users maximum but you never know, certainly < 100.

Fgets & Fseek, I shall have to look into that, I don't recall using those functions before...

Thanks for your input anyway.

Cheers,
MRb