dro62273 2010-07-19 23:05
浏览 82
已采纳

Ajax和PHP - 找不到会话cookie?

i'm having a little problem. I'm working on a wordpress template which lists all files on my ftp. Therefore i ask for the the password and pass it straight along to the ftp_connect.

So the structure looks like this. I'm asking for the password, and if entered (and send) i'll include a file called ftp_include.php which lists all my files. That's working fine so far.

The only problem I have is that I reload/refresh this inlcude with Ajax (jQuery). However only the include. And everyime i refresh this include has to connect to the server again (with the password entered at the beginning).

A few guys already told me here that I need to work with SESSIONS in PHP, which store my password and inside my include i'm retrieving it again.

Somehow i can't figure out why my SESSION cookie wont work. I guess it's getting stored properly with this:

session_start();

                session_start();
                if(!isset($_SESSION['ftp-password']) ) {
                    $_SESSION['ftp-password'] = $_POST['password'];
                }
                var_dump(ini_get_all('session')); //shows both times 
                //this result:http://cl.ly/1hzA -> 
                //so it seems it doesn't get stored properly, does it? 

i think so because i'm retrieving the password inside the include with this lines:

if(isset($_SESSION['ftp-password']) ) {
    $ftp_user_pass = $_SESSION['ftp-password'];
    echo "Password: " . $ftp_user_pass;
} else {
    print "can't find cookie!";
}

and it even CONNECTS to the server for the first time. However as soon as i refresh the include with Ajax somehoe it always says "can't find cookie". Any idea why that happens. I even tried with set_cookie() but the same result.

thank you for your tips

  • 写回答

1条回答 默认 最新

  • douzhao9608 2010-07-20 03:28
    关注

    It looks like WP does strange things with the sessions. Like it unsets them and/or overwrites them. Here is a page with some discussion on it, such as putting session_start() before get_header() (which was probably causing your headers already sent error).

    Why don't you just store the password in a variable in php and pass that to jquery? E.g:

    <?php
       $ftp_pass = $_GET['ftp_pass'];//or however you get the password
    ?>
    
    <script>
    window.setInterval('yourfunction()', 1000);
    
    function yourfunction() { 
        alert('<?=$ftp_pass?>');//replace with your jQuery refresh code
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料