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
Related
I have made a program that lets you enter the username and password then it stores it in a text file and when i want to login i want to make it so it loops through the text file that has the usr/pass in to find if you entered your credentials in correctly. I'm not sure how to do this. please can somebody help
Example text.txt file:
$ cat text.txt
text1|answer1
text2|answer2
text3|answer3
Example code:
cat test2.php
<?php
$text="text1";
$file="text.txt";
$f = fopen($file, 'r');
while($data = fgets($f))
{
$ar_data=explode('|',$data);
if($ar_data[0]==$text) {
echo "looking for: ".$ar_data[1]."\n";
}
}
Example usage:
$ php test2.php
looking for: answer1
The text fileis not a good way for verify user credentials. You should try sql database. sqlite3 for example.
You can just hard code them in an array:
$passwords = ['someuser' => 'password'];
If you really want to store them in a file, so you can change them (for example) without editing the code, One way is to use something like this:
$passwords = ['someuser' => 'password'];
file_put_contents('passwords.php', '<?php return '.var_export($passwords,true).';');
This will create a file with something this in it (white space not withstanding):
<?php return array('someuser' => 'password');
Then when you need to import it into code you can simply use
$passwords = require 'passwords.php';
Which will put the contents of that file into that variable. Then you can check them really easily like so:
$passwords = require 'passwords.php';
if(isset($passwords[$user]) && $passwords[$user] == $password){
//do something when logged in
}
You can also modify the array and then save it:
$passwords = require 'passwords.php';
$passwords['someuser'] = $new_password;
file_put_contents('passwords.php', '<?php return '.var_export($passwords,true).';');
Of course you can even edit the passwords manually in the file. Sort of like a config file.
As I mentioned in the comments, it's better to use the DB, encryption and what not but as you said
this is only for me and someone else
As long as you don't have any third party data, and your ok with the security implications of this, then you can probably squeak by with the above.
To explain it:
Var Export converts arrays to valid PHP code, but in a string format. Then if we add the PHP open tag <?php, the return call return and the ending ; to it and save it in a PHP file, we now have a valid PHP file with dynamic passwords saved in it as an array.
When you have such a file that returns an array, you can inject that into a variable just by setting it like I showed above. Then it's a simple matter of checking to see if everything matches up.
Performance wise your offloading most of the penalty of this to saving the file, importing an array like this is very fast as is the key lookup. Much faster then iterating though a file and trying to parse the data from it.
You'll have to get the paths and filenames right and all that Jazz, but it should be pretty strait forward.
I have a problem with the following code. How do i put 2 included files into 1 line include?
I already tried it like the code below but nothing is shown (blank page). It should show the url.
Url: theme/default/main/index.php (it would be)
application-sql-realtime.php = (should show 'default' or anything else when user changing their themes template it connect to sql)
<?php include('./theme/<?php include_once("config/application-sql-realtime.php");?>/main/index.php');?>
Refer image for code
include_one returns boolean
Maybe can be like this:
<?php
$theme = exec("php config/application-sql-realtime.php");
include("./theme/{$theme}/main/index.php");
?>
But I think you better put it in some class/function
<?php
// inside application-sql-realtime.php you declare a function to return theme name eg: getThemeName
include("config/application-sql-realtime.php");
$theme = getThemeName();
include("./theme/{$theme}/main/index.php");
?>
Make a class with a static or non static method to retrive the value you need (e.g. 'default'). Then you can implement this value in your string to include the first file. Following an example of a static method call:
<?php
$val = EgClass::getPathPart();
include('./theme/'.$val.'/main/index.php');
?>
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.
I want to create a installer for my current project, that automatically modifies a dozens of config files.
So if the form was sent, the PHP script should look in which config file the searched option is and change it. Before you ask, I cant put the files together ;) .
A basic config line looks like this:
$config['base_url'] = 'test';.
I tried to use str_replace()but this didn't work because I don't know what is currently in the variable.
So I need a function that searches for $config['base_url'] = '%'; in multiple files and replaces it with $config['base_url'] = 'new_value'; (for example).
I realise the answer's accepted, and originally I deleted this, however, in the comments you mention the config being editable, which presumably means by other users, so you can't guarantee the spacing will match, nor that they'll use ' instead of " always, so the following is perhaps a little more forgiving
$name = 'base_url';
$value = 'new_value';
$config = '$config["base_url"] = "old_value";';
$config = preg_replace('/\[(?:\'|\")'.$name.'(?:\'|\")\]\s*=\s*(\'|\")(.*)\\1;/', "['".$name."'] = '$value';", $config);
echo '<pre>', var_dump($config), '</pre>';
You can use a regular expression like the following:
/\$config\['base_url'\] = '[a-zA-Z0-9]';/
Which you would have to adapt to each line.
A better solution, in my opinion, would be to create a template config file with lines like the following:
$config['base_url'] = '%BASE_URL%';
Where you could simply use str_replace().
I know that CakePHP params easily extracts values from an URL like this one:
http://www.example.com/tester/retrieve_test/good/1/accepted/active
I need to extract values from an URL like this:
http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
I only need the value from this id:
id=1yOhjvRQBgY
I know that in normal PHP $_GET will retrieve this easally, bhut I cant get it to insert the value into my DB, i used this code:
$html->input('Listing/vt_tour', array('value'=>$_GET["id"], 'type'=>'hidden'))
Any ideas guys?
Use this way
echo $this->params['url']['id'];
it's here on cakephp manual http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params
You didn't specify the cake version you are using. please always do so. not mentioning it will get you lots of false answers because lots of things change during versions.
if you are using the latest 2.3.0 for example you can use the newly added query method:
$id = $this->request->query('id'); // clean access using getter method
in your controller.
http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::query
but the old ways also work:
$id = $this->request->params->url['id']; // property access
$id = $this->request->params[url]['id']; // array access
you cannot use named since
$id = $this->request->params['named']['id'] // WRONG
would require your url to be www.example.com/tester/retrieve_test/good/id:012345.
so the answer of havelock is incorrect
then pass your id on to the form defaults - or in your case directly to the save statement after the form submitted (no need to use a hidden field here).
$this->request->data['Listing']['vt_tour'] = $id;
//save
if you really need/want to pass it on to the form, use the else block of $this->request->is(post):
if ($this->request->is(post)) {
//validate and save here
} else {
$this->request->data['Listing']['vt_tour'] = $id;
}
Alternatively you could also use the so called named parameters
$id = $this->params['named']['id'];