0

I'm trying to make a custom form in Drupal. This form call a web service and will receive some Json. in this Json response, I have three information (or an error) : 1. User name 2. User language 3. User custom Token (special for another webapp)

Those three info must be placed in session (or anything else) and will be shown in some webpages.

In example, on every page, It will be written "Hello Mr John", and on some web pages I want to show a icon + link with the custom Token to access to another web application, ...

We already have a lots of users, and we don't want to have all the users in the drupal DB.

How can I Do? I'm using Drupal 7

Thanks in advance

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
clement
  • 4,204
  • 10
  • 65
  • 133

1 Answers1

0

I wrote this function to deal with session in Drupal

function _mySite_session($key, $value = NULL)
{
    static $storage;
    if ($value)
    {
        $storage[$key] = $value ;
        $_SESSION['mykey'][$key] = $value ; // I use 'mykey' as a session key because in case some other module uses 'type' in $_SESSION. So try to make it unique.
    }
    elseif (empty($storage[$key]) && isset($_SESSION['mykey'][$key]))
    {
        $storage[$key] = $_SESSION['mykey'][$key];
    }
    return $storage[$key];
}

To set a session variable:

_mySite_session("USERNAME", "Mr. John");

And to retrieve the value of the session variable:

$username = _mySite_session("USERNAME");
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
  • :ok thanks but where did you put this function? In a special part of Drupal? And where did you get the session variable? There is no error or security trouble by using that? Where can I wrote the if(isset($username))? in a special file? – clement Apr 04 '14 at 12:45
  • It's up to you where to put the function. Use the function in your custom module or your `template.php` file. And No, there's no security issues using that. – Muhammad Reda Apr 04 '14 at 12:50
  • Setting and retrieving the session variable can go anywhere you like. It depends on your code logic. – Muhammad Reda Apr 04 '14 at 12:52
  • Ok thanks. You think that it is possible to make a ajax call, returning Json values and set them to session? – clement Apr 04 '14 at 12:54
  • Yes, it's sure possible. It's also depends on your code logic. – Muhammad Reda Apr 04 '14 at 12:56
  • how Can I make Session after the AJAX Call? We cannot set ajax in JS/JQUERY, it mist e in PHP, so how can you add session in JS? – clement Apr 04 '14 at 13:55
  • Assuming you are retrieving the JSON response in variable `$json`. `$json = json_decode($json); _mySite_session("USERNAME", $json->users->username);` Kindly note that it depends on the structure of the response JSON string. Check [json_decode documentation](http://php.net/manual/de/function.json-decode.php) and [this discussion](http://stackoverflow.com/q/17995877) – Muhammad Reda Apr 04 '14 at 17:43
  • =@Muhammad ok thanks but I think the best way os to provide "login" and "password" inputs and when user press the 'login' button, there will be a ajax call that get the response in the JS, isn't it? so the JS can't make PHP and sessions... – clement Apr 04 '14 at 19:10
  • As far as I know; No, JS can not make PHP sessions. – Muhammad Reda Apr 08 '14 at 08:32
  • thanks so how can I make this as simple as possible? – clement Apr 08 '14 at 08:37
  • I must make a form with login:password. This form will make a Ajax Call or Rest or ... to a webservice that will return datas to store in session. How make it possible in drupal, this is my initial question and it's left unanswered... I have to set it in session allright but HOW :-) – clement Apr 08 '14 at 09:09