save several parameters in a single session

2

I have a question with PHP , I make a form with only input , and with 2 buttons, one to save and another to collect.

The idea is that when you click on the save button, it will be stored in $_SESSION , as many variables as you type in input , but it only saves the last written variable.

I put some of the code.

index.php

<body>
<form action="guardar.php" method="post">


    <br/><br/>
    <h1>Escriba una palabra o un numero</h1><br/>
    <input type="text" name="datoparametro" VALUE="" id="datoparametro"><br/><br/>


    <input type="submit" name="ok" VALUE="guardar"><br/>


</form>

<input type="submit" value="recojer" onclick="location='recojer.php'"/> <br/>
<input type="submit" value="borrar" onclick="location='borrar.php'"/>
</body>

save.php

<?php

session_start();
$midato=$_POST["datoparametro"];


$_SESSION["dato"]=$midato;


if(isset($_SESSION["dato"])){

  $midato=$_SESSION["dato"]+ 1;
}
else{
    $midato=array();
    $midato[]=$_POST["datoparametro"];
    $_SESSION["dato"]=$midato
}

?> 
    
asked by racxo 18.02.2017 в 11:20
source

3 answers

0

The answer is simple, and you do not need to check if there is $ _SESSION ["data"] ...

<?php
    session_start();       
    $_SESSION["dato"][]=$_POST["datoparametro"];
?> 
    
answered by 18.02.2017 / 18:04
source
2

If the idea is to save as many values in the session variable $_SESSION["dato"] as they are sent in $_POST["datoparametro"] , you could do the following:

  • Validate if variable $_SESSION["dato"] exists. In the case that it does not exist, we initialize it as a array .

  • Then we add the value sent in $_POST["datoparametro"] to the array in variable $_SESSION["dato"] .

Example:

<?php

session_start();

// Si no existe, la inicializamos
if (empty($_SESSION["dato"])) {
    $_SESSION["dato"] = array();
}

// Agregamos el nuevo valor al arreglo
$_SESSION["dato"][] = $_POST["datoparametro"];
?>
    
answered by 18.02.2017 в 16:33
0

In order to pass the variable as a session, this code must be done:

if ($ldapbind) {        
        session_start();
        $_SESSION['name']=$_POST["t1"]; 
           echo" alert('Bienvenido Al Panel de  Administracion de Usuario.');
            window.location.href='panel.php'; "; 
    } 

Then on your other page where you will receive the name before HTML you must place this code: It is also with a little security if the other person knows the name of your other page but has not logged in with that code can not access, you can only access if it is logged in.


session_start(); 
if (!isset($_SESSION['name'])) { 
    header("location: index.php");
}

    
answered by 18.02.2017 в 16:58