I'm trying to make a small script where if a user clicks on a desired language, the appropriate <div>'s inside the website will display:block; or display:none;
The problem is, I would like to maintain the language the user has chosen, throughout his visit to the website; so i read here and there a session might do the trick.
The plan is to include a code like this in each webpage; and retrieve the 'lang' variable from the session, but I can't seem to make it work
Code:
<?php
if(!isset($_SESSION))
{ session_start(); } //start a session if it's not started yet
if(!isset($_SESSION['lang']))
{ $_SESSION['lang']="eng";} // if no language was saved, make default language english
else{$_SESSION['lang']=$_SESSION['lang'];} // store ?previous? session data
?>
later on, for making the right <div>'s appear and disappear; i keep using the variable from the session; $_SESSION['lang']
would it work to put that code in a separate .inc.php file and then later on #include it everywhere (once it works)?
Well your description is rather vague, but as far as I know PHP doesn't support onclick events so you would need set the display:block with javascript.
and call it in you html code with something like this
<a href="#" onclick="showBlock('thisDiv')" >Show this div</a>
<div id="thisDiv" >
-- div contents ---
</div>
If you want a user to choose a language by clicking on something you have to either create a link for the language the basically links page to the page with the language choice appended to the url and then have PHP code at the top of your page that sets the session value based on the value of the get parameter, like below:
language link -> <a href="thispage.php?language=english" >English</a>
make sure you have session_start() at the top of every page that needs to refer to the $_SESSION['language'] variable. and also make sure that nothing else except for the <? of <?php tag comes before the your call to the session_start() function.
Last edited by webmaster54880; 06-20-2009 at 09:37 AM.
The reason why your code above isn't working is because you can not call the session_start() function based on the state of a $_SESSION variable. The php engine will not even check if the $_SESSION['lang'] is indeed set without a call to the session_start() function FIRST. This is very important => the session_start() function tells the php engine to prepare to handle $_SESSION variable that are either being declared in the the page or the are being passed from other pages.
change your code to this and see if it helps cleans up your problem
<?php
session_start(); // <- make sure there is no space before this line and
if(!isset($_SESSION['lang']))
{ $_SESSION['lang']="eng";} // if no language was saved, make default language english
?>
// AND BY THE WAY YOU DO NOT NEED THIS LINE OF CODE BECAUSE IT IS REDUNDANT.
else{$_SESSION['lang']=$_SESSION['lang'];} // store ?previous? session data
IF THE $_SESSION['lang'] variable was set in a previous page just include the code above at the top of every page and the $_SESSION['lang'] value will be retained for scripts that you have on other pages.
The key to session handling in PHP is to remember that they are super globals like $_POST an $_GET and don't need to be re-initialized or redeclared like regular variables on each new page that uses them. Just remember to use the session_start() "FIRST", lol.
Hope this helps.
Last edited by webmaster54880; 06-20-2009 at 09:59 AM.
Keep in mind that most servers are configured to destroy sessions after a certain amount of time. If you try to revive a day-old session, it will almost never work.
tyvm for the help so far, but it seems like i can't quite get it to work as it should...
i used the second method that was suggested, without posting the data inside the URL... since a session variable stays the same value across pages, unless you change it, that should be ok...
somehow when i switch to another page (or refresh) , the session language resets itself to english...
below are the codes i'm using: (i put JS alerts to see where it went wrong, but can't figure out why it goes wrong)
Bookmarks