Get session username in any file?

0

I'm doing a small application where the user registers some data and I want the user to be registered in the registry but I can not get it.

1.- The user starts session (login.php) and I have a hidden form that sends the username to another POST file

echo "Bienvenido $username
<form action='add_retiro.php' method='post'>
<input type='hidden' class='form-control' id='username' name='username' 
value='$username'  required>
<input type='submit' value='Empezar'/>
";

2.- The 2nd file receives the username (add_registro.php)

<?php
$time = time();
$username = $_POST['username'];
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
} else {
echo "Inicia Session<br>";
echo "<br><a href='http://web.xyz/index.html'>Login</a>";
exit;
}

$now = time();
$fechaguardada = $_SESSION["expire"];
$tiempo_transcurrido = $now-$fechaguardada;

if($tiempo_transcurrido >= 60000) {
session_destroy();

header('Location: http://web.xyz/index.html');
exit;
 }
?>

<html>
<form action="add_retiro1.php" method="post"><h2>Deposito de Retiros</h2>

<input class="large" type="number" id="inputtext2" name="inputtext2"  
placeholder="Escribe el Numero de Retiro" required/>


<input class="large" type="number" id="inputnumber1" name="inputnumber1" 
placeholder="Escribe el Monto del Retiro" required/>

<input type="hidden" class="form-control" id="inputtext3" name="inputtext3" 
value="<?php echo $username ?>" placeholder="Nombre de Usuario" required />

<input type="hidden" class="form-control" id="inputtext4" name="inputtext4"  
value="<?php  echo date("d-m-Y (H:i:s)", $time -18000); ? >" 
placeholder="Hora y Fecha" required />


<input type="submit" value="Enviar"/></div>

</form>

This registers correctly in the bd After registration the php file that does the insert function in the db redirects to the file add_registro.php

But when recapturing another record the User appears blank because $ username = $ _POST ['username']; IT DOES NOT HAVE VALUE since it was a redirection

WHAT SOLUTION DO YOU RECOMMEND ME?

open some way to get the username of the session to use it in any file?

Helppppp ......

    
asked by Jesus Obregon 13.07.2017 в 19:10
source

1 answer

1

The code you sample is a bit weird or incomplete.

We go to the mess, you can do the persistence of data in several ways but I will focus on 1 that I see that you use with knowledge or not, Sessions .

The sessions allow persistence of data for the duration of the session, how to use them is simple

file_1.php

<?php
// indicamos a PHP que vamos a usar sesiones
session_start();
// creamos una variable de sesion
$_SESSION['miVariable'] = 'Este string sera guardado en la variable de sesion';
header('Location: otro_archivo.php');

other_file.php

<?php
// indicamos a PHP que vamos a usar sesiones
session_start();
// imprimimos el contenido de sesion
echo $_SESSION['miVariable']; // Este string sera guardado en la variable de sesion
    
answered by 13.07.2017 в 21:31