Serv from local IP (inet addr) with PHP

php

Use PHPs built-in server to serve from local IP - or inet addr. In order to use or test from other devices on the same local network.

#!/usr/bin/env php
<?php

// This will get your IP, e.g. http://192.168.87.100
$inet = trim(shell_exec('hostname -I'));

// If first arg is set ignore inet addr
// Then you could use e.g. localhost
if (isset($argv[1])) {
    $inet = $argv[1];
}

// Default port. If second arg is set then use this as the port
$port = 8080;
if (isset($argv[2])) {
    $port = $argv[2];
}

echo "Serving on $inet:$port";
echo PHP_EOL;
echo $command = "php -S $inet:$port";
echo PHP_EOL; 
passthru($command);


This page has been viewed 822 times. First hit: 16-Apr-2019 10:24:46
Share email, Google+, Twitter, Facebook.
comments powered by Disqus
Table