简体   繁体   English

如何使用Javascript和Jquery Vector Map设置会话

[英]How can I set a Session using Javascript with Jquery Vector Map

Im using a jQuery Vector Map Library with name jqvmap . 我正在使用名称为jqvmap的jQuery矢量地图库 Anyone knows a way to Set a Session in Javascript instead of setting a Cookie?: 有谁知道用Java设置会话而不是设置Cookie的方法?:

My code: 我的代码:

function getCountryName(code) {
    var path = JQVMap.maps["world_en"].paths[code];
    return path && path.name;
}
var fromCountryCode = getCookie(cookieName) || "";
worldMap = jQuery('#vmap').vectorMap({
    map: "world_en",
    backgroundColor: '#FFCC28',
    borderColor: '#818181',
    scaleColors: ['#222222', '#ffffff'],
    borderOpacity: 0.25,
    color: '#2F3132',
    hoverOpacity: 0.8,
    multiSelectRegion: true,
    selectedColor: '#FBB24B',
    selectedRegions: '{{$flag}}',
    //selectedRegions: [fromCountryCode],
    enableZoom: true,
    showTooltip: true,
    onRegionClick: function(e, code, name) {
        code == "AE" ||
        code == "AF" ||
        code == "AG" ||
        code == "AL" ||
        code == "AM" ||
        code == "AO" ||
        code == "AR" ||
        code == "AT" ||
        code == "AU" ||
        etc...
        code == "ZW"
        $("#message").text("Your Country: " + name);
        setCookie(cookieName, code, 600); // minutes
        window.location.replace("https://example.com/");
    }
});

My idea is to Set a Session in Javascript instead of setting a Cookie by replacing the following part of code: 我的想法是用Java语言设置会话,而不是通过替换以下代码部分来设置Cookie:

setCookie(cookieName, code, 600);

replaced by 取而代之

session(['name' => $code]);

I read SO and it seems that sessions cannot be altered from client side, but probably someone knows a workaround using AJAX. 我读了SO,看来会话不能从客户端更改,但是可能有人知道使用AJAX的解决方法。 brgds. brgds。

Well, TBH I haven't fully understand the whole workflow of Your application, but if You need to create a PHP cookie-less session , You will need to pass back & forth the session id by yourself. 好吧,TBH我还没有完全了解您的应用程序的整个工作流程,但是如果您需要创建一个无PHP cookie的会话 ,则需要自己来回传递会话ID。 This can be done using ajax/php like below. 可以使用ajax / php如下所示。 Create two PHP pages: 创建两个PHP页面:

set_session.php: set_session.php:

<?php 
  ini_set("session.use_cookies", 0);
  ini_set("session.use_only_cookies", 0);
  session_start();

  if(isset($_POST['code'])){
    $code = filter_var($_POST['code'], FILTER_SANITIZE_STRING);
  }

  $_SESSION['name'] = $code;
  $_SESSION['value'] = 'https://example.com';

  header('Content-Type: application/json');
  $response = [];
  $response['sessionid'] = session_id();
  echo json_encode($response);
?>

get_session.php: get_session.php:

<?php 
  ini_set("session.use_cookies", 0);
  ini_set("session.use_only_cookies", 0);

  if(isset($_POST['sessionid'])){
    $sessionid = filter_var($_POST['sessionid'], FILTER_SANITIZE_STRING);
  }

  session_id($sessionid);
  session_start();
  header('Content-Type: application/json');
  $response = [];
  $response['name'] = $_SESSION['name'];
  $response['value'] = $_SESSION['value'];
  echo json_encode($response);
?>

JavaScript: JavaScript的:

var mySessionId = "";

function setSession (code){
   $.ajax({url: 'set_session.php'
    ,data: {code: code}
    ,type: "post"            
    ,dataType: "json"
    ,success: function (result) {
      mySessionId = result.sessionid;
    }
  });
}

function getSession (){
  $.ajax({url: 'get_session.php'
    ,data: {sessionid: mySessionId}
    ,type: "post"              
    ,dataType: "json"
    ,success: function (result) {
      // result will be: {name: AZ, value: https://example.com}
    }
  });
}

Fisrt, You should get a session id into mySessionId : 首先,您应该将会话ID放入mySessionId

setSession('AZ');

then, You can reuse this session id later from Your html page to recall the session variables stored server-side: 然后,您可以稍后在HTML页面中重用此会话ID,以调用存储在服务器端的会话变量:

getSession();

Moreover, depending from Your needs, You can also use an http header to redirect the user to the new link from directly inside the get_session.php page. 此外,根据您的需要,您还可以使用http标头将用户直接从get_session.php页面内重定向到新链接。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM