Zenji01 Posted June 18, 2009 Share Posted June 18, 2009 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. Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted June 18, 2009 Share Posted June 18, 2009 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"); Quote Link to comment Share on other sites More sharing options...
Zenji01 Posted June 19, 2009 Author Share Posted June 19, 2009 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]. Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted June 19, 2009 Share Posted June 19, 2009 You can echo out $servers[$i] to see what value is being passed. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.