472,348 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

Setting session variable with hyperlink

hgeithus
Hi.

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';
  2.  
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 11797
Markus
6,050 Expert 4TB
@hgeithus
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'; ?>
  2.  
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>
  3.  
This then passes the language to the URL which you can later obtain using GET. Consider:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  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' );
  8.  
  9. echo $lang;
  10.  
  11. ?>
  12.  
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
pbmods
5,821 Expert 4TB
Heya, hgeithus.

Check to make sure $_GET['lang'] is set before changing $_SESSION['language'].
Dec 23 '08 #4
Markus
6,050 Expert 4TB
@pbmods
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
  1.  
  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. }
  11.  
  12.  
Dec 23 '08 #5
Atli
5,058 Expert 4TB
@Markus
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

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

Similar topics

1
by: Bart Plessers \(artabel\) | last post by:
Hello, Currently developping a site, where I use some session variables. The site is located at my home computer. Because I have a dynamic IP...
3
by: Wayne Wengert | last post by:
I have an application where a session variable is set in an ASPX page, The process calls an ASP page and when in that page the session variable...
2
by: Al Wilkerson | last post by:
Hey guys, I'm having problems with creating/storing a value in a session variable for later retrieving on another page. Here is the aspx page...
2
by: vinu thomas | last post by:
Friends, Is it possible to access a ASP Session variable in ASP.net Page. The project i am working on uses both ASP and ASP.Net. To gain access in...
4
by: Michel Lapointe | last post by:
Hello, Does anyone know if it is possible to set a session variable when a user click on a HyperLink? Thank ML
4
by: T Ralya | last post by:
I am told that ASP.NET controls the session ID and session variables, but that does not fit my symptoms. I am posting here as directed. I'm hoping...
2
by: Ned Balzer | last post by:
Hi, Apologies if this is a newbie question, I haven't found the answer in any faqs. I have an asp.net 2.0 page that sets session variables and...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to...
3
by: hon123456 | last post by:
Dear all, I have a session variable session("loginid) which can be passed from A.asp to B.asp. Then In B.asp I have a hyperlink to C.asp e.g. <...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.