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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.