Im using a jQuery Vector Map Library with name jqvmap. Anyone knows a way to Set a Session in Javascript instead of setting a 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:
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. 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. This can be done using ajax/php like below. Create two PHP pages:
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:
<?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:
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
:
setSession('AZ');
then, You can reuse this session id later from Your html page to recall the session variables stored server-side:
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.
Hope this helps.