Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Using php to open and edit variables in a file, possible?
Old 12-25-2007, 11:30 PM Using php to open and edit variables in a file, possible?
J0ker's Avatar
Skilled Talker

Posts: 61
Trades: 0
I want it so I can put all my variables in a file, say config.php and have a little admin thing to edit the values in the file without having to use a database. Is this possible all the file opening/writing tutorials don't seem to talk about this. What I want is to edit the already existing variables.
__________________

Please login or register to view this content. Registration is FREE
- Free Gaming Forum/Arcade

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
J0ker is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-26-2007, 05:47 AM Re: Using php to open and edit variables in a file, possible?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
What is the problem exactly? You cannot read the file or parse the variables out of it or maybe you cannot process the POST request to save changes back to file?
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-26-2007, 05:52 AM Re: Using php to open and edit variables in a file, possible?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Wel, I guess that parsing is the most challenging part here.

First you need to read the whole config file into a string using something like file_get_contents(). Next you have to write some regexp to preg_match_all(). I think it would look like the following:
PHP Code:
$re "!^\s*\($[^\s]+)\s*=\s*(['"]?)(.*)\\1;$!i"; 
After you preg_match_all() you will have to use $1 and $3. Draw a form and process it, I suppose taht these two you can manage yourself.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-26-2007, 06:12 AM Re: Using php to open and edit variables in a file, possible?
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
You have a configuration file, which contains variables, however it sounds like you want to treat it as an ordinary file, where you can alter the information that's already saved in it, and then re-save the file with the new configuration.

It's possible, but how do you want to go about it?

I suggest the first thing you want to look at, is backing up the file you are about to change.

PHP Code:
copy('config.php''config.old.php'); // extension left as php so code is hidden 
That way, you can revert back to the old one, if it fails.

This is where you decide on handling what to do with the file.

I think the best way for you would be to create a form that mimics the variables and values of this configuration file, and allow yourself to make the changes using the form. When you submit the form, the changes you want will be made.

If possible, maybe you can find another solution for this? I don't think allowing a configuration file to be writable a good thing, to me if you did need to do this, then you should probably plan for your site to go down for maintenance while this happens, because you might have some unexpected results messing with the configuration in a live site.

You can also just include at the end of your config.php file another file that contains similar variables/values that has your changes in it, that way, if it's at the end of the configuration file, it will overwrite any existing variables with your information.


Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Old 12-26-2007, 06:25 AM Re: Using php to open and edit variables in a file, possible?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Quote:
Originally Posted by mastercomputers View Post
then you should probably plan for your site to go down for maintenance while this happens, because you might have some unexpected results messing with the configuration in a live site
You never heard about development server? I think it is rather obvious to test the script in the sandbox before putting it into production.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-26-2007, 06:35 AM Re: Using php to open and edit variables in a file, possible?
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
mtishetsky,

I'm not saying he's just developing and testing this script out, I'm envisioning that when he has the script working and that he intends on using it, it would be put to use on a live site, so I'm suggesting he schedules these types of events to keep users aware and so he can take it off while the changes are made, unless altering the configuration file does not affect the site but even I don't trust changing the config while a live site is operating.

Sorry if I didn't make myself clear.


MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Old 12-26-2007, 09:12 AM Re: Using php to open and edit variables in a file, possible?
Ultra Talker

Posts: 483
Trades: 0
I actually wrote a script to do this exact thing a few years ago. I'll be buggered if I can find it, though.

In reality, the smarter thing to do is just place all of your settings into a simple DB table called 'settings' or something. In your 'config' file, just have your DB connection settings and store the rest of the stuff in the DB. That way you don't need to worry about accessing, reading, rewriting etc. your config files.
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 12-26-2007, 12:51 PM Re: Using php to open and edit variables in a file, possible?
J0ker's Avatar
Skilled Talker

Posts: 61
Trades: 0
Yeah I guess I should use a database, I just was trying to find any easier way around it but, that's ok thanks anyways.
__________________

Please login or register to view this content. Registration is FREE
- Free Gaming Forum/Arcade

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
J0ker is offline
Reply With Quote
View Public Profile
 
Old 12-26-2007, 02:27 PM Re: Using php to open and edit variables in a file, possible?
Novice Talker

Posts: 13
Name: hach22
Trades: 0
http://www.webmaster-talk.com/php-fo...tml#post516788
hach22 is offline
Reply With Quote
View Public Profile
 
Old 12-27-2007, 03:28 AM Re: Using php to open and edit variables in a file, possible?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
The most simple way is to place the whole file contents into a single textarea, edit and save. Coding will take 5 minutes but you will have to take care of what you edit to avoid misspelling, missed quotes and other things of that kind.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Using php to open and edit variables in a file, possible?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31564 seconds with 12 queries