1
votes

I'm new to Drupal and need insigh on the workings of it's session management, my goal is to send variables over different pages.

This first piece of code gets a value from the frontpage then redirects to the next page.

<?php
session_start();
$_SESSION['beer'] = $_GET['g'];
header("Location: http://127.0.0.1/drupal/Page2.php");
exit();
?>

Next page is this

<?php
session_start();
echo $_SESSION['beer'];
?>

Page2.php echos the value of $_SESSION['beer'] as it should.

Now when i copy this same code as a page inside Drupal 7 content modules.

Change header location in php file

<?php
session_start();
$_SESSION['beer'] = $_GET['g'];
header("Location: http://127.0.0.1/drupal/node/1");
exit();
?>

Add code to Drupal page

<?php
/*session_start();*/ / Already initialised in drupal
echo $_SESSION['beer'];
?>

But the results of $_SESSION['beer'] comes back undifined.

Notice: Undefined index: beer in eval()

Why?

2
1.You don't need to call session_start, Drupal does this for you. 2. use the drupal_goto function to redirect to another page. How are you adding your drupal page? With a hook_menu I hope. Adding all the code for your module will help us help you. - 2pha
There are Drupal functions for all the functions you used in your code. Where the hell did you put this code? You seems very new to Drupal indeed. If I were you I'd begin by reading the official documentation before trying anything else because you did wrong. :( - Djouuuuh

2 Answers

1
votes

You use variable_get() and variable_set(). Its covered over at DrupalAnswers.

0
votes

I finally found out what I was doing wrong. Being neophyte I didn't include the bootstrap to my page Page2.php

Now here is the new code.

Page2.php :

<?php
define('DRUPAL_ROOT', getcwd());
require_once
DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
variable_set('beer', $_GET['g']);
header("Location:/drupal/node/1");
exit();
 ?>

And the content page

<?php
echo variable_get('beer');
?>

As suggested by nathanlctnstn, instead of $_SESSION I used variable_set to store data.

And now the results :

Nuts!

Thanks!