Jump to content

[SOLVED] Need Help with short script!


will35010

Recommended Posts

Hi All,

  Just wanted to say thanks in advanced. I'm a complete noob when it comes to php. I run a few game servers and I'm trying to put together a script that sends console messages. I have the messages in an array so I can add new ones easily, but it has a problem with line 15. Line 15 is send_msg($msg);

 

Here is the script:

 

<?php

 

// - edit - this is  Open Source of course....

 

/*-------------------------------------------------------------------------*/

// Set up the messages

/*-------------------------------------------------------------------------*/

 

$msg = array(0 => '"Radio spammers will be killed!"',

1 => '"LG members are admins so shut up!"',

2 => '"Hax accusers will be tarred and beaten!"',

3 => '"Get a demo and shut up!"',

4 => '"Get your stats @ www.LowGravityURT.com"');

foreach ($msg as $key => $value) {

print "$value\n";

}

send_msg($msg);

// ----------------------------------------

// function to connect to game server and

// send the message to the q3a console

// ----------------------------------------

 

function send_msg($msg){

  $rconpassword = "urtlg";

  $port = "27960"; // default quake port. Change if needed

  $start = "\xff\xff\xff\xffrcon " . $rconpassword . " ";

 

// ----------------------------------------

// Open socket

// ----------------------------------------

 

  $fp = fsockopen("udp://8.12.16.171", $port, $errno, $errstr, 10);

 

// ----------------------------------------

// Error checking

// ----------------------------------------

 

  if (!$fp) {

    echo "$errstr ($errno)<br />\n";

  } else {

    stream_set_timeout($fp, 2);  // Set a low timeout

 

  // Send commands

  fputs($fp, $start . "say " . $msg . "\n");

  fclose($fp);

 

 

  } // end if

 

// ----------------------------------------

// Change to your liking    sleep(seconds)

// ----------------------------------------

sleep(4);

}  // end function send_msg

 

?>

Link to comment
https://forums.phpfreaks.com/topic/128480-solved-need-help-with-short-script/
Share on other sites

first you never close off your foreach statement

second you need a ; after print()

and

your send_msg($msg); doesnt make any sense because $msg=array so it should be

send_msg($msg[0]); for message one in example

or

send_msg($msg[1]); for message 2

 

 

 

first you never close off your foreach statement

second you need a ; after print()

and

your send_msg($msg); doesnt make any sense because $msg=array so it should be

send_msg($msg[0]); for message one in example

or

send_msg($msg[1]); for message 2

 

Thank you!!! That's what I was looking for. I was able to fix the other errors, but it would only print "array" on the console. I didn't know how to call each message. Can I put a wait between messages?

well if you want a time space between each post you could use

sleep($num_seconds_to_sleep)

 

but if you want to send all messages you are going to have to do what CroNiX said

foreach ($msg as $key => $value) {
      print "$value\n";
      send_msg($value);
}

 

or you will have to call each message

send_msg[0];

sleep(5);//sleep 5 seconds

send_msg[1];

 

it all depends what you want to do

Thanks all for the quick help!!! I will change it to what cronix said so I don't have to call each one. Here is the working script:

 

<?php

 

// - edit - this is  Open Source of course....

 

/*-------------------------------------------------------------------------*/

// Set up the messages

/*-------------------------------------------------------------------------*/

 

$msg = array(0 => '"Radio spammers will be killed!"',

1 => '"LG members are admins so shut up!"',

2 => '"Hax accusers will be tarred and beaten!"',

3 => '"Get a demo and shut up!"',

4 => '"Get your stats @ www.LowGravityURT.com"');

foreach ($msg as $key => $value) {

print "$value\n";

}

send_msg($msg[0]);

sleep(3);

send_msg($msg[1]);

sleep(3);

send_msg($msg[2]);

sleep(3);

send_msg($msg[3]);

sleep(3);

send_msg($msg[4]);

 

// ----------------------------------------

// function to connect to game server and

// send the message to the q3a console

// ----------------------------------------

 

function send_msg($msg){

  $rconpassword = "urtlg";

  $port = "27960"; // default quake port. Change if needed

  $start = "\xff\xff\xff\xffrcon " . $rconpassword . " ";

 

// ----------------------------------------

// Open socket

// ----------------------------------------

 

  $fp = fsockopen("udp://8.12.16.171", $port, $errno, $errstr, 10);

 

// ----------------------------------------

// Error checking

// ----------------------------------------

 

  if (!$fp) {

    echo "$errstr ($errno)<br />\n";

  } else {

    stream_set_timeout($fp, 2);  // Set a low timeout

 

  // Send commands

  fputs($fp, $start . "say " . $msg . "\n");

  fclose($fp);

 

 

  } // end if

 

// ----------------------------------------

// Change to your liking    sleep(seconds)

// ----------------------------------------

sleep(4);

}  // end function send_msg

 

?>

I was also trying to point out that you already have a sleep() at the end of your send_msg() function, which is currently set to 4 seconds.  So it will send a message, wait 4 seconds, send another message....until there are no more to send.

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.