tags:

views:

126

answers:

3

My first stab at this so please don't laugh! I've created a session variable to allow users to switch between UK and US content on the same site (UK default).

<?php
session_start();
$_SESSION['territory'] = 'UK';
if (isset($_SESSION['territory'])){
    echo 'Session is set to '.$_SESSION['territory'];
}
else{
    echo 'Session not set yet';
}
?>

All good so far. I now need a couple of links to set this variable when users click either UK or US. All I can Google is setting variables via forms, with nothing helpful about setting via a plain old href. Can anybody steer me in the right direction? Much appreciated.

+2  A: 

You can append ?lang=UK to the url, then the $_GET['lang'] variable will be available in your code. For example:

switch($_GET['lang']) {
 case 'UK': case 'US':
   $_SESSION['territory'] = $_GET['lang'];
   break;
 default:
   $_SESSION['territory'] = 'UK';
}
baloo
I was just writing something like that, ]1
Adirael
A: 

Firstly, you can do this with just a cookie (no session necessary).

That being said, just create some links to a uri, say: /set_territory.php?cc=UK and so forth.

set_territory.php can then set the territory via $_GET['cc'] and redirect the user to the appropriate content.

webbiedave
Thanks everyone, although I want to avoid using a URI if poss. Is a cookie a better option?
DK
+1  A: 

if you want to set session variable with help of click of link. You will have to use ajax, when you click on a link you can call a javascript function this function can then in turn make a ajax hit to a php file and then this php file can set the session variable.

sushil bharwani
To clarify, this can work because the Ajax call will "trigger" the PHP script that sets the session, without changing the page that the user is on. Of course, you'd probably want to refresh the page afterwards to change the content.
erjiang