PHP Scripting Tutorial

Everything todo with programming goes HERE.
Post Reply
User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

PHP Scripting Tutorial

Post by Light »

What You Need

Your server needs to be set up with scripting. If you're renting from a Tron server host, then it's probably already set up for you. Otherwise, set a script directory and install what you're expecting to use. For this tutorial, you will need PHP installed. If you need help setting this up, you can reference the wiki page. Alternatively, I have a script that sets everything up for you.

You should locate your server's ladderlog.txt file. Using this as a reference is going to make life so much easier.

An IDE is optional, but it does make it faster to write code. If you want something that doesn't interfere with you writing too much, you could try out Geany. If you want something that helps you out a lot more, you may give Eclipse or NetBeans a try.


Shebang Line

We're going to need a shebang line at the top of our PHP page, pointing to PHP. So, our file this far will look like this.

Code: Select all

#!/usr/bin/php
If you have shell access, you can find it's location on Linux machines with the following command. I think it also works on Mac, but I may be wrong.

Code: Select all

[email protected]:~$ which php
/usr/bin/php
You can find more information on the shebang line here:
http://en.wikipedia.org/wiki/Shebang_(Unix)


Getting Started

We're going to start with our opening and closing PHP tags.

Code: Select all

#!/usr/bin/php
<?php



?>
Now we want to make a loop. Inside this loop, we're gonna wait until there's a new line written to the log, read it's contents, and see if we want to do something with it. Let's start with creating our while loop.

Code: Select all

#!/usr/bin/php
<?php

while (!feof(STDIN))
{
	
}

?>

User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

Re: PHP Scripting Tutorial

Post by Light »

Reading the Data

So, we have a couple new things here. Function feof(), which checks for the end of file, and the exclamation mark which checks that it's false. feof() is boolean, meaning it returns either true or false. Here's a small example to show the use of !.

Code: Select all

// This ...
if ($this == false)
{
}

// ... is the same as this.
if (!$this)
{
}
Then, we have STDIN. This is our input stream. It's what content is being sent to the script, which in our case, is the log data. We checked to make sure we weren't at the end, so now let's read our line.

Code: Select all

#!/usr/bin/php
<?php

while (!feof(STDIN))
{
	$line = rtrim( fgets(STDIN) );
}

?>
$line is just a variable. We're setting it to what's returned by the rest. So, it will be a string.

rtrim() will trim off the right side of what's returned. It's going to strip new lines or spaces by default, which is good for us.

fgets() reads a chunk of data at a time, stopping at new lines. This is perfect for us since everything written in the log ends in a new line.

As a side note. Everything written has a new line appended to it. This means that there will be an extra loop made if we check for the end of line in the while statement. If we instead check after we read the next line, we could prevent the script from running an extra loop. I just find it nicer to put it in the while statement.

Here's what the more efficient script would look like as we are now. Just set the while loop to true or 1 so it always loops, and check for the end of file inside.

Code: Select all

#!/usr/bin/php
<?php

while (1)
{
	$line = rtrim( fgets(STDIN) );
	
	if (feof(STDIN))
		break;
}

?>
If it's the end of file, we'll just break out of the while loop and stop. Now, let's continue ...
Last edited by Light on Thu Feb 20, 2014 1:47 am, edited 1 time in total.

User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

Re: PHP Scripting Tutorial

Post by Light »

Sorting the Data

I'm going to make this a little easier. Instead of using regex to check everything, we will just explode the string. This is going to split the line into parts, which we will separate by spaces. Here's an example of a log line.

Code: Select all

CHAT [email protected] hahah!!!
Sorry, Lowkey. :P You were the first one I saw. So, what happened here? The CHAT at the beginning of the line shows us that it was a message written into regular chat. [email protected] is the one who wrote it, and the message was everything that follows. In this case, it was "hahah!!!". Let's split our lines into pieces now.

Code: Select all

#!/usr/bin/php
<?php

while (!feof(STDIN))
{
	$line = rtrim( fgets(STDIN) );
	$part = explode(" ", $line);
}

?>
$part is another variable. It's collecting what's returned from the explode function, which is splitting $line at every " " (space). This is going to return an array, and store that in $part. This is what $part looks like if we run it through print_r().

Code: Select all

Array
(
    [0] => CHAT
    [1] => [email protected]
    [2] => hahah!!!
)
So, at every space, we've cut the line of text. Now, we can check pieces of it much more easily. You can read each piece by using our variable $part and the array key we want to call. 0 is the first key, so we will check to see what that is, and make sure it's a line we want to use. In this case, it's "CHAT".


Checking the Line

Let's make an if statement, and see if it's a chat line. We're just going to see if $part[0] is equal to "CHAT".

Code: Select all

#!/usr/bin/php
<?php

while (!feof(STDIN))
{
	$line = rtrim( fgets(STDIN) );
	$part = explode(" ", $line);
	
	if ($part[0] == "CHAT")
	{
		
	}
}

?>
So, if the line doesn't start with "CHAT", we're just going to ignore it and wait for the next line. This if statement only returns true if someone writes something into chat. Let's get the message they wrote now ..

Code: Select all

#!/usr/bin/php
<?php

while (!feof(STDIN))
{
	//$line = rtrim( fgets(STDIN) );
	$line = "CHAT [email protected] hahah!!!";
	$part = explode(" ", $line);
	
	if ($part[0] == "CHAT")
	{
		$message = implode(" ", array_slice($part, 2));
	}
}

?>
Here, we're setting $message to what the user said. We're using implode() to put together the array being returned by array_slice(). It's going to separate each piece by a space so it's a normal sentence now instead of an array. We're offsetting the array by 2, so it's going to get rid of the first two items, which are the type of log (CHAT) and the user ([email protected]). It will leave whatever the user said.

So, here's what $message actually is, just to show you what we're dealing with a little more visually.

Code: Select all

$part = array("CHAT", "[email protected]", "hahah!!!");
$message = "hahah!!!";

// If the message was longer than one word ..
$part = array("CHAT", "[email protected]", "hahah!!!", "weeee");
$message = "hahah!!! weeee";
Let's now check to see if what they said was "Hello". This should be pretty straight-forward at this point.

Code: Select all

#!/usr/bin/php
<?php

while (!feof(STDIN))
{
	$line = rtrim( fgets(STDIN) );
	$part = explode(" ", $line);
	
	if ($part[0] == "CHAT")
	{
		$message = implode(" ", array_slice($part, 2));
		
		if (strtolower($message) == "hello")
		{
			
		}
	}
}

?>
The only new thing here is strtolower. This is so they don't have to type it a certain way to trigger the script. Also, make sure that what we're checking it to be equal to is all lowercase, otherwise making the first lowercase will always make it return false and never work.

User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

Re: PHP Scripting Tutorial

Post by Light »

Using Server Commands

Everything to get to here was important, but this is probably the part you wanted most. We're going to make the script actually do something to the server!

Everything the script outputs is read by the server. The server, much like our script, also uses new lines to tell it when it's ready to use our output. So, when we output something, we always need to append a new line to it.

So, this far, we have someone saying "hello" to the server. Let's say hello back. We will use a CONSOLE_MESSAGE so we can make it appear to be a normal message. We will color our name, and use a chat color of "0xffff7f", which is the default chat text color. This will make the script appear to be a regular user in chat on users' clients.

Code: Select all

#!/usr/bin/php
<?php

while (!feof(STDIN))
{
	$line = rtrim( fgets(STDIN) );
	$part = explode(" ", $line);
	
	if ($part[0] == "CHAT")
	{
		$message = implode(" ", array_slice($part, 2));
		
		if (strtolower($message) == "hello")
		{
			echo "CONSOLE_MESSAGE 0xcc00ddSmart Server0xffff7f: Hello, {$part[1]}!" . "\n";
		}
	}
}

?>
We're going to be named "Smart Server", and write "Hello, USERNAME!". If you remember earlier, their username was the second item in the array $part, with the key 1. We put brackets around it to assure everything is escaped properly when we echo it out. Otherwise, having a " in their name may ruin our script.

User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

Re: PHP Scripting Tutorial

Post by Light »

Load the Script

Congratulations! The script is done. All you have to do is go load the script into the server. You need load it from a configuration file, because users are not permitted to load scripts on the server.

script.cfg

Code: Select all

KILL_SCRIPT script.php
SPAWN_SCRIPT script.php
So, once you have that in your settings directory, you can include it with the following command. Just remember, "script.php" is the name of your PHP file in your scripts folder. Set it accordingly.

Code: Select all

INCLUDE script.cfg
Now, the script is loaded. At this point, you just need to trigger the script by saying "hello".
2014-01-26_11-14-54.png

User avatar
kyle
Reverse Outside Corner Grinder
Posts: 1867
Joined: Thu Jun 08, 2006 3:33 pm
Location: Indiana, USA, Earth, Milky Way Galaxy, Universe, Multiverse
Contact:

Re: PHP Scripting Tutorial

Post by kyle »

wiki material? and can i use vim :)
Image

User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

Re: PHP Scripting Tutorial

Post by Light »

kyle wrote:wiki material? and can i use vim :)
I thought about puttin' it on wiki, but I like forums formatting better. lol I forgot about the link limit though, and ran into that when I was posting. >_>

Uhm, sure you can use vim, but ain't you a programmer for something around here? It's just like any other language when it comes to writing, except you don't need anything special to compile, other than PHP packages to run it. Any text editor will do.

User avatar
kyle
Reverse Outside Corner Grinder
Posts: 1867
Joined: Thu Jun 08, 2006 3:33 pm
Location: Indiana, USA, Earth, Milky Way Galaxy, Universe, Multiverse
Contact:

Re: PHP Scripting Tutorial

Post by kyle »

Light wrote:Uhm, sure you can use vim, but ain't you a programmer for something around here? It's just like any other language when it comes to writing, except you don't need anything special to compile, other than PHP packages to run it. Any text editor will do.
I was just kidding about the vim thing :P and yes I do program.
Image

User avatar
Jip
Round Winner
Posts: 397
Joined: Sat Sep 26, 2009 5:32 pm

Re: PHP Scripting Tutorial

Post by Jip »

Nice tut. Did you know there is a PHP framework for arma scripting?

User avatar
Light
Reverse Outside Corner Grinder
Posts: 1664
Joined: Thu Oct 20, 2011 2:11 pm

Re: PHP Scripting Tutorial

Post by Light »

Jip wrote:Nice tut. Did you know there is a PHP framework for arma scripting?
I have my own little framework with plug-and-play scripts and whatnot, but this was just something small to show people how to get started. I get asked way too often, and all I had to show really was an old PM, so I thought I'd just make something I can reference people to.

Post Reply