Would be _extremely_ appreciative if someone could offer advice on this...
I am trying to create a very simple system to display different navigation menus based on preferred langauge. (I know there are a few modules for multilingual sites, but all are far more complex than what I need, and most seem fairly buggy. Also, I'm trying to improve my understanding of Drupal's inner workings with this project.)
The first part of the project requires setting an $interface_language variable. Most site visitors will not ever log in, so I thought I should set the variable based on URL (e.g. the variable will be set to "english" when a visitor goes to www.site.com/home/en) and use a session variable to store the value.
My first approach was to use sess_write and sess_read (code below). This code seemed to work at first. But it creates havoc for drupal's user authentication system.
With this code inserted in my theme's page.tpl.php I can login (and gain admin privilages) using completely random username/password combinations. In fact, I can gain admin privilages and 'log in' without even signing in... I just visit site content, and somehow I am automatically logged-in.
<?php
/* using URL parameter, set the variable $interface_language */
/* This variable will be used to switch navigation menus, header, footer etc */
if (arg(2) == 100) {sess_write('session_language', 'en');}
if (arg(2) == 101) {sess_write('session_language', 'ar');}
$interface_language = sess_read('session_language');
echo 'Interface language is <br />';
echo $interface_language;
?>
I assume I'm using sess_read and sess_write incorrectly... Can anyone explain if they are appropriate in this situation, and, if so, how to use them properly?
I've also tried writing directly to $_SESSION (code below). This seems to work, although I need a way to clear the $interface_language variable as soon as a user logs in. (Any advice on that?)
While the code below seems to work, I'm still worried I'm doing something that will interfere with the way Drupal uses $_SESSION. The discussion at http://drupal.org/node/32436#comment-59947 made me realize there's more going on than I realized.
Here's the code... Any thoughts on if this is ok? Is there a better (yet still simple) way to do it? Should I just be creating a 2nd session variable separate from Drupal's $_SESSION ?
<?php
/* using URL parameter, set the variable $interface_language */
/* This variable will be used to switch navigation menus, header, footer etc */
(arg(2) == 100) {$_SESSION['session_language'] = 'en';}
if (arg(2) == 101) {$_SESSION['session_language'] = 'ar';}
$interface_language = $_SESSION['session_language'];
echo 'Interface language is ';
echo $interface_language;
?>
Sorry for the long post, and THANK YOU for any help you can provide. I get the feeling that eventually (if I get it working) this should be implemented as a small module, but I still have to learn how to do that... looking at http://www-128.ibm.com/developerworks/ibm/library/i-osource5/ and drupaldocs for now.
Comments
Session based variable to control content
Issac77 - Did you ever figure out the best way to store and retreive a variable during a session to control access to content? I have the same need.
you should not use sess_write
you should not use sess_write and sess_read see http://drupal.org/node/825972
https://interface-network.com - Interface Network is an action and research technology governance agency.