Use secure POST session requests:
Inside the webpage (e.g. index.php) we need to save sessionid
<?php <head> ... <script type="text/javascript"> sid = '<?php echo session_id(); ?>'; </script> <script type="text/javascript" src="ajaxrequest.js"></script> ... </head>
Ajax requests (ajaxrequest.js)
/* simple getAjax function * @param $url request url * @param $param parameter (dont use ?) * @param callback function on success */ var spinnerid = '#spinner'; // Spinner as long ajax requests running $(document).ajaxStart(function() { $(spinnerid).show(); }); $(document).ajaxStop(function() { $(spinnerid).hide(); }); function getAjax( url, param, callback ) { var data = null; url += "?sid=" + sid + "&" + param; $.ajax({ url: url, method: "POST", // uncomment to use GET, POST is secured by session cache: false, async: true, success : function(data){ callback(data); }, } getAjax( 'http://domain.com/', 'data=foo', function( data ) { // do stuf with data var jsonobj = eval("(" + data + ")"); var data = jsonobj[0][ 'data' ]; });
The responsible side of php:
if( isset( $_GET['sid'] ) ) $client_sid = $_GET['sid']; if( session_id() == null ) session_start(); if( session_id() != $client_sid ) { // noID or wrongID, redirect to mainindex ignore_user_abort(true); header( "HTTP/1.1 403 Forbidden" ); header("Connection: close", true); exit; } else { // get data if( isset( $_GET['data'] ) ) { $data = $_GET['data']; } else if( isset( $_POST['data'] ) ) { $data = $_POST['data']; } else { $data = null; } // do stuff with data // return data as json $resp[0]['data'] = $data; print_r( json_encode( $resp ) ); }
v4d Feb 12 '16 at 20:49 2016-02-12 20:49
source share