Change php value with variable in url?

Asked

Viewed 242 times

1

I have the following code:

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

I would like to change the same values with a variable in the url, for example:

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?

1 answer

4


You can use the GET method :

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

If you do it this way, and don’t pass the variables, you 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 the 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';
  • I’ll try that

  • It did not work: http://radioturn.com.br/live/app.php?ip=stream.radioturn.com.br&port=8000

  • I messed up, I fixed it right there

  • I got it, thank you!

  • Is it possible to set a default value? in case I enter only in app.php? without the variable

  • Let me give you an example

  • @Paulosérgiofilho, take a look :)

  • It worked, thank you very much :)

  • 1

    Only one point to add, if PHP 7, you can use the ternary operator like this: $ip = $_GET['ip'] ?? 'ip padrao';

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.