Jump to content

Passing a variable to the system() function


Zenji01

Recommended Posts

I'm very new to php. I'm looking to find a way to pass a variable to the system() function. I've tried looking it up but I don't think I'm searching the proper terminology. I'm using php 4.4.9 on FreeBSD 7.2 with apache 1.3.41. Code below. What i'm trying to do is execute the nc command and have it grab the server name in the $servers[$i] variable and try to connect to port 1494. The server list is sent to the php page from a form post. This is my first post the the forum so I hope i have provided all the important information. Please be gentle.

 

 

#!/usr/local/bin/php-cgi
<html>
<head>
<link rel="stylesheet" type="text/css" href="../style/style.css">
<title></title>
</head>
<body>
<br>
<br>
<?php
$serverlist = $_POST['servers'];
$servers = explode("\n",$serverlist);

if (empty($serverlist))
{
   print "No servers were entered";
}
else
{
   for($i = 0; $i < count($servers); $i++)
      {
          [color=red]system('nc -z $servers[$i] 1494');[/color]
          print "<br>";
      }
}
?>
</body>
</html>

 

EDITED BY akitchin: please use code tags in future posts.

The argument to system needs to be in double quotes or php will not expand the variable.

 

Also, system returns the last line of the output, so you either need to echo it, or assign it to a variable then echo it.

 

echo system("nc -z $servers[$i] 1494");

The argument to system needs to be in double quotes or php will not expand the variable.

 

Also, system returns the last line of the output, so you either need to echo it, or assign it to a variable then echo it.

 

echo system("nc -z $servers[$i] 1494");

 

 

Thanks for the responce. The nc command still seems to ignore $servers[$i].

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.