Saturday, 13 June 2015

Non secure to Secure AJAX call in PHP with Maintaining Session Variables

Want to Non secure to Secure AJAX call in PHP and maintain session?


What is .htaccess file?
".htaccess is a configuration file for use on web servers running the Apache Web Server software.

When a .htaccess file is placed in a directory which is in turn 'loaded via the Apache Web Server', then the .htaccess file is detected and executed by the Apache Web Server software.

These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer."

------------------------------------------------

Write below code in .htaccess file.


SetEnvIf Origin "http(s)?://(www\.)?(yourdomainname.com)$" AccessControlAllowOrigin=$0
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin

Header set Access-Control-Allow-Credentials true

-------------------------------------------------------------

In your jQuery AJAX call add below code,

xhrFields : { withCredentials : true }

Example,

$.ajax({ 
 type: 'POST',
url: http://www.yourdominname.com?test.php
data:  $('#formname').serialize(),
xhrFields : { withCredentials : true },
success: function(msg) 
}
});

1 comment:

  1. I can successfully call non secure to secure pages via AJAX with maintaining session variables also. Thanks for you help. Its working great.

    ReplyDelete