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 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常