Determine execution according to source page

1

Is there any way to set a particular action based on the source page?

Ex: if the previous page is exibir.php execute "x" or if the previous page is inserir.php execute "y".

    
asked by anonymous 08.07.2015 / 15:45

2 answers

-2

Do as follows:

Previous page:

session_register("pagina_atual");

And on the new page:

if($_SESSION['pagina_atual']=="exibir"){
pagina exibir
} else {
pagina outra
}
    
08.07.2015 / 18:35
1

Because HTTP requests are isolated, we need to use other resources to point the source page. All of these involve defining an identifier of the source page that will be passed to the next page.

Some alternatives that can be explored include (it's up to you to decide which one to use in each case):

  • Using sessions
  • Source:

    <?php 
    
    session_start();
    $_SESSION['origem'] = "paginaOrigem";
    

    Next Page:

    <?php
    
    session_start();
    
    if ($_SESSION['origem'] === "paginaOrigem"){
    
    // ....
    
    }
    
  • Using cookies
  • Source:

    <?php 
    
    setcookie('origem', 'PaginaOrigem');
    

    Next Page:

    <?php
    
    
    if ($_COOKIE['origem'] === "paginaOrigem"){
    
    // ....
    
    }
    
  • Using URL parameters (if link)
  • Source:

    <a href="paginaDestino.php?origem=paginaOrigem">Link</a>
    

    Next Page:

    <?php
    
    
    if ($_GET['origem'] === "paginaOrigem"){
    
    // ....
    
    }
    
  • Using a field in hidden in form (if it is a POST )
  • Source:

    <input type="hidden" name="origem" value="paginaOrigem">
    

    Next Page:

    <?php
    
    
    if ($_POST['origem'] === "paginaOrigem"){
    
    // ....
    
    }
    

    Considerations: Using GET (parameter by URL) is the easiest to manipulate.

    When using sessions or cookies, try clearing the contents of the variable after requesting the source page, as this can cause the next page to perform the same action twice and get some "dirt"

    <?php
    
    
    if ($_COOKIE['origem'] === "paginaOrigem"){
    
    // ....
    
    }
    
    // Terminou de executar tudo?
    setcookie('origem', null, -1);
    // Ou em caso de Session
    unset($_SESSION['origem']);
    
        
    08.07.2015 / 20:20