doutale7115 2015-12-30 19:16
浏览 36
已采纳

会话变量值已更改并设置为零

I have three files: login.php, index.php, summary.php. All my session variables are set when user visits login.php. Below is my code snippet:

<?php
            if( isset($_POST['submit']) )
            {

                $username = $_POST['username'];
                $password = $_POST['password'];
                $_SESSION['username'] = $username;                          

                if(!($con = ssh2_connect('192.168.1.7', 22))){                      
                    echo "<script> alert(\"Fail: Unable to establish connection\"); </script>";
                } 
                else {                  
                    if(!ssh2_auth_password($con, $username , $password )) {
                            echo "<script> alert(\"Fail: Unable to authenticate\"); </script>";
                    } 
                    else {

                        //echo "<script> alert(\"Successfully logged in\"); </script>";
                        $_SESSION['con'] = $con;                            
                        header("Location: index.php");
                        //echo $_SESSION['con'];
                    }
                }
            }
?>

When I print my session variable $_SESSION['con'] in login.php, it prints Resource ID#1 as expected. Below is my index.php file which uses both the session variables.

<?php    
  session_start();
  echo $_SESSION['username']; 
  echo $_SESSION['con'];  
?>

When I print the session variables in index.php, $_SESSION['username'] gives me the correct output but $_SESSION['con'] prints 0 which was supposed to print Resource ID#1. That is, session variable con's value has been changed to zero.

  • 写回答

1条回答 默认 最新

  • doukuanghuan7582 2015-12-30 19:21
    关注

    $con is a resource, and you can't persist resources. You'll have to recreate the ssh connection every time if you need to use it.

    And even if it would be possible to persist resources in your session, the ssh connection will likely become invalid at some point in the future, and you'll have to re-create it again.

    The reason you can't persist resources is because most of them (if not all) map to host system's handles, and handles are valid only in the context of the process that created them. And since each PHP request is routed to a dedicated process (either newly created or extracted from a pool of processes), you can't use a resource in two separate requests as its likely they'll end up being served by different processes.

    There's a very good article on the PHP manual website talking about persistent database connections. It describes very well the different setups in which PHP code ends up executing, and why persistent connections have advantages/disadvantages.

    I also found this SO question about making persistent ssh connections. Beside the solutions enumerated in that question, another possible one would be for you to write a daemon that keeps the ssh connection open, and reconnects in case the connection got closed. You'd then connect from your PHP script to that daemon. Note that you'll still won't save the connection to the daemon and will have to recreate it every time you need it; this is just a solution to avoid saving the username and password into the session.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度