好的,我有domain.com
然后我使用javascript运行xhr请求api.domain.com
以前我将api.domain.com
作为domain.com/api
但我想将其移至子域名和登录工作正常,直到我将其移至api.domain.com
我正在测试一个登录脚本并尝试保持会话生效但每次运行它都会清除$ _SESSION
db_connect.php
include_once("config.php");
ob_start();
session_start();
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
auth.php
<?php
require($_SERVER['DOCUMENT_ROOT'].'/db_connect.php');
if (!$db) {
die('Could not connect: ' . mysql_error());
}
$method = $_SERVER['REQUEST_METHOD'];
if ( isset($_GET['id']) ){
$id = $_GET['id'];
} else {
$id = 'all';
}
switch (strtoupper($method)) {
case "GET":
if ($_SESSION['auth']) {
$check = true;
} else {
$check = false;
}
$arr = json_encode(array('result'=>$check));
echo $arr;
break;
default:
echo "Streets closed pizza boy!";
}
signin.php
<?php
require($_SERVER['DOCUMENT_ROOT'].'/db_connect.php');
if (!$db) {
die('Could not connect: ' . mysql_error());
}
$method = $_SERVER['REQUEST_METHOD'];
if ( isset($_GET['id']) ){
$id = $_GET['id'];
} else {
$id = 'all';
}
switch (strtoupper($method)) {
case "POST":
$postdata = json_decode(file_get_contents("php://input"));
$src = (array)$postdata->user;
$password = hash( 'sha512', $src['password']);
$q = $db->query("SELECT *
FROM users u
WHERE u.email = '".$src['email']."'
AND u.password = '".$password."'");
if($q->num_rows > 0){
$check = true;
$_SESSION['auth'] = 1;
$maps = array();
while($row = mysqli_fetch_array($q)) {
$product = array(
'auth' => 1,
'id' => $row['id'],
'name' => $row['name'],
'email' => $row['email'],
'access' => $row['access']
);
array_push($maps, $product);
}
//$_SESSION['company_id'] = $product['company_id'];
}else{
$check = false;
}
$_SESSION['id'] = $product['id'];
$_SESSION['email'] = $product['email'];
setcookie("username", $productx§['email'], time()+(84600*30));
$arr = json_encode(array('result'=>$check, 'user'=>$maps));
echo $arr;
break;
default:
echo "Your favorite color is neither red, blue, or green!";
}
我尝试将 db_connect.php 设置为
<?php
include_once("config.php");
ob_start();
session_set_cookie_params(0, '/', '.domain.com');
session_start();
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
但这没有做任何事情,会话变量也会丢失。
PHP文件也通过AJAX调用
所有页面是否应该将angularjs dom连接到数据库?
答案 0 :(得分:1)
对于使用跨子域会话,在所有子域项目中,您必须使用以下代码:
session_name('SessionName');
session_set_cookie_params(
1800,
ini_get('session.cookie_path'),
'.example.com'
);
session_start();
重要:仅当一个服务器上的所有子域都有效时(导致会话存储在一个tmp目录中)。如果要在不同的服务器上使用类似的会话,请在memcached中使用SAN存储或存储会话等。
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://122.122.122.122:11211');
session_name('SessionName');
session_set_cookie_params(
1800,
ini_get('session.cookie_path'),
'.exmaple.com'
);
session_start();
答案 1 :(得分:0)
尝试设置会话名称:
$session_name = session_name("my_session");
然后,将会话cookie设置为跨越所有子域:
session_set_cookie_params(0, "/", ".example.com");
然后执行session_start()
- 现在应该可以了。
答案 2 :(得分:0)
.htaccess
# CORS Headers (add this)
<ifModule mod_headers.c>
Header add Access-Control-Allow-Origin "http://domain.com"
## Post the domain that will be doing the xhr requests
Header add Access-Control-Allow-Credentials: "true"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
</ifModule>
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
<强> domain.com 强>
在主网站的标题中发布以下内容
ini_set('session.cookie_domain', '.domain.com' );
session_start();
XHR请求
现在我们需要将凭据从 domain.com 发布到 api.domain.com 我使用AngularJS
$http({
method: 'GET',
url: '//api.domain.com/auth/',
xhrFields: {
withCredentials: true
},
crossDomain: true
}).success....
同时更改您的配置以允许使用凭据发送
.config(function ($routeProvider, $httpProvider) {
$httpProvider.defaults.withCredentials = true;
//rest of route code
答案 3 :(得分:0)
这对我有用:
ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
session_start();
起初,它不起作用,我不得不删除所有cookie,最后它按预期工作。