0

私はcookielessセッションソリューションと戦ってきました。もちろん、cookielessセッションソリューションは素晴らしいです。別のページにリダイレクトした後、セッション情報を読み取ることができないため、実装に問題があります。

これがtestcode.phpの私のテストコードです

<?php
ini_set('session.use_trans_sid', '1');

session_start();

if (isset($_GET['pagecode'])) {
    session_id($_GET['pagecode']);
print_r($_SESSION); // **cannot read session information here**
exit();
}

if (isset($_SESSION['cookieconfirmed']) && $_SESSION['cookieconfirmed'] == 1) {

} else {
/** Checks if the user's browser is cookie-enabled **/
    if (isset($_GET['redirected'])) {  // if the page has gotten redirected
        $_SESSION['cookieconfirmed'] = 1;  // confirmed the cookie-disability
        if (isset($_COOKIE['testcookie'])) {
            header ('location: testcode.php');
        } else {
           header('location: testcode.php?pagecode=' . session_id());
        }
    } else {
       setcookie('testcookie', 'OK');  //sets a test cookie.
       header('location: testcode.php?redirected=1'); // redirects the page to check     cookie-disability
    }

    exit(0);
}
?>

ご覧のとおり、このコードは機能しません。しかし、リンクをクリックして別のページにリダイレクトすると、うまく機能します。testcode.phpのコードは次のとおりです。

<?php
ini_set('session.use_trans_sid', '1');

session_start();

if (isset($_GET['pagecode'])) {
    session_id($_GET['pagecode']);
print_r($_SESSION); // **able to read session information here**
exit();
}

if (isset($_SESSION['cookieconfirmed']) && $_SESSION['cookieconfirmed'] == 1) {

} else {
/** Checks if the user's browser is cookie-enabled **/
    if (isset($_GET['redirected'])) {  // if the page has gotten redirected
        $_SESSION['cookieconfirmed'] = 1;  // confirmed the cookie-disability
        if (isset($_COOKIE['testcookie'])) {
            header ('location: testcode.php');
        } else {
           echo '<a href="testcode.php?pagecode=' . session_id() . '">Click here to continue</a>';
        }
    } else {
       setcookie('testcookie', 'OK');  //sets a test cookie.
       header('location: testcode.php?redirected=1'); // redirects the page to check     cookie-disability
    }

    exit(0);
}
?>

リンクをクリックせずにこれを機能させるにはどうすればよいですか?

4

2 に答える 2

2
ini_set('session.use_trans_sid', '1');

これは、PHP ページのすべてのページで実行する必要があります。セッション処理スクリプト内だけで実行することはできません。PHP がページを生成するときにオンになっていない場合、そのページのフォームと URL にセッション ID が挿入されません。したがって、これを php.ini に入れるか、少なくとも httpd.conf/.htaccess ( としてphp_value) に入れて、すべてのスクリプトのグローバル オプションにする方がよいでしょう。

于 2011-02-28T19:34:18.787 に答える
0
PHP function for this is :


function append_sid($link) {
    if(session_id() !== NULL && !isset($_COOKIE['PHPSESSID'])) {
        if(strpos($link, "?") === FALSE) {
            return $link . "?PHPSESSID=" . session_id();
        } else {
            return $link . "&PHPSESSID=" . session_id();
        }
    } else {
        return $link;
    }
}


Javascript Function for this is:


function append_sid(link) {
    <?php if(session_id() !== NULL && !isset($_COOKIE['PHPSESSID'])) { ?>
        var session_id = '<?php echo session_id ?>';
        if(link.indexOf('?') == -1) {
            return link + '?PHPSESSID=' + session_id;
        } else {
            return link + '&PHPSESSID=' + session_id;
     }
    <?php } else { ?>
        return link;
    <?php } ?>
}


A caveat – passing session id by URL requires the session.session.use_trans_sid tio be set to 1. 

php_value session.use_trans_sid = 1 in the .htaccess file. You can also try the function:

ini_set('session.use_trans_sid', '1')
于 2014-01-22T11:57:05.053 に答える