Send variables $ _POST [] with header ("location: URL");

3

I have a PHP file in which I send the actions with forms in which there are inputs type hidden with the variables I need. But there comes a time when I need to reload the page and keep the variables of $_POST . The reload is necessary to avoid the re-submission of the form.

How could I do something like that? JavaScript maybe? or is there any way to do it with PHP?

    
asked by Pavlo B. 31.01.2017 в 09:57
source

1 answer

3

You need to use sesiones to use global variables to your program, these will be common in all your files, persisting until you close the session.

In this way you do not need to pass parameters through GET / POST, you can redirect using header() without losing data stored in session variables.

<!-- method POST -->
<input type="hidden" title="nombre usuario" name="usuario">
// Se abre una sesión.
session_start();
$miarray = $_REQUEST['usuario'];

$_SESSION['usuario'] = $usuario;

// Ya puedes redirigir.
header("location: ....")

// En otro fichero o en el mismo:
if(isset($_SESSION['usuario']) {
    echo "has logeado con el usuario temporal ".$_SESSION['usuario'];
}
// Se cierra sesión (de manera controlada).
session_destroy();

Or a PHP cookie :

$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
    
answered by 31.01.2017 / 10:21
source