473,765 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting session variable with hyperlink

hgeithus
20 New Member
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 11969
Markus
6,050 Recognized Expert Expert
@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
hgeithus
20 New Member
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 Recognized Expert Expert
Heya, hgeithus.

Check to make sure $_GET['lang'] is set before changing $_SESSION['language'].
Dec 23 '08 #4
Markus
6,050 Recognized Expert Expert
@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 Recognized Expert Expert
@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
hgeithus
20 New Member
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
2796
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 adress, I use no-ip (www.no-ip.org) to have my own custom domain name (pvo.no-ip.org) My ISP blocks port 80, so website runs at port 4040 The service no-ip offers "Mask / Cloaking Options": every request to my domain is "wrapped in a frame", so end user can only see one URL in browser.
3
807
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 appears to be null. How can I pass a session variable to an ASP page? Wayne
2
1751
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 where I create/store the value <%Session="programming";%> <asp:HyperLink id="hlnkProgramming"Target="main" NavigateUrl="CategoryResults.aspx">Programming</asp:Hyperlink>
2
1416
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 to the site user has to enter user name and password. We using ASP page to validate user access right, if its a successful login we then create session variable to store necessary things. We use ASP.Net for all new development. These entire asp.net pages require those session variable. I would...
4
522
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
1951
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 that someone can at least recommend something to try to isolate the problem. I have a simple application that demonstrates my problem. Page 1, step1: SaveSessionVariableButton will create a string value, show it on screen, save it in a session variable and show the session ID on screen....
2
399
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 then redirects to another page. For the page that gets redirected to, I've swapped in a simple page for testing that reads the session variables and displays their values.
1
6508
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 /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
3
4765
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. < A href..C.asp>. The session variable cannot be passed to C.asp. The session variable is empty. What can I do to debug this problem? Thanks
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.