douqie6454 2016-07-18 08:28
浏览 61
已采纳

从for循环中多次运行php脚本

I want to execute tel2.php for each ip address I get from running the for loop. My tel2.php file has a session_start(). Hence, everytime the for loop script runs, I get the error saying session has already started. Please guide me how to fix this. Thank you.

<?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; 
}
?> 
  • 写回答

1条回答 默认 最新

  • dongque1958 2016-07-18 08:46
    关注

    Indeed like @yafater said in the comment

    remove session start from tel2. once is enough

    To be more specific I would say the following : The two files you've provided are linked, one is included in the other. So basically you have to consider it as one file only. Also you can't start a session twice without closing inbetween, so only one session_start() is required.

    To help you understand the code would look like so after PHP has included the tel2.php file inside the first file :

    <?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:
            }
        }   
    
    }
    

    (The only time where you actually need to start a session again is if you had redirected the user on Tel2.php. In this case it would have been considered as a seperate file.)

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化