hgnisitida hgnisitida - 4 months ago 38
PHP Question

Run php script multiple times from a for loop

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

Answer

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.)

Comments