Jump to content

AJAX and session variables aren't working


mctrivia

Recommended Posts

I am using SAJAX version 0.12 on my web site http://matthewandzabrina.com

 

it works great for requesting data back from the server but it seems that anything I write to a session variable is ignored.

 

The trouble code is below:

function ajaxRequest($com) {
  //x_ajaxRequest(".picHTML",args1,arg2,...,returnFunction);

  //get list of arguments
  $vars=array_slice(func_get_args(),1);

  //get database
  global $conn;

  //Handle Picture List
  if ($com=='.picHTML') {
    //get vars
    if (isset($vars[0])) {
      $_SESSION['width']=$vars[0];
      $temp=array(
'showtag' => $vars[1],
        'showinmap' => $vars[2],
        'mapN' => $vars[3],
        'mapE' => $vars[4],
        'mapS' => $vars[5],
        'mapW' => $vars[6]
      );
      $_SESSION['index' . CAT_NUM]=serialize($temp);
    }
    $indexvars=unserialize($_SESSION['index' . CAT_NUM]);

 

$indexvars gets the values needed to sucessfully generate the result i want from the ajax command but if you reload the page it always reverts back to the original values and not the updated values.

Link to comment
Share on other sites

i found that i can read session variables in ajax requests but writing does not work so as a work around I used 1 session variable to hold an id number and a mySQL database entry to store the actual data.

 

 

//open database
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db(DB_DATA, $conn);

//setup default user vars:
$GLOBALS['userVars']=array(
'showtag' => 'Show All',
        'showinmap' => 0,
        'mapN' => 999,
        'mapE' => 999,
        'mapS' => -999,
        'mapW' => -999,
'width' => 800
);




if (!isset($_SESSION['id' . CAT_NUM])) {  


  //if first entering site generate a session id and save default values
  $ip=ip2long($_SERVER['REMOTE_ADDR']);
  $sql = "INSERT INTO `aigaznet_mazpic`.`session` (`id`, `ip`, `time`, `cat`, `vars`) VALUES (NULL, '" . $ip . "', NOW()," . CAT_NUM . ", '" . serialize($GLOBALS['userVars']) . "');"; 
  mysql_query($sql,$conn);
  $_SESSION['id' . CAT_NUM]=mysql_insert_id($conn);

} else {


  //if already enterd load saved user values
  $sql='SELECT `vars` FROM `session` WHERE `id`=' . $_SESSION['id' . CAT_NUM];
  $res = mysql_query($sql,$conn);
  if ($user = mysql_fetch_assoc($res)) {
    $GLOBALS['userVars']=unserialize($user['vars']);
  }


}

function updateUserVars() {
  $sql="UPDATE `session` SET `time`=NOW(), `vars` = '" . serialize($GLOBALS['userVars']) . "' WHERE `id`=" . $_SESSION['id' . CAT_NUM];
  global $conn;
  mysql_query($sql,$conn);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.