Jump to content

String replacement in a text file...


shakeit

Recommended Posts

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 :-))

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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);

  }

?>

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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
.
/*
** ...
*/

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.