Page 1 of 1
Problem with a variable.
Posted: Thu May 24, 2007 6:13 am
by Dalfin
Hi All,
I am still a noob to PHP, just started learning it.
I am trying to write a PHP that will install a DB and also write some lines in a file which is the global.inc.php .
But i have some problems.
Problem 1.
I take some variables and put values into them by using a form.
Then i want to write some lines in global.inc.php using these values.
This is how the result must be in the file:
Code: Select all
<?php
$glob['dbdatabase'] = 'mydb';
$glob['dbhost'] = 'localhost';
$glob['dbpassword'] = 'pswd';
$glob['dbprefix'] = 'data';
$glob['dbusername'] = admin';
$glob['installed'] = '1';
$glob['rootDir'] = 'http:\\localhost\site';
$glob['rootRel'] = '/cpplessons/';
$glob['ptyxiakiURL'] = 'http:\\localhost\site\';
?>
This is the code i have written in order to produce these result using fwrite:
Code: Select all
$filename = 'includes/global.inc.php';
$somecontent = "'<?php'\n
'$glob'['dbdatabase'] = ''$database'';\n //$database="mydb";
'$glob'['dbhost'] = ''$host'';\n //$host="localhost";
'$glob'['dbpassword'] = ''$password'';\n //$password="pswd"; and so on...
'$glob'['dbprefix'] = 'data';\n //data is not a variable
'$glob'['dbusername'] = ''$username'';\n
'$glob'['installed'] = '1';\n
'$glob'['rootDir'] = ''$url'';\n
'$glob'['rootRel'] = '/'$relation'/';\n
'$glob'['ptyxiakiURL'] = ''$url\'';\n
'?>'" ;
if (is_writable($filename)) {
if (!$handler = fopen($filename, 'a')) {
echo "Cannot open file ($filename)\n";
exit;
}
if (fwrite($handler, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)\n";
exit;
}
echo "Succesfull write ($somecontent) to file ($filename)\n";
fclose($handler);
}
else {
echo "Cannot write to file!\n";
exit;
}
The problem is i don't know how must i write things for $somecontent in order to take $glob as text and $database as variable and write to file the appropriate value. I think it's a prob with " and ' but i can't quite grasp it.
Can anyone tell me what is the right way to do that in order to get the result i posted on top?
Thanks in advance.
Posted: Thu May 24, 2007 6:37 am
by Maugrim_The_Reaper
You can use two functions - serialize() or var_export(). Both can do similar things. If the file content doesn't need to be completely readable use serialize():
Code: Select all
$store = array('name' => 'Maugrim', 'password' => hash('sha256', 'password'));
$stringStore = serialize($store);
echo $stringStore;
// get it back!
$storeCopy = unserialize($stringStore);
var_dump($storeCopy);
Otherwise if it needs to be readable, use var_export(). Note var_export() uses eval() to rebuild the array into a named variable so you should be careful user input NEVER finds it's way in there or you'll have a serious security weakness. Obviously this means serialize() is the safer one to use where possible.
Code: Select all
$store = array('name' => 'Maugrim', 'password' => hash('sha256', 'password'));
$stringStore = '$storeCopy = ' . var_export($store, true) . ';';
echo $stringStore;
// get it back!
eval($stringStore);
var_dump($storeCopy);
Posted: Thu May 24, 2007 6:38 am
by volka
try
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', true);
$glob = array(
'dbdatabase' => $database,
'dbhost' => $host,
'dbpassword' => $password,
'dbprefix' => 'data',
'dbusername' => $username,
'installed' => '1',
'rootDir' => $url,
'rootRel'=> $relation,
'ptyxiakiURL' => $url
);
if ( !file_put_contents($filename, '<?php $glob=' . var_export($glob, true) . ';') ) {
echo 'failed to write $glob to '.$filename;
}
Posted: Fri May 25, 2007 3:26 am
by Dalfin
Hi Again,
I tried to implement what volka suggested but when i run it i get this msg:
Fatal error: Call to undefined function: file_put_contents() in c:\program files\easyphp1-8\www\ergasia\install.php on line 64
What have i done wrong?
Posted: Fri May 25, 2007 3:32 am
by maliskoleather
what version of php are you running?
you need php5 for file_put_contents();
otherwise, do it the longer way:
fwrite();
Posted: Fri May 25, 2007 4:26 am
by Dalfin
I see.
Can someone give me a heads up on how to use the table with fwrite?
EDIT:
This doesn't work.
glob is considered a variable io simple text:
Code: Select all
if (fwrite($handle, "<?php $glob=['dbdatabase']='" . $database . "';") === FALSE)
Posted: Fri May 25, 2007 5:21 am
by volka
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', true);
$glob = array(
'dbdatabase' => $database,
'dbhost' => $host,
'dbpassword' => $password,
'dbprefix' => 'data',
'dbusername' => $username,
'installed' => '1',
'rootDir' => $url,
'rootRel'=> $relation,
'ptyxiakiURL' => $url
);
$text = '<?php $glob=' . var_export($glob, true) . ';';
$fd = fopen($filename, 'wb') or die("fopen($filename, 'wb') failed");
if ( !fwrite($fd, $text) ) {
echo 'failed to write $glob to '.$filename;
}
fclose($fd);
Posted: Fri May 25, 2007 5:36 am
by Dalfin
Tks a lot volka.
Now i can write to the file but the result i get is:
Code: Select all
<?php $glob=array (
'dbdatabase' => 'mydb',
'dbhost' => 'localhost',
'dbpassword' => 'pswd',
'dbprefix' => 'data',
'dbusername' => 'root',
'installed' => '1',
'rootDir' => 'http://localhost/site',
'rootRel' => 'ergasia',
'ptyxiakiURL' => 'http://localhost/site',
);
instead of
Code: Select all
<?php
$glob['dbdatabase'] = 'mydb';
$glob['dbhost'] = 'localhost';
$glob['dbpassword'] = 'pswd';
$glob['dbprefix'] = 'data';
$glob['dbusername'] = 'root';
$glob['installed'] = '1';
$glob['rootDir'] = 'http://localhost/site';
$glob['rootRel'] = '/ergasia/';
$glob['ptyxiakiURL'] = 'http://localhost/site/';
?>
Any ideas?
Hoping not to be too much of a pain.
Posted: Fri May 25, 2007 5:50 am
by volka
ah, so this isn't the only "place" where $glob is set? You want to set elements on an existing array $glob?
maybe
Code: Select all
$glob = array(
'dbdatabase' => $database,
'dbhost' => $host,
'dbpassword' => $password,
'dbprefix' => 'data',
'dbusername' => $username,
'installed' => '1',
'rootDir' => $url,
'rootRel'=> $relation,
'ptyxiakiURL' => $url
);
$text = '<?php $glob= array_merge($glob, ' . var_export($glob, true) . ');';
$fd = fopen($filename, 'wb') or die("fopen($filename, 'wb') failed");
if ( !fwrite($fd, $text) ) {
echo 'failed to write $glob to '.$filename;
}
fclose($fd);
will suffice.
Posted: Fri May 25, 2007 5:55 am
by Dalfin
These are the settings that must be written to a config file in order for the site to work.
So, this file must have written inside exactly this text and the only things that change are the variables like password, user etc.
Posted: Fri May 25, 2007 5:59 am
by volka
There's little difference between
Code: Select all
<?php
$glob = array();
$glob['abc'] = 1;
$glob['xyz'] = 2;
print_r($glob);
and
Code: Select all
$glob = array(
'abc'=>1,
'xyz'=>2
);
print_r($glob);
Why can't it be the second version?
(there may be a reason, I just want to know why)
Posted: Fri May 25, 2007 6:07 am
by Dalfin
volka wrote:ah, so this isn't the only "place" where $glob is set? You want to set elements on an existing array $glob?
maybe
Code: Select all
$glob = array(
'dbdatabase' => $database,
'dbhost' => $host,
'dbpassword' => $password,
'dbprefix' => 'data',
'dbusername' => $username,
'installed' => '1',
'rootDir' => $url,
'rootRel'=> $relation,
'ptyxiakiURL' => $url
);
$text = '<?php $glob= array_merge($glob, ' . var_export($glob, true) . ');';
$fd = fopen($filename, 'wb') or die("fopen($filename, 'wb') failed");
if ( !fwrite($fd, $text) ) {
echo 'failed to write $glob to '.$filename;
}
fclose($fd);
will suffice.
Using above i get this:
Code: Select all
<?php $glob= array_merge($glob, array (
'dbdatabase' => 'ergasia',
'dbhost' => 'localhost',
'dbpassword' => '@dm1n',
'dbprefix' => 'data',
'dbusername' => 'root',
'installed' => '1',
'rootDir' => 'http://localhost/ergasia',
'rootRel' => 'ergasia',
'ptyxiakiURL' => 'http://localhost/ergasia',
));
inside my php file.
Posted: Fri May 25, 2007 6:09 am
by Dalfin
volka wrote:There's little difference between
Code: Select all
<?php
$glob = array();
$glob['abc'] = 1;
$glob['xyz'] = 2;
print_r($glob);
and
Code: Select all
$glob = array(
'abc'=>1,
'xyz'=>2
);
print_r($glob);
Why can't it be the second version?
(there may be a reason, I just want to know why)
I think i see what you mean.
It is just that these lines are meant to be for a configuration file that all other pages use to view properly.
I just thought it had to be the same.
I will try your previous code again.
Posted: Fri May 25, 2007 6:17 am
by Dalfin
You were 100% correct. It was my poor knowledge that made things hard!
Site is working fine.
Do you thing it would be too much posting another question...?
Well, here goes.
In the same file i also create a DB and want to import data from an exported file i have.
Problem is that mysql_query() does not support multiple queries.
The file with the DB Data has many queries (3x Create table and much more INSERT INTO) .
What is the best way to execute this sql file?
Posted: Fri May 25, 2007 6:58 am
by volka
The
mysqli[/i] extension has a function multi_query() that can handle more than one statement at once.