Determine execution according to home page

Asked

Viewed 59 times

1

Is there any way to define a certain action according to the source page?

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

  • 1

    What’s the point of this? I believe there are better ways to do it.

  • a safer way is by using Session or even client side cookies..

  • I didn’t even understand the problem...

  • 2

    In the previous page you make a Session to register the previous page and in the new page.. makes an if($_SESSION[x]=='display.php'){ displays such a pag } Else { displays another pag }

  • Thank you André Baill !!

  • @Andrébaill make an answer.

  • Okay done. @Victoralm if you would be so kind as to accept my reply. Thank you! :)

Show 2 more comments

2 answers

1

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

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

  1. Use of sesssions

Origin:

<?php 

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

Next page:

<?php

session_start();

if ($_SESSION['origem'] === "paginaOrigem"){

// ....

}
  1. Using cookies

Origin:

<?php 

setcookie('origem', 'PaginaOrigem');

Next page:

<?php


if ($_COOKIE['origem'] === "paginaOrigem"){

// ....

}
  1. Using URL parameters (if a link)

Origin:

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

Next page:

<?php


if ($_GET['origem'] === "paginaOrigem"){

// ....

}
  1. Using a field in hidden in the form (if a POST)

Origin:

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

Next page:

<?php


if ($_POST['origem'] === "paginaOrigem"){

// ....

}

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

When using Sesssions or cookies, try to clear the contents of the variable after the request of 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']);

-2


Do it this way:

On the previous page:

session_register("pagina_atual");

And on the new page:

if($_SESSION['pagina_atual']=="exibir"){
pagina exibir
} else {
pagina outra
}
  • 2

    session_register is an old function that has already been removed from PHP since 5.4. Please rephrase your answer.

Browser other questions tagged

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