27.12.2014, 07:16
(
Last edited by TakeiT; 07/01/2015 at 12:10 AM.
)
This is designed to more or less integrate with a server UCP. However, it can be used to easily start and stop your server remotely, without losing any data or worrying about ssh.
The first step is to login to ssh and create a new user, using the function adduser. Then, install the server to /home/username (The name you just created). This will make things simple.
Now, all we've got to do is make the script, let's get started.
We're going to create the buttons to start, so open notepad and design as needed. This is an unstyled verstion. I'll make the code, and comment what each line does.
This is extremely basic, and I would recommend using some sort of styling, or importing it to a user control panel.
But that's it for the html code, now we have to wire these buttons up. Let's add the api next. Create a folder called include, and paste the api found here: https://sampforum.blast.hk/showthread.php?tid=355574 inside. I renamed it to samp.php to make it easier.
Now, at the top of your html script, we will set everything up.
This will set everything up. We've included the API, and the functions that are called when the buttons are pressed. Now, lets tell the script what to do when the buttons are pressed.
Simple as that! Now, when that is pressed, the server should start up. Now, we have to stop the server.
And there you have it. A basic start/stop system. Now, obviously, there are better ways to write this code. I would personally define the ips and passwords in a seperate config file, and style everything to look nice, however, I made this to be basic as possible so everyone can understand. If you have any questions, reply below. Any typos, I apologize in advance. I wrote this at 3am.
The first step is to login to ssh and create a new user, using the function adduser. Then, install the server to /home/username (The name you just created). This will make things simple.
Now, all we've got to do is make the script, let's get started.
We're going to create the buttons to start, so open notepad and design as needed. This is an unstyled verstion. I'll make the code, and comment what each line does.
PHP Code:
<form method='POST' action='control.php'> //you want the name to match the name of the php document, IE control.php.
<input type='submit' name="start" value='Start Server' /> //creates a button that will be used to start the server
<input type='submit' name="stop" value='Stop Server' />
</form>
But that's it for the html code, now we have to wire these buttons up. Let's add the api next. Create a folder called include, and paste the api found here: https://sampforum.blast.hk/showthread.php?tid=355574 inside. I renamed it to samp.php to make it easier.
Now, at the top of your html script, we will set everything up.
PHP Code:
<?php
include('include/samp.php');
if($_POST['start'])
{
}
if($_POST['stop'])
{
}
?>
PHP Code:
if($_POST['start'])
{
$connection = ssh2_connect('serverip', 22); //port is usually 22. change serverip to the ip, leaving the quotes.
ssh2_auth_password($connection, 'username', 'password'); //logging into the ssh, much like putty. Pretty self explanatory on what to do
$stream = ssh2_exec($connection, 'cd /home/username/samp; nohup ./samp03svr &'); //runs change directory (cd) command. Set it to your server's location. after the semicolon, it runs a separate command to start the server.
echo "Server started."; //sends a message on the webpage saying the server was started.
}
PHP Code:
if($_POST['stop'])
{
$query = new SampRcon("ip", 7777, "rcon pass"); //fill in the credentials for the server. this will allow connection to the rcon.
$query->call("say {FFFFFF}The server will be restarted. You will be kicked.", 1); //send a message in game to tell the players the server will be stopped.
$query->call("exit", 2); //send the command /rcon exit. will stop the server.
$query->close(); //close the connection
echo "Server stopped";
}