Reputation: 661
Thanks a lot and sorry for bothering.. I'm still a PHP rookie, I am having a few problems trying to understanding/finding out how to keep multi session live together works.. here my existing sessions e.g.
session_start();
// If no session exists, create one
if (!session_is_registered('lesson')) {
$_SESSION['lesson'] = new lesson;
}
$lesson = $_SESSION['lesson'];
and then i want to add new session from code at below:
if (!isset($_POST['languageselect'])) {
$languageselect = $standardlanguage;
} else { /* set standard language */
$languageselect = $_POST['languageselect'];
} //endif
if($languageselect == 1) { /* My Language Pack */
$languagearray = array (
1=>"one",
2=>"two");
} else { /* English Language Pack (=standard) */
$languagearray = array (
1=>"one",
2=>"two");
function printoptionbox($boxname, $cssclass, $elementsarray, $kataktiv=1) {
echo "<select name='$boxname' class='$cssclass'>";
while (list($key,$value) = each($elementsarray)) {
if ($key == $kataktiv) {
$SELECTED = "SELECTED";
} else {
$SELECTED = "";
} //endif
echo "<option $SELECTED value='$key'>$value</option>";
} //endwhile
echo "</select>";
}
Here the HTML code from dropdown language options:
<td class="arial" width="210"><?php printoptionbox("languageselect", "languageselect", $language_array, $kataktiv=$languageselect); ?></td> <input type="submit" name="languageselectsubmit" value="OK" width="30" style= "width: 30px; font-size:10px" class="submitbutton">
but i most confuse, how to create the session from language code above and then would be work for another page (different code), because the multiple language options just work in first page but will resetting for next page... Thank you so much for any advice on this
Upvotes: 0
Views: 1007
Reputation: 30481
First of all, I am not entirely sure you fully understand the concept of session and how it works on PHP. Session in PHP refers to all variables (language, lesson, etc.), not to single variables. So what you really are looking for is not how to keep "multi session live", but rather on how to "store multiple variables into a session".
You can save multiple variables into a single session by saving them as keywords inside $_SESSION. For instance you can save language setting to session by invoking $_SESSION['lang']='en';
. Similarly you can add "lesson" variable to session by invoking $_SESSION['lesson']=1;
.
I would strongly suggest reading through a tutorial on how PHP sessions work. For instance, take a look to PHP Sessions on w3schools.com.
Taking it to the level of your code, there are several points that you could consider taking a look into:
printoptionbox
from $_SESSION
.As a quick test you could try replacing your call with:
<?php printoptionbox("languageselect", "languageselect", $language_array, $_SESSION['language']); ?>
Also:
$_SESSION
!As a side note:
session_is_registered
may potentially also cause problems, since it has been deprecated in the latest versions of PHP. Also, you can replace it straight away with isset
instead:// If no session exists, create one if ( isset($_SESSION['lesson']) ) { ... }
Upvotes: 1
Reputation: 25249
I think you misunderstood the concept behind the $_SESSION global. It's really just a global associative array that is used to store values between different requests.
Everything you put into that global during execution will be saved until the next request takes place or the session times out.
So, consider this example:
// file1.php
session_start();
$_SESSION['lesson'] = new lesson();
// file2.php
session_start();
$_SESSION['language_code'] = 1;
// file3.php
session_start();
// dumps both values we packed into the session from the previous requests
var_dump($_SESSION);
Upvotes: 2
Reputation: 16827
I feel like maybe I didn't quite follow your intention here, but I want to mention that 1) you will not be able to maintain two sessions at the same time and 2) I don't think you would really need to anyhow.
The session can store array data to be retrieved later. If you are sure you want to store that data in session then just add your array and draw it back out when needed. What you need to do is write your code so that if they choose a different language the new language is swapped out in the session by overwriting the old array.
You may find it easier (as I have very recently) to store your language arrays in flat files serialized on the server and then draw them out based on users preferences. This is very low on overhead, very quick, and you gain the ability to 'cache' your language so that there is little to no processing needed.
Upvotes: 3