dousha4804 2017-09-06 13:49
浏览 39

PHP - 在子域上未定义会话

My login page: example.com/login (root/public_html/login)
My subdomain: api.example.com (root/api)

When I make a simple GET request using AngularJS $.http it fails to load the session variable. Directly loading the page works fine, but when using Angular it fails (Giving a CORS Error even though its on the same server and host just a different subdomain).

Is it possible making a js request to the absolute path (not relative path) causes a cors exception?

In my .htaccess file in public_html I have: php_value session.cookie_domain .example.com

The file im loading from $.http:

if(!isset($_SESSION)) session_start();
if(!empty($_SESSION['username'])) {
    echo "Working";
} else {
    echo "Not finding username";
    exit;
}

Login Code:

session_set_cookie_params(0, '/', '.example.com');
session_start();
$_SESSION['username'] = $query[0]['username'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['image'] = $query[0]['image'];
$_SESSION['role'] = $query[0]['role'];
$_SESSION['banned'] = $query[0]['banned'];
$_SESSION['id'] = $query[0]['id'];
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['useragent'] = $_SERVER['HTTP_USER_AGENT'];
setcookie(session_name(),session_id(),time()+604800,"/",".example.com");
header("Location: /"); exit;

EDIT: api.example.com's doc directory is /api and not the conventional /public_html/api would this make a difference?

Edit: Everything was solved once I moved the root/api docs folder to root/public_html/api and then used relative paths in the JS.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)