469,271 Members | 1,191 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Setting session variable with hyperlink


I have a session variable stored within a cookie, and I want this to change when pressing a hyperlink.

i.e. I have this variable
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['language'] = 'en_US';
This is set initially if it hasn't been set before using an if statement:
Expand|Select|Wrap|Line Numbers
  1. if (! isset($_SESSION['language']))
  2.     $_SESSION['language'] = 'en_US';
  3. }
I want to set this variable to something else using a hyperlink; like a language bar:

Expand|Select|Wrap|Line Numbers
  1. <a href="<?php $_SESSION['language'] = 'nb_NO'; ?>">Bokmål</a> |
  2. <a href="<?php $_SESSION['language'] = 'nn_NO'; ?>">Nynorsk</a>
The problem is that the code enclosed in the php tags is invoked wether i press the link or not. It just runs. How do I restrict it to run only when pressing the link?

Thanks in advance.
Dec 22 '08 #1
6 11494
6,050 Expert 4TB
Ok, no problem; this is easily fixed. Your logic is wrong for the above. Let's fix it!

Each time you do
Expand|Select|Wrap|Line Numbers
  1. <?php $_SESSION['language'] = 'lang'; ?>
you're resetting the session variable to the given language. What you actually want to do is just pass the language value to the href attribute. Like so:

Expand|Select|Wrap|Line Numbers
  1. <a href="?lang=nb_NO">Bokmål</a> |
  2. <a href="?lang=nn_NO">Nynorsk</a>
This then passes the language to the URL which you can later obtain using GET. Consider:

Expand|Select|Wrap|Line Numbers
  1. <?php
  3. /*
  4.    This checks that the parameter exists in the query string. If it does
  5.    we assign that value to the $lang variable. If it doesn't, we assign a default value: en_US
  6. */
  7. $lang = ( isset ( $_GET['lang'] ) ? $_GET['lang'] : 'en_US' );
  9. echo $lang;
  11. ?>
Does that help you?
Dec 22 '08 #2
Hi, yes that works pretty well :D

but I also encountered another problem that I didn't figure out how to solve well (when trying to apply this to multiple pages within my site). I have, say, three pages one.php, two.php and three.php, and all of them now has this language selection bar:

Expand|Select|Wrap|Line Numbers
  1. <div id="navbar"><div class="langbar"><a href="?locale=en_US">English</a> | <a href="?lang=de_DE">Deutsch</a> |
  2. <a href="?lang=ru_RU">Русский</a> |                     
  3. <a href="?lang=nb_NO">Bokmål</a> |
  4. <a href="?lang=nn_NO">Nynorsk</a></div></div>
I want the user to be able to make a language choice at any point while visiting my site. I read somewhere that storing the variable in a cookie (in a session) was a nice way to do this.

So all my pages should start with:

Expand|Select|Wrap|Line Numbers
  1. <?php session_start(); 
  2.     /* and then I tried this */
  3.     $lang = ( isset ($_GET['lang'] ) ? $_GET['lang'] : 'en_US' );
  4.     /* store the variable in a cookie so I can remeber it between the pages */   
  5.     $_SESSION['language'] = $lang;
  6.     setlocale(LC_ALL, $_SESSION['language']);
  7. ?>
But the problem is that when I browse to another page the URL will change to (i.e.) "www.mywebsite.com/two.php", and the $lang variable is then set to 'en_US'. Hmm.

I know there should be a simple solution to this. Just can't see it, because I'm a noob.

Any help is very much appreciated :D
Dec 23 '08 #3
5,821 Expert 4TB
Heya, hgeithus.

Check to make sure $_GET['lang'] is set before changing $_SESSION['language'].
Dec 23 '08 #4
6,050 Expert 4TB
I think you mean, check if $_SESSION['language'] is already set before changing it.

The way you're doing it now, the session will be updated on every page regardless of whether it's been set previously. So all we need to do is: check and see if the session is already set. We can do that using isset().

Expand|Select|Wrap|Line Numbers
  2. // The ! operator checks for FALSE instead of TRUE in the IF condition.
  3. // So it basically reads: if the session ISN'T set, then do the code.
  4. if( ! isset ( $_SESSION['language'] ) )
  5. {
  6.     $lang = ( isset ($_GET['lang'] ) ? $_GET['lang'] : 'en_US' );
  7.     /* store the variable in a cookie so I can remeber it between the pages */   
  8.     $_SESSION['language'] = $lang;
  9.     setlocale(LC_ALL, $_SESSION['language']);
  10. }
Dec 23 '08 #5
5,058 Expert 4TB
He probably meant; make sure the GET variable is set before setting the SESSION variable, or else the SESSION variable will always be reset to the default value if no GET variable is passed.

Your code, while it would successfully prevent the session from reverting to the default on every page, would also prevent the code from changing the language in the SESSION, which is what the OP was aiming for.

This would probably be closer:
Expand|Select|Wrap|Line Numbers
  1. if(isset($_GET['lang'])) {
  2.   $_SESSION['lang'] = $_GET['lang'];
  3. }
Then you could either add a elseif clause to set the default value for the SESSION variable if it isn't set, or simply have the code that uses the SESSION variable check that, rather than have it done on ever page.
Dec 23 '08 #6
I ended up doing this, and it works very well :)

Expand|Select|Wrap|Line Numbers
  1. session_start();
  2. if(isset($_GET['lang'])) 
  3. {
  4.     $_SESSION['lang'] = $_GET['lang'];
  5.     setlocale(LC_ALL, $_SESSION['lang']);
  6. }
  7. else
  8. {
  9. setlocale(LC_ALL, $_SESSION['lang']);
  10. }
Thanks alot. Not so difficult as it first seem ;)
Dec 23 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Wayne Wengert | last post: by
2 posts views Thread by Al Wilkerson | last post: by
2 posts views Thread by vinu thomas | last post: by
4 posts views Thread by Michel Lapointe | last post: by
4 posts views Thread by T Ralya | last post: by
2 posts views Thread by Ned Balzer | last post: by
3 posts views Thread by hon123456 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.