So I have 2 different .ini files that stores different languages and I'm trying to choose which one I will read data from via a form.
Is there an easy way to do this, or should I use MySQL to switch between the files? With this I mean storing the filename and then changing the filename value in the database via the form.
Or as I'm trying to accomplish, store a $filename variable in PHP that holds either file 'a.ini' or file 'b.ini', depending on my choice.
It should also be possible to switch back and forth between the choices.
Right now I'm stuck and have no idea what to do.
I have this and I know I have to put it in a function, but from there I have no clue..
$ini_array = parse_ini_file("languages/EN.ini");
I'm trying to change the "EN" to a different value, but to no success so far.
My code right now, after som modifications: https://pastebin.com/a077jFE1
Right now I either have to refresh the page after submitting or submit again for the changes to take effect. Why is this occuring?
I would try something like this:
if (isset($_POST["your_form_field"])){
$ini_string = "languages/" . $_POST["your_form_field"] . "ini";
$ini_array = parse_ini_file($ini_string);
} else {
# Default
$ini_array = parse_ini_file("languages/EN.ini");
}
mmdts has a good answer too, store the posted value and use it on all your pages :)
Have you tried looking up storing data in a session?
A good guide on how to do this is available at:
How to set session in php according to the language selected?
You can follow the first answer to the letter in the php script present in the action of the form which allows the user to select the language.
And in all your other pages, you'd just check for
session_start(); // You have to call this at the start of each page to make sure that the $_SESSION variable works.
if ($_SESSION['lang'] == 'en')
$ini_array = parse_ini_file("languages/EN.ini");
etc.
And the full session documentation is available at http://php.net/manual/en/reserved.variables.session.php
Related
I have already searched an answer here and with google, but I don't found something. Because I'm having trouble ask the right question to find something.
What is the best way for this problem:
My Page: edit_data.php
I have a form (method="post" action="save.php"). On submit I store the data in a MySQL table.
In save.php:
MySQL insert (return the new id of dataset)
if success I call edit_data.php?id=<new_id>
if error I call error.php?msg=<error message>
The problem is that I lose the data on error.
This is what I want:
- go back to edit_data.php
- show the error directly there
- and I want that all fields contains their original data
I cannot take $_GET, because the data are too big.
Does anyone have an easy solution for me?
Thank you
Is there any reason you can't use $_SESSION? That way, all your data will be saved for the duration of the session, or until you delete it.
Make sure that every PHP document contains session_start(); before any headers are output, this also goes for any blank space before your <?php tag.
To put all your $_POST data in a $_SESSION['POST'] you could do something like,
<?php
session_start();
foreach($_POST as $key=>$val) {
$_SESSION['POST'][$key] = $val;
} ?>
Then you can access your previous POST variables by accessing $_SESSION['POST']['KEYNAMEHERE']
PS: $_GET and $_POST are interchangeable here
If $_GET is to big, you can edit your php.ini file.
Please note that PHP setups with the suhosin patch installed will have
a default limit of 512 characters for get parameters. Although bad
practice, most browsers (including IE) supports URLs up to around 2000
characters, while Apache has a default of 8000.
To add support for long parameters with suhosin, add
suhosin.get.max_value_length = in php.ini
http://www.php.net/manual/en/reserved.variables.get.php#101469
Max size of URL parameters in _GET
So I have a complicated PHP file that does some calculations and data-manipulation for me, what it does it not important, anyway I want to be able to access some of the variables I used in that file from a different PHP file.
I know I could include or require the first php file in the second php file, but then when one loads the second php file, one would be loading and re-preforming all the tasks in the first php file.
I want to run the first php file and then run the second file and have the second file pick up from where the first file left off in terms of the values of variables I used.
How can I do this?
I would prefer not to store the values of the variables I am going to use in a database adn the recover them in the second file, but if using a database is the only solution then please let me know.
You can use the $_SESSION superglobal to store user data across a site / application for each user.
In the first file you'd do something like :
<?php
// do shitload of calculations here !
if(empty( session_id() )) session_start(); //check if there is a session
//and start one if there is'nt
$_SESSION['myData'] = "results from my calculations goes here";
?>
In the next file that data is now available :
<?php
$data = isset( $_SESSION['myData'] ) ? $_SESSION['myData'] : 'Not available';
echo "Result of calculations : ". $data;
?>
You'll find everything you'll ever need to know about sessions in the PHP manual.
I am not sure if I understood correctly.
You can use this code at first file:
session_start();
$_SESSION['var_name']=var_value;
At second file:
session_start();
Now $_SESSION['var_name'] has the value setted before.
If you want to learn more about sessions check w3schools.com/php/php_sessions.asp .
I have created a JS script which has more optional settings.
So, somewhere near the beginning of the file I have something like this:
var doThat = true;
var playThat = false;
...
This script will be used by users and not by developers so they may not know how to edit a JavaScript file. How can I create a kind of admin-panel which would allow to change some variables in a JavaScript file.
I was thinking about creating an interface which will contain radio buttons to choose those values, now the problem is: how do I actually save those changes in the .js file? Should I use PHP to edit the js file directly or is there some better way?
Depends on who needs to have access to those variables.
If these variables are shared between all your visitors you should definitly save them on the server.
If you need them only per user and per session, save them via PHP in a session or with cookies at the user. Cookies can be created with JS aswell
If you need them only per user but persitant you can use the localStorage-Object. Nearly all modern browsers support this.
If you have a login-script and you need the variables persistent and per user you should use php, since this is the most reliable way to identify a user over time.
for this make an interface with radio button. then make all changes and save it to database.The saved result's corresponding work will be performed.
What you could do is have a PHP file that writes these JavaScript variables, in the following way:
$doThatVariable = true; // This variable could be retrieved from a database
$playThatVariable = false; // This variable could be retrieved from a database
// This code could be ran on relevant pages to correctly set up the JavaScript variables
print("<script type=\"text/javascript\">
var doThat = " . $doThatVariable . ";
var playThat = " . $playThatVariable . ";
</script>\n");
You would then be able to use the doThat and playThat variables in your JavaScript.
Hope this helps.
I am working on my personal site, where I want to store my customers recent search result limited to that particular session.
I am using PHP platform and Javascripts.
Here is an example of what I am exactly looking at :
It stores your previously searched domain name for that particular session so that user can make decision by comparing those results.
Thanks.
EDIT- Well Thanks for all of your answers and suggestions.
But If you have noticed
above example
It looks like some kind of script loading a new content on the same page without refreshing it and keeping previous search content <div> as it is.
How to achieve this using javascripts or some sort of div layer ????
UPDATE START
This example uses page reload. If you want to do it without page reload, you can but you'll have to use AJAX to load new search results. But then, it's not a PHP question. I suggest looking at jquery library, as it makes it easy. Tutorials: http://docs.jquery.com/Tutorials and e.g. this one ( http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax ).
When loading data via AJAX, the page rendering result (in my example search.php) should return only HTML for results part, not whole HTML page. This is generally a first part of my tutorial (without session).
But I really think that AJAX in here is not really needed. Session is more reliable and allows access to your page from older / mobile browsers where not always JS works correctly.
UPDATE END
Ok then. Let's try the simple tutorial then. Sorry if too simple, but I don't know your exact level.
PHP has mechanism called sessions. In reality they are just bytes stored on server. Server knows which session is for each client by reading session cookie from client browser.
Not every page uses sessions (not every page needs it, and session uses server space, even if only temporarily), session is not enabled by default. To turn on session you use command
<?php session_start(); ?>
In most cases this is either run by PHP framework you use, or put near the top of your site. Session is definitely needed if you want to authenticate user somehow. Or in your case :)
To access session you can use superglobal $_SESSION variable (superglobal means that you can access it anywhere). It's an array, so session element will be e.g. $_SESSION['search'] etc.
As example, let's assume that your page looks like that
<html>
...
<form action="search.php" method="post">
Search: <input type="text" name="searchQuery" />
<input type="submit" value="Search" />
</form>
...
</html>
this very form will send user search to file named search.php. It can be the same file where the form resides - in simplest case when you put both your code and HTML in one file. Beginners often use this schema, although it's not advisable as result is a mess and hard to further change.
In search.php then, you'll use similar code:
<?php
if (!empty($_POST['searchQuery'])) //we have a new search
{
$result = do_search($_POST['searchQuery']);
}
?>
Then, somewhere below you'll display your search result ($result variable). do_search() function is your search mechanism, I guess you have it somewhere. You may have it not 'wrapped' in a function, then I advise to create it like that, it's much more useful.
function do_search($searchQuery)
{
...
return $result;
}
mind it, the above code doesn't use sessions yet. Let's add saving previous search results in session. The code may then look like that:
<?php
session_start(); //Starting session
//let's create session variable used to store results
if (!isset($_SESSION['searches']))
$_SESSION['searches'] = array();
if (!empty($_POST['searchQuery'])) //we have a new search
{
if (isset($_SESSION['searches'][$_POST['searchQuery']]) //User already searched on this value, delete previous result from sesion
{
unset($_SESSION['searches'][$_POST['searchQuery']]);
}
$result = do_search($_POST['searchQuery']);
//Let's add new search on the begining of session array to make iterations easier.
$result = array($_POST['searchQuery'] => $result); //convert result to same format as session table
$_SESSION['searches'] = array_merge($result, $_SESSION['searches']);
}
?>
In display you'll now not iterate on $result variable as before, but instead you will do something like
foreach ($_SESSION['searches'] as $query => $result)
{
...//display of single result
}
I haven't tested following code and it's not a full program. Parts to display result and to do actual search are not described but I guess you have them already prepared. Also, this is only one possible approach of countless possibilities. But I hope this helps :)
Possible modification - now I always perform search, even if user already searched on this term. You may want to receive the result from cache without second search. Then the code will look like
if (isset($_SESSION['searches'][$_POST['searchQuery']]) //User already searched on this value
{
$result = $_SESSION['searches'][$_POST['searchQuery']];
unset($_SESSION['searches'][$_POST['searchQuery']]);
}
else
{
$result = do_search($_POST['searchQuery']);
}
For more in-depth information about sessions and some other constructs used in my example I suggest PHP manual
http://pl.php.net/manual/en/book.session.php
and various tutorials over the network. Or you can add a comment here :)
Put this code near the beginning of your script(s):
if (!isset($_SESSION['previous_searches']) || !is_array($_SESSION['previous_searches'])) {
$_SESSION['previous_searches'] = array();
}
[edit]
This code snippet checks if if there is already an array with prevous searches and if not it will be created.
[/edit]
Then when the user hits the search page put this code in the receiving script of the search:
$_SESSION['previous_searches'][] = $_GET['what_ever_your_search_value_might_be'];
[edit]
This code snippet adds the current search value to the and of the array with previous search values
[/edit]
Now you have all previous search values in $_SESSION['previous_searches']
If your website is a web application where you never reload the page nor change the page, you can keep it JavaScript in a global store (declare at top level something like var StoredSearch = []; and use it). If not, then use $_SESSION to store this and AJAX to save/load searches from JavaScript to PHP.
I render a page using YUI. and depending on the user I need to change how it is rendered. This change is not something that can be parametrized, it is drastic and different for each user.
Please tell me how can I generate Javascript dynamically?
I personally use a PHP file to pass a JavaScript object made up of some basic session and internal settings, nothing mission-critical as passing information to the client isn't overly secure, but I believe it might follow the same principles as what you are looking for.
Similarly, I use this to display certain elements once the client is logged in, although all the authorization is still done on the server-side. If my session handler gives the PHP file the ok, it outputs a JavaScript object using a PHP heredoc string, otherwise, it doesn't output anything. You can use attributes of this object to compare against, or you could output only the JavaScript for how a certain page should be rendered, based on settings in your PHP file.
HTML:
<script src="common/javascript/php_feeder.php" type="text/javascript"></script>
PHP:
//my session handler authorisation check has been removed
//although you could place your own up here.
//assuming session was authorised
//set content type header
header("content-type: application/x-javascript");
$js_object = <<<EOT
var my_object = {
my_attr: '{$my_attr}',
my_attr2: '{$my_arrt2}',
etc: '{$etc}'
}
EOT;
print($js_object);
You can probably create two separate Java script files, and include the required file, depending upon the user type.
Pseudocode
If user_type is One
<Script src='one.js' type='javascript'></script>
else
<Script src='other.js' type='javascript'></script>
End If
JavaScript has an eval function, so I think (I haven't tried it) that you can generate JavaScript by writing it into a string variable (and then calling eval on that string variable).
A little bit of elaboration here would most certainly help in getting you a more descript and helpful answer. That in mind, though, you could easily just use functions declared inside an if statement to provide distinctly varied experiences for different users.
A very basic example:
<script>
function do_something(userType)
{
if (userType == 'A')
{
// everything you need to do for userType A
}
if (userType == 'B')
{
// everything you need to do for userType B
}
}
</script>