我想为运行for循环获得的每个ip地址执行 tel2.php 。我的tel2.php文件有一个session_start()。因此,每次for循环脚本运行时,我都会收到错误消息,说明会话已经开始。请指导我如何解决这个问题。谢谢。
<?php
session_start();
include("check.php");
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'searchrouters';
//connect with the database
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
$checkbox1=$_POST['IP'];
{
for($i=0; $i<sizeof($checkbox1);$i++){
$query="INSERT INTO checked (name) VALUES ('".$checkbox1[$i]."')";
$conn->query($query) or die(mysql_error());
$_SESSION['ipadd'] = $checkbox1[$i];
include('tel2.php');
}
}
?>
Tel2.php
<?php
session_start();
require_once "PHPTelnet.php";
$telnet = new PHPTelnet();
$telnet->show_connect_error=0;
// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$ipadd = $_SESSION['ipadd'];
echo $ipadd;
$result = $telnet->Connect("$ipadd",'nib2p1','nib2p1');
switch ($result) {
case 0:
$telnet->DoCommand('term length 0', $result);
echo $result;
$telnet->DoCommand('show clock', $result);
// NOTE: $result may contain newlines
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
break;
case 1:
echo '[PHP Telnet] Connect failed: Unable to open network connection';
break;
case 2:
echo '[PHP Telnet] Connect failed: Unknown host';
break;
case 3:
echo '[PHP Telnet] Connect failed: Login failed';
break;
case 4:
echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet';
break;
}
?>
答案 0 :(得分:0)
确实像@yafafater在评论中所说的那样
从tel2删除会话启动。一次就够了
更具体地说,我会说以下内容:
您提供的两个文件是链接的,一个包含在另一个中。所以基本上你必须将它视为一个文件。此外,您无法在两者之间关闭时启动两次会话,因此只需要一个session_start()
。
在PHP将tel2.php文件包含在第一个文件中之后,为了帮助您理解代码看起来如此:
<?php
// start session
session_start();
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'searchrouters';
//connect with the database
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
$checkbox1=$_POST['IP'];
{
for($i=0; $i<sizeof($checkbox1);$i++){
$query="INSERT INTO checked (name) VALUES ('".$checkbox1[$i]."')";
$conn->query($query) or die(mysql_error());
$_SESSION['ipadd'] = $checkbox1[$i];
// include the file tel2.php
// session can't be started again, it's already on
require_once "PHPTelnet.php";
$telnet = new PHPTelnet();
$telnet->show_connect_error=0;
$ipadd = $_SESSION['ipadd'];
echo $ipadd;
$result = $telnet->Connect("$ipadd",'nib2p1','nib2p1');
switch ($result) {
case 0:
case 1:
case 2:
case 3:
case 4:
}
}
}
(实际上你需要再次启动会话的唯一时间是你在Tel2.php上重定向用户。在这种情况下,它会被视为一个单独的文件。)