简体   繁体   English

在PHP中重定向无cookie会话而不单击链接

[英]Redirecting cookieless sessions in PHP without clicking a link

I've been fighting with the cookieless sessions solution. 我一直在与无cookie会话解决方案作斗争 Of course cookieless sessions solution is amazing. 当然,无cookie会话解决方案令人惊叹。 I have a trouble in implementing it because I can't read the session information after redirecting to another page. 我在实现它时遇到了麻烦,因为在重定向到另一个页面后我无法读取会话信息。

Here's my test code in testcode.php 这是我在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);
}
?>

As you can see this code doesn't work. 如您所见,此代码不起作用。 but if i redirect to another page by clicking a link it works well. 但如果我通过点击一个链接重定向到另一个页面,它运作良好。 Here's the code in testcode.php : 这是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);
}
?>

How can I get this to work without clicking a link? 如何在不点击链接的情况下使其工作?

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

You have to have this on every single one of your PHP pages - you can't do it just within the session handling script. 您必须在每个PHP页面上都有这个 - 您不能只在会话处理脚本中执行此操作。 If it's not on when PHP generates a page, it won't insert the session ID into forms and urls on that page. 如果在PHP生成页面时它没有打开,它将不会将会话ID插入该页面上的表单和URL。 As such, it'd be better if you put this into your php.ini, or at least httpd.conf/.htaccess (as a php_value ) to make it a global option for all scripts. 因此,最好将它放入php.ini,或至少httpd.conf / .htaccess(作为php_value ),使其成为所有脚本的全局选项。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM