无法访问2个文件之间的会话变量

I'm unable to access my session variables even with session_start() on both files.

File1.php

<?php
  session_start();

  ...

  if($responseCode == 1) {
    $_SESSION['card_id']    = $_POST['card_id'];
    $_SESSION['password']   = $_POST['password'];
    print '<script type="text/javascript">'; 
    print 'window.location = "http://domain.com/File2.php";';
    print '</script>';
  }
?>

File2.php

<?php
  session_start();

  $account = getAccount();

  echo "document.write('$account')";

  function getAccount() {
    $card_id = $_SESSION['card_id'];
    $string = "card = " . $card_id ;
    return $string;
  }
?>

File1.php does a curl request and, if it succeeds, it redirects to File2.php. However, I only see card : without the card_id I entered.

It does not work because the www is considered a subdomain, and hence the session does not persist. You can fix this in 2 ways -

  1. Which I consider is the better way, to use htaccess to redirect all non www urls to www urls, which is better for SEO

  2. Check the PHP manual on how to set session across multiple subdomains.

This post explains how www is considered a subdomain, if you are curious.