如何序列化phpseclib? [英] how to serialize phpseclib?
问题描述
我一直在尝试序列化 phpseclib 连接,因为我正在从 ajax 加载一些 php 页面,我不想一次又一次地重新连接.
I have been trying to serialize phpseclib connection since I am loading some php pages from ajax and I dont want to reconnect again and again.
第 1 页 - 首先定义 SSH2
require("Net/SSH2.php");
session_start();
$ssh = new Net_SSH2 ("localhost");
if ($ssh->login($username, $pass)) {
$_SESSION['obj'] = serialize($ssh);
}else{
echo "Invalid Login !!!";
}
第 2 页 - 通过反序列化获取存储的会话
session_start();
require("Net/SSH2.php");
$ssh = unserialize($_SESSION['obj']);
echo $ssh->exec('ls');
我也想将登录信息放在 $_SESSION['obj']
中,所以每当我在那个 php 页面上时,我只是 $ssh = unserialize($_SESSION['obj']);
然后 $ssh->exec('pwd')
..
I want to put Login information in $_SESSION['obj']
too so Whenever I am on that php page i just $ssh = unserialize($_SESSION['obj']);
and then $ssh->exec('pwd')
..
是否有解决办法,或者每当我在那个 PHP 页面上时,我都必须始终连接/登录到 Net/SSH2.php ???
Is there work around or will I have to always connect/login to Net/SSH2.php whenever I am on that PHP page ???
推荐答案
拥有持久连接的唯一方法是实际让脚本不断运行以保持连接打开.这意味着您有一个带有永无止境的 while (true) { ... }
循环的脚本,它打开一次连接并保持它.然后,您需要找到一种与此脚本通信的方法,以使其执行命令.AMQP、ØMQ、Gearman、套接字和类似技术可以用于此目的.
The only way to have a persistent connection is to actually have a script constantly running which is keeping the connection open. Which means you have one script with a never ending while (true) { ... }
loop which opens a connection once and keeps it. You then need to find a way to communicate with this script to make it execute commands. AMQP, ØMQ, Gearman, sockets and similar techniques can be employed for that.
这篇关于如何序列化phpseclib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!