Skip to content

Instantly share code, notes, and snippets.

@axgle
Created October 25, 2018 05:19
Show Gist options
  • Save axgle/af1f2224900f8764b83ad7225a80ca77 to your computer and use it in GitHub Desktop.
Save axgle/af1f2224900f8764b83ad7225a80ca77 to your computer and use it in GitHub Desktop.
get valid random socket port with php
<?php
//get valid random socket port with php
$s = stream_socket_server("tcp://localhost:0");
$ss = stream_socket_get_name($s,0);
$pos =
strrpos($ss,":");
$p = substr($ss,$pos+1);
fclose($s);
//$p+=1;
echo "http://localhost:$p\n";
exec("php -S localhost:$p");
//var_dump($ss);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment