How to make PHP work with subdomain and AJAX?

I work in a PHP project that uses subdomains, sessions, and Ajax. But unfortunately, I can’t get it to work! I will try to explain:

Suppose I am in this domain: app.mysite.com/index.php

In this domain, I have a form that executes an Ajax request mysite.com/functions/execute.php (without any subdomain)

In the first line of execute.php , I have require_once , which contains the helper.php file . In this file I set:

ini_set('session.cookie_domain',  '.mysite.com');
session_set_cookie_params(0, '/', '.mysite.com');
session_start();

All of the listed PHP files also contain helper.php.

If for example I run:

echo $_SESSION["myValue"];

app.mysite.com/index.php , auth.mysite.com, : ". execute.php Ajax, undefined index!

?

+4
3

, . Ajax Post , :

$.ajax({
    method   : "POST",
    url      : "https://example.com/functions/execute.php", 
    data     : myData,
    xhrFields: { 
        withCredentials: true
    }
}).done(function(result) {
    alert("success"));
});

execute.php :

ini_set('session.cookie_domain',  '.example.com');
session_set_cookie_params(0, '/', '.example.com');
session_start();
header('Access-Control-Allow-Credentials: true');

, example.php:

header('Access-Control-Allow-Origin: http://app.example.com');
+1

-, cookie/session . , cookie, . , Google. PHP, cookie 3 . , , HTML, PHP, cookie 2 . :

<html>
   <head></head>
   <body>
      <p>Please wait.....</p>
      <img src="http://domain2.com/setcookie.php?theme=whateveryourthemehere" />
      <img src="http://domain3.com/setcookie.php?theme=whateveryourthemehere" />
   </body>
</html>

img , , . onload body. , cookie 2. Onload Callback:

<head>
   <script>
   function loadComplete(){
      window.location="http://domain1.com";//URL of domain1
   }
   </script>
</head>
<body onload="loadComplete()">

cookie , PHP (setcookie.php ):

<?php
if(isset($_GET['theme'])){
   setcookie("theme", $_GET['theme'], time()+3600);
}
?>

cookie :) - , cookie:)

, tweek . , ,

,

+1

Source: https://habr.com/ru/post/1656040/


All Articles