Changing php value with a variable in the url?

1

I have the following code:

$ip = 'stream.radioturn.com.br';
$port   = '8000';

I would like to change the same values with a number in the url, eg

app.php?ip=ipqualquer.com.br&port=2531

And so it would display the same code but with the values of the new url

How can I do this?

    
asked by anonymous 09.01.2017 / 01:54

1 answer

4

You can use the GET method:

$ip=$_GET['ip'];
$port=$_GET['port'];

If you do this, and do not pass the variables, you will probably have an error displayed. So do it this way:

//verificando a existencia
if(isset($_GET['ip']) && isset($_GET['port'])){
// se existir
$ip=$_GET['ip'];
$port=$_GET['port'];
} else{
// aqui não existe, ai voce pode passar outros valores
$ip='ip padrao';
$port='porta padrao';

}

Or with simplified if and else that has the same effect:

$ip=isset($_GET['ip'])?$_GET['ip']:'ip padrao';
$port=isset($_GET['port'])?$_GET['port']:'Porta padrao';
    
09.01.2017 / 01:57