Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Redirect Same Address Different Port

Right now I have this:

header("Refresh: 0; url=http://192.168.100.100:10500/redirect2.php");

How can I do the same redirect but without writing address, only port? Both files are in the same folder on the same server.

The thing is that I don't know the address that will be used to access this server (private or public).

like image 406
Kliwer Avatar asked Jun 14 '26 21:06

Kliwer


2 Answers

Use the superglobal $_SERVER array, Location header and exit;

$port = '10500';
header('Location: '
    . ($_SERVER['HTTPS'] ? 'https' : 'http')
    . '://' . $_SERVER['HTTP_HOST'] . ':' . $port
    . $_SERVER['REQUEST_URI']);
exit;
like image 140
Daniel W. Avatar answered Jun 18 '26 00:06

Daniel W.


Maybe it's be useful for someone, isHttps() function can detect SSL.

function isHttps() {
      return
        (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
        || $_SERVER['SERVER_PORT'] == 443;
    }

$destinationPort = '10500';

$schema = isHttps() ? "https" : "http";
$schema .= "://";
header('Location: ' . $schema . $_SERVER['HTTP_HOST'] . ':' . $port
    . $_SERVER['REQUEST_URI']);

exit;
like image 34
Manian Rezaee Avatar answered Jun 18 '26 01:06

Manian Rezaee