Home     Articles & Projects     Products & Web Services     Forum

Just got a VPS server and want to know can i do the following

Hi guys, just got myself a VPS server, I need to find away with PHP to open and close a port on the firewall while the PHP script runs.
Is that possible?

Hi Russell, 2 steps. First

Hi Russell,

2 steps. First thing to do would be to investigate the commands required (nothing to do with PHP) to open and close the ports as required. Then, you can get PHP to exec() those commands, either directly or via a shell script; for example:

<?php
 
// run command to open port
 
exec("/usr/bin/firewallctrl --open --port=22");
 
// do stuff that requires port 22 open
  // ....
  // run command close port
 
exec("/usr/bin/firewallctrl --close --port=22");
?>

The command in the exec() statements in the above are completely made up - but once you know what commands you need to control your firewall; then that should be all you need to do...

Cheers,
David.