How to display the previous navigation url with PHP?

Asked

Viewed 1,351 times

0

I use the function below to go to the next question of a question system I have,I could not create this function to go back,someone would have some hint?

Knob:

  echo"<button id='button' type='next' name='next' class='btn btn-danger'>
                      <span class='glyphicon glyphicon-circle-arrow-right'></span>Próxima</button><br />";

Function:

 if(isset($_GET['proxima'])){
    $pergunta = (int)$_GET['proxima'];
    header('location: comportamento.php?nro_pergunta='.$pergunta);  
 }

1 answer

3


Perhaps the solution is a very simple thing, as in the case below, using $_SERVER['HTTP_REFERER']

<?php if (isset($_SERVER['HTTP_REFERER'])): ?>
    <a href="<?php echo $_SERVER['HTTP_REFERER']; ?>">Voltar</a>
<?php endif ?>

Updating

As suggested in the comments, you can optionally check if the origin is from the same site. You can use the function parse_url to extract only the domain.

$host = parse_url($_SERVER['HTTP_REFERER', PHP_URL_HOST);

if ($host === 'exemplo.com.br') {
  // Lógica se for o mesmo domínio
}
  • 3

    And if he wants, he can still check if the referer is from the same site, to enable the button.

  • @Wallace Maxters , it worked thanks.

  • @Bacco , worked out thank you.

Browser other questions tagged

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