Home Account

Serv from local IP (inet addr) with PHP

2019-04-15 19:52 dennis iversen

Tags: 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 requested 2917 times