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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料