Ben Phelps Posted September 16, 2006 Share Posted September 16, 2006 I am wondering how i can open a files (config.php) and change some values via a text box then save.Exampleconfig.php[code]<?php$site_url= "http://127.0.0.1/";?>[/code]I want to open config.php and change http://127.0.0.1/ to what ever is imputed in the text box.It will be an install script when its finished Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 16, 2006 Share Posted September 16, 2006 using fopen()fwrite()fclose()functions Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 16, 2006 Author Share Posted September 16, 2006 i don't have any idea how i could do that Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted September 16, 2006 Share Posted September 16, 2006 well you could make two php pages like so:[b]index.php[/b][code].....<body><form action="config.php" method="post">Change Ip to: <input type="text" name="ip"><input type="submit" name="submit" value="Submit New Ip"></form>....</body>[/code][b]config.php[/b][code]<?if(isset($_POST['submit'])) {$ipfield = $_POST['ip'];echo "Current Ip is:" . $ipfield . ".";}?>[/code] Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 that is close but i want it to physically edit the file.so before edit[code]<?php$site_url= "http://127.0.0.1/";?>[/code]after edit[code]<?php$site_url= "http://192.168.0.100/";?>[/code]it is a config file for a script I'm working on Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted September 17, 2006 Share Posted September 17, 2006 That script would physically edit the file, it takes the ip from a input box and changes the variable on the config page. I may not be be completely understanding of what you need but I think from what you just posted it should work for your needs. Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 i need it to open the file change http://127.0.0.1/ to what ever the user inputs and save the file for later use, not just change the var. when the script is being used Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted September 17, 2006 Share Posted September 17, 2006 with that said[code]fopen('config.php');fwrite($ipaddress);fclose();[/code] Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 what do i do with that ??? I'm sorry i don't understand php that well Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted September 17, 2006 Share Posted September 17, 2006 use php.net as a reference for function usage. fopen, fwrite, and fclose open files write text ot files and close an active file. Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 i have a script that writes the ip to the config.php but it writes to the end of the document. I need it to write in a certain place.[code]<?php$script_url = "it needs to write here" ;?>not here[/code]Here are the scripts i use now.get.php[code]<html><head><title></title></head<body><form action="write.php" method="post"> <p align="center"><strong>Script Installer </strong><br> Where is the script at :<br> <input type="text" name="ipfield"> <br> <input type="submit" name="submit" value="Install"> <br> Ex. <strong>http://www.thepchs.com/script/</strong></p></form></body><html>[/code]write.php[code]<?php$filename = 'config.php';$somecontent = "$ipfield";// Let's make sure the file exists and is writable first.if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent === TRUE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote $somecontent to file $filename"; fclose($handle);} else { echo "The file $filename is not writable";}?> [/code]config.php[code]<?phpscript_url = "";?>[/code] Quote Link to comment Share on other sites More sharing options...
GeorgeMoney Posted September 17, 2006 Share Posted September 17, 2006 You could use this script (untested).[code]<?phpif(isset($_POST['newip'])) {$newip = $_POST['newip'];$oldtext = file_get_contents('config.php');$newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext);$fp = fopen("config.php", "w");fwrite($fp, $newtext);fclose($fp);} else {?><form method="post" action="<?php echo $_SERVER['SCRIPT_URI'] ?>">IP: <input type="text" name="newip" /><br><input type="submit" value="submit" /></form><?php } ?>[/code] Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 THANK YOU VERY MUCH ;D ;D ;D ;D ;D ;D ;D ;D ;D ;DIt works ;D ;D ;D ;D ;D ;D ;D ;D ;D ;DNow is there a way i could do that like 3 more times editing something different every time? if not thats ok Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 its ok i figured it out, and once again thank you ;D ;D ;D ;D ;D Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 how can i get it to show a different page after i click submit. like a conformation page. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 17, 2006 Share Posted September 17, 2006 update.php[code]<?phpif(isset($_POST['newip'])) {$newip = $_POST['newip'];$oldtext = file_get_contents('config.php');$newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext);$fp = fopen("config.php", "w");fwrite($fp, $newtext);fclose($fp);header("location: thankyou.php");}?>[/code]form.php[code]<html><body><form method="post" action="update.php">IP: <input type="text" name="newip" /><br><input type="submit" value="submit" /></form></body></html>[/code]thankyou.php[code]<html><body>thank you</body></html>[/code]or easer[code]<?phpif(isset($_POST['newip'])) {$newip = $_POST['newip'];$oldtext = file_get_contents('config.php');$newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext);$fp = fopen("config.php", "w");fwrite($fp, $newtext);fclose($fp);echo "thank you";exit;} else {?><form method="post" action="<?php echo $_SERVER['SCRIPT_URI'] ?>">IP: <input type="text" name="newip" /><br><input type="submit" value="submit" /></form><?php } ?>[/code] Quote Link to comment Share on other sites More sharing options...
GeorgeMoney Posted September 17, 2006 Share Posted September 17, 2006 Or if you want them to press a 'confirm' button:[code]<?phpif(isset($_POST['newip'])) { if(isset($_POST['confirm'])) { $newip = $_POST['newip']; $oldtext = file_get_contents('config.php'); $newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext); $fp = fopen("config.php", "w"); fwrite($fp, $newtext); fclose($fp); } else { echo "Are you sure about that? <form method='post' action='$_SERVER[SCRIPT_URI]'><input type='hidden' name='newip' value='$_POST[newip]' /><input type='submit' name='confirm' value='confirm' /></form>"; }} else {?><form method="post" action="<?php echo $_SERVER['SCRIPT_URI'] ?>">IP: <input type="text" name="newip" /><br><input type="submit" value="submit" /></form><?php } ?>[/code] Quote Link to comment Share on other sites More sharing options...
Ben Phelps Posted September 17, 2006 Author Share Posted September 17, 2006 WOW!!! ;D ;DThanks for all the help. It works perfect now. ;D ;D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.