I have a file named config.php, it stores the variables that connect to a database, (Username, password etc) I want to be able to edit these settings from a html form(I have it setup)
I however cannot find out how to redefine these variables from the form, I send the form to the page updateDatabaseSettings.php but don't know how to then change them.
Your help in this would be greatly appreciated.
The code I have so far:
config.php
$DBUSER="root";
$DBPASS="";
$DBHOST="127.0.0.1";
$DBNAME="mydb";
updateDatabaseSettings.php
$newDBHOST = $_POST['dbhost'];
$newDBNAME = $_POST['dbname'];
$newDBUSER = $_POST['dbuser'];
$newDBPASS = $_POST['dbpass'];
Say the user input 192.168.1.2 for $newDBHOST I want that to replace the text in $DBHOST
Thanks
Your updateDatabaseSettings.php script would need to overwrite the contents of the config.php file.
A simple approach would be to construct a string containing all of the new content (including the <?php ?> tags and all the variable declarations). You could then pass that string to file_put_contents() to overwrite the config.php file.
Remember to check your file permissions though. You need to allow the webserver to write to config.php, or it won't work.
you have many choices so it is hard to say but I would create two instances of DB connection - one default and second variable connection. Default connection can take configuration info from config.php and you can always connect to default db and second instance will always take configuration info directly from POST and create new connection when POST request is called.
Also you can use global variables and just rewrite them when you need or change config.php file directly with PHP (look fopen,fwrite,fclose,... functions).
I guess include config.php in updateDatabaseSettings.php and replace the values ? Or even set these variables as global ? Then I suppose you have to kill and init a new db connection. hard to answer, would need to know how your app is architectured.
Related
Kind note: Practicing PHP
I got the CRUD library from this repo: https://github.com/rorystandley/mysqli-crud-php-oop.
I setup the database, connected to to it and everything is great. I am only unaware of how to use this globally?
I have different pages for different data tables, I see it not logical to create multi "insert.php" files for each page.
Insert.php:
<?php
include('class/mysql_crud.php'); // Contains the server info (locahost, user, pass, table)
$db = new Database();
$db->connect();
$data = $db->escapeString("name5#email.com"); // Escape any input before insert
$db->insert('CRUDClass',array('name'=>'Name 5','email'=>$data)); // Table name, column names and respective values
$res = $db->getResult();
print_r($res);
I believe include(); won't solve it as each page has different data to pass around.
Should I create a condition for "if that page, then pass this data"??
Thanks in advance.
Currently I am working with Smarty and been busy with translations.
I am using the config files for translation, but I cannot find a way to collect all the vars that are not in my config file. When I don't have the translation in my config file, the output is blank.
My config files look like:
register = "Registreren"
username = "Gebruikersnaam"
password = "Wachtwoord"
login = "Inloggen"
In PHP I use:
$this->smarty = new Smarty();
$this->smarty->configLoad(THEME_DIR . "/translations/nl.conf");
$this->translations = $this->smarty->getConfigVars();
echo $this->translations["username"]; // output: Gebruikersnaam
I can use in my HTML:
{#password#}
{#username#}
{#password#}
{#login#}
But when I want to output a not yet translated var like this:
{#logout#}
My result is blank.
Does anyone know how to use a default function when this occurs? Or maybe add the not found var to my config file? Or at least, show the var name instead of nothing.
There is a way that doesn't need resorting to |default for each of your variables, but it requires a little change in one of the core files.
on line 340 of smarty/sysplugins/smarty_internal_data.php
replace
return null
by
return "#$variable#";
After this, all vars not defined int he conf file will appear as #name# (i.e. this is your #password#).
If for some reason you want a variable to be empty, just define it in the conf file as
variable = ""
The only way I found was this:
{#foo#|default:'foo'}
setting a default, if the variable is empty it will display that string.
http://www.smarty.net/docsv2/en/tips.tpl
I've been trying to solve this problem for days without success.
I am using blueimp Jquery File Upload and everything works fine but I need to save my pictures in different folders depending on a parameter send by url like /index.php?customer=160
<input type="hidden" name="customer" value="<?php print $_GET["id_cliente"];?>">
I created the hidden field in the form and got it in the uploadhanndler.php.
/files/'.$_POST['customer'].'
, here everything goes ok, the file is saved on the folder I wanted, but the next time I open the window /index.php?customer=160, the files listed are the files/ folder not the files/160/ folder or the number I want to list.
I realized I could use the PHP user directories, and the files are beeing saved in a folder like this 6afa0f7338b14aeed39eb7656f364b4e that comes from the session_id(), I tried then to change the session_id() with the number of the folder I want this way at the begining of the /index.php?customer=160
session_start();
session_id($_GET['customer']);
but the files are still beeing saved in folder 6afa0f7338b14aeed39eb7656f364b4e, and when I print the session_id() is 160.
PHP user directories is a good method to achieve what I want? what I am doing wrong?
Thanks in advance and excuse my poor english.
The code below will show you how to save a session ID called customer_id and then use it as a folder name. Please let me know if you need any more help.
// start (or restart) the session
session_start();
// set the customer id session var
$_SESSION['customer_id'] = // set this variable something that you've retrieved from the DB
// create a folder if it doesn't already exist
$dir = '/httpdocs/mySite/customers/' . $_SESSION['customer_id'];
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
even it has been a while since a last answer has been given to this question, I'd like to give a small update on that issue. Since version 5.17 of the jquery-file-upload library ther is a support for user-directories.
Within the UploadHandler.php you will find the option 'user_dirs' => false. Just set it to 'true' and you will have user-directories based on session-id.
If you want to have your own user-directories not based on session-ids, but e.g. based on user-id (your own defined Session key) you can proceed in the following way:
within the index.php file (as stated in the Demo of the jquery-file-upload library) you place following
right after "require('UploadHandler.php');"
class CustomUploadHandler extends UploadHandler {
protected function get_user_id() {
#session_start();
return $_SESSION[*<your own user id session key>*];
}
}
followed by:
$upload_handler = new CustomUploadHandler(array(
'user_dirs' => true
));
...and thats it...now you have your own user-directories for your uploaded files
I'm working on a project that needs to have users log into a MySQL database. I'm working through making an authentication system(auth/user table with their names, email, hashed password etc, php session), its more work than I realized but am making my way through it. I'm a little confused on the initial connection to the database though. I've read to keep it outside of the document root for the site, which I can do, but not sure how to reference it. Here is what I was thinking:
Document root for site:
/var/www/public/example.com/public/ {index.html,css,img,etc}
Could I safely place the db_connect info here:
/var/www/public/example.com/private/ securephpfunctions.php
I'd then have one line in all my php files:
<?php
// include in all files that need to do stuff
require_once('examplecom_fns.php');
?>
Inside examplecom_fns.php is:
<?php
// include in all files
require_once('/var/www/public/example.com/private/securephpfunctions.php');
require_once('forms_fns.php');
require_once('outputs_fns.php');
?>
After this, I assume I can call my DB connect function to insert new users info into the DB, use for not signed in users searching public entires, etc. Is that correct/OK? Is there a better or more secure way to do what I'm trying? Am I way off base?
You can reference your document root with the following:
$_ENV['document_root'];
Then, by adding a relative path, you can get your private path:
realpath($_ENV['document_root'] . '../private');
Then add to that the name of your private config file:
realpath($_ENV['document_root'] . '../private/securephpfunctions.php');
I have a mysql query that returns sum of values in multiple columns. The query is right and everything is working normally when i include it in the main page. But the problem starts when i make a function in another
page include the query over there and return the sum and print it in main page its not working .
Below is the main page call :
require('totals.php');
$res_easyhint_total=easyhint_totals($currentpid);
print $res_easyhint_total;
//The above is contained in a while loop and current pid gets updated each time.
Function page:
function easyhint_totals($currentpid){
require('connect.php');
$sql_easyhint_total = "SELECT sum(Coffee+Gift+Cools+Affection+Patience+Anger+EHignore) from whyangry.posts where Pid=$currentpid";
$res_easyhint_total=mysql_query($sql_easyhint_total,$con);
$res_easyhint_total=mysql_fetch_array($res_easyhint_total);
$res_easyhint_total=$res_easyhint_total[0];
return $res_easyhint_total;
}
I dont get what the error is please help.
Do you define any functions in connect.php? If not try adding this:
$res_easyhint_total=mysql_query($sql_easyhint_total,$con);
if (mysql_errno() != 0) {
echo mysql_error();
}
$res_easyhint_total=mysql_fetch_array($res_easyhint_total);
Have you checked the result from the new page there itself? I mean to say if you tried to print the result from the new page itself as like you tried from main page. Then one more thing need to concetrate that, the path of both file while including. Try to pass any other variable from the new page to main page and check if the new file included properly.
If you are able to access other variable from new page on main page and its just not returning the result from the function. Try to include connect.php on main page also and check it.
Check your connection string is returning the proper linked identifier and also check the logs if there is any error or warning from mysql like Warning: mysql_query(): [2002] No such file or directory (trying to connect via unix:///var/run/mysql/mysql.sock). In that case try to set the proper socket file location.
Have you included the file "connect.php" before?
if(!#include_once('connect.php')) {
// include connect.php
}