javascript - Second php file can't find Session started in first file -
i can't php $_session variable work way want to. i've got 2 files. first 1 setting variable:
<?php session_start(); header("access-control-allow-origin: *"); $_session['authenticated'] = "yes"; echo json_encode('authenticated successfully.'); ?>
and second 1 trying retrieve it:
<?php session_start(); header("access-control-allow-origin: *"); print '<pre>'; var_dump($_session['authenticated']); print '</pre>'; ?>
but second file prints null
when should print "yes"
, new error recorded in server's log:
[thu sep 22 12:52:47.763114 2016] [:error] [pid 26644] [client <ip_here>] php notice: undefined index: authenticated in <second_file> on line 5, referer: <client_url_here>
both files accessed ajax calls javascript.
the ajax code file 1 works following:
$.ajax({ url: corrdomain + '/auth.php', //path , name of file #1. it's correct , working. type: 'post', data: { //this part excluded in above php code simplification. it's guaranteed work. username: username, password: password, action: 'init' }, datatype: 'json', success: function(data){ //check if data contains right information, call function file #2. }, error: function(){ //something went wrong } });
when code gets thumbs php, user authorized following code run file #2:
$.ajax({ url: path + '/getproducts.php', //also correct , working. type: 'post', datatype: 'json', success: function(data){ products.push(data); orderproductids(); //update col 1 + 2; updateorders('reload'); //set interval of updating col 1. setinterval(function(){ updateorders('update'); }, 10000); }, error: function(){ alert('something went wrong.'); } });
update: added session_name();
first row in php files. session works if open each file in browser not if access them via ajax. problem still persists, maybe can me.
update #2: in chrome's web inspector can see both files returning session cookie ids, have different values. see screenshots.
update #3: i've researched , found php sessions isn't possible when using phonegap, though i'm requesting php file via jquery ajax. can confirm this?
make sure start session session_start();
on page makes ajax calls
edit
a not preferable maybe usefull method send session_id each ajax request use more token.
the first php-script:
session_start(); echo json_encode([ 'message' => 'session started', 'session_id' => session_id() ]); exit();
store received id in javascript-variable:
var session_id; $.ajax({ ... success: function(data) { session_id = data.session_id; } ... }
after send session_id along each ajax-request:
var data = {session_id: session_id, ... }; $.ajax({ type: "post", url: "server.php", data: data, success: function(data) { } });
instead of session_id can generate random token instead send along each request.
Comments
Post a Comment