shakeit Posted December 18, 2007 Share Posted December 18, 2007 Hello everyone.. I am having trouble replacing a string I get from input to a specific location in a text file... $server = "localhost"; $username = "root"; $password = "Something"; .... I want to let the user put in his username and password in textfields and then replace what he has written with a file containing the data written above... Could anyone help me? I do not need the code for input fields and so on, just help with the code which takes the string and replaces "root" and "something".. Thanx in advance :-)) Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/ Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 You need to create a FORM, then take those inputs and then you can have the $variable equal their input. Then just save the file. Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-417775 Share on other sites More sharing options...
shakeit Posted December 18, 2007 Author Share Posted December 18, 2007 No .. becuase i only want to do this once.. and every time i need that file i would need to register new username and password, wouldnt I? Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-417792 Share on other sites More sharing options...
kjtocool Posted December 18, 2007 Share Posted December 18, 2007 In general, you have to get the information from the user somehow. What the above poster was saying is that you need to first create a form. This form would have two text areas, where the user could enter in his information, and then a submit button so he can submit the information. You would then grab the information with either a $_GET or $_POST statement. Once you have the information, you can do whatever you want with it, such as: $username = $_POST['username']; $password = $_POST['password']; Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-417793 Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 Explain further what you are trying to do exactly. Are you trying to setup some kind of setup form for someone to connect to a database? Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-417794 Share on other sites More sharing options...
shakeit Posted December 18, 2007 Author Share Posted December 18, 2007 Hmm.. yes, i see now that i explained my first post very poorly, so i will try a little bettter:Like you said revraz.. i am trying to make a kind of setup solution so that the user can write his mysql server adress, username and password in inputfields.. then he presses submit and the config while, which contains information like written above: $server = "localhost"; $username = "root"; $password = "Something"; .... This fields will then get modified only once, replacing the fields with the users input information , so that the user do noe need to write this again, only once...Thats why i cannot use $_POST and set: $server = "$servername"; $username = "$username"; $password = "$password"; .... I think this is more lucid now...? Thanks for helping guys... Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-417910 Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Why start with a default config file? Just create one when your user fills out the form. eg; <?php if (isset$_POST['submit'])) { $write = ' <?php $server = "' . $_POST['server'] . '"; $username = "' . $_POST['username'] . '"; $password = "' . $_POST['password'] . '"; ?>'; file_put_contents('config.php',$write); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-417917 Share on other sites More sharing options...
shakeit Posted December 19, 2007 Author Share Posted December 19, 2007 Why start with a default config file? Just create one when your user fills out the form. eg; <?php if (isset$_POST['submit'])) { $write = ' <?php $server = "' . $_POST['server'] . '"; $username = "' . $_POST['username'] . '"; $password = "' . $_POST['password'] . '"; ?>'; file_put_contents('config.php',$write); } ?> Yeah but this config file is used further in the class to connect to database and so on, so it contains a lot of data in the config file which is loaded at every webpage, for instance: <?PHP //------ Paramaters to change ------// $server = "localhost"; //databasename $uname = "root"; //username $pw = "something"; //password $dbprefix = 'test'; //the prefix of the database and tables $database = $dbprefix.'community'; //-------- ------------- --------------// //tables that are used by the objects $users_table = $dbprefix."users"; $statistic_table = $dbprefix."statistics"; $administrators_table = $dbprefix."administrators"; $online_table = $dbprefix."online"; $vote_table = $dbprefix."vote"; $vote_statistic = $dbprefix."vote_statistics"; // Login / Logout features $incorrectLogin = 'Incorrect login'; //Incorrect login message $loginHeader = 'tull.php'; //Page to redirect after successfull login $logoutHeader = 'logout.php'; //Page to redirect after wanting to logout $frontpage = 'front.php'; //Redirect page after successfull logout //Script options $nameLengthMIN = 2; $adressLengthMIN = 5; $emailLengthMIN = 5; $phoneLengthMIN = 8; $cityLengthMIN = 2; $countryLengtMIN = 2; $usernameLengthMAX = 20; $passwordLength = 4; $sendMail = true; //errormessages $couldNotConnectMysql = "<BR>Error!<BR>Could not connect to MySQL.<BR>\n Please check your settings in config.php"; $couldNotOpenDB = "<BR>Error!<BR>Could not open database.<BR>\n Please check your settings in config.php"; $invalidDate = "<BR>Error!<BR> Invalid date format OR early year, must be YYYY-MM-DD"; $blankField = "<BR>Error!<BR>You must enter a date and at least two alternatives"; $noSelection = "<BR>Error!<BR>You must select one alternative"; //.......................... Connecting to database ............................................// $connect2 = mysql_connect($server,$uname,$pw); $query = "use $database"; if( mysql_query($query) == null ) { $query = "CREATE DATABASE $database"; $result = mysql_query($query); if( $result == 1 ) { $connect = mysql_connect($server,$uname,$pw) or die ($couldNotConnectMysql); mysql_select_db($database,$connect) or die ($couldNotOpenDB); } else { echo "Error in database (Errornumber ". mysql_errno() .": \"". mysql_error() ."\")<br>"; } } else { //it already exists, so we connect $connect = mysql_connect($server,$uname,$pw) or die ($couldNotConnectMysql); mysql_select_db($database,$connect) or die ($couldNotOpenDB); } ?> It would not be so good to write all of this? Isnt it a way just to replace the three first variables? Thanx... Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-418057 Share on other sites More sharing options...
rab Posted December 19, 2007 Share Posted December 19, 2007 Just include the user's config file in the database config one. <?PHP //------ Paramaters to change ------// include "/path/to/$user/config.php"; //-------- ------------- --------------// //tables that are used by the objects $users_table = $dbprefix."users"; $statistic_table = $dbprefix."statistic . /* ** ... */ Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-418066 Share on other sites More sharing options...
shakeit Posted December 20, 2007 Author Share Posted December 20, 2007 Write an explicit file for the user information and include it in database config file... hmmm.. yeah, that might work, but it will make two files... but a good solution.. thanx... :-) Quote Link to comment https://forums.phpfreaks.com/topic/82208-string-replacement-in-a-text-file/#findComment-419327 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.