How to store variable values ​​when loading multiple pages

I am creating a php script, which stores 3 array: $images, $urls, $titlesbased on the input form in php file.

I want to print the values ​​of these arrays in the first part of the page, and then pre-populate the form input fields with the value of the arrays. Also, when the user changes the input field and clicks "Save", the page should reload with the modified version.

My problem is that every time the php file is called in the browser, the value of the variables is deleted. Is there a way to store array values ​​so that the form is always filled with the last saved values?

<?php
//save the arrays with the form data
$images = array($_POST["i0"],$_POST["i1"],$_POST["i2"],$_POST["i3"]);
$urls = array($_POST["u0"],$_POST["u1"],$_POST["u2"],$_POST["u3"]);
$titles = array($_POST["t0"],$_POST["t1"],$_POST["t2"],$_POST["t3"]);
//print the arrays
print_r($images);
print_r($urls);
print_r($titles);
//create the form and populate it
echo "<p><form method='post' action='".$_SERVER['PHP_SELF']."';";
$x = 0;
while ($x <= 3) {
   echo"<div>
            <input name='i".$x."' type='text' value='".$images[$x]."'>
            <input name='u".$x."' type='text' value='".$urls[$x]."'>
            <input name='t".$x."' type='text' value='".$titles[$x]."'>";
       $x++;
}
?>
<br>
<input type="submit" name="sumbit" value="Save"><br>
</form>
+5
source share
7 answers

PHP.

session_start();
$_SESSION['images'] = $images;

( ) :

session_start();
$images = $_SESSION['images'];
+15

. , , .

<?php
    if(sizeof($_POST) >0)
    {
      //UPDATE VALUES
    }
?>
+2

, , , session_start(); script. $_SESSION [$ x] .

, : " , ?", $_POST, :

<?php
if(!$_POST){
    $_POST = array();
    foreach(array("i0","i1","i2","i3") as $i) $_POST[$i]="";
    foreach(array("u0","u1","u2","u3") as $i) $_POST[$i]="";
    foreach(array("t0","t1","t2","t3") as $i) $_POST[$i]="";
}
foreach($_POST as $k=>$v)  filter_input(INPUT_POST,$k,FILTER_SANITIZE_SPECIAL_CHARS);

//save the arrays with the form data
$images = array($_POST["i0"], $_POST["i1"], $_POST["i2"], $_POST["i3"]);
$urls   = array($_POST["u0"], $_POST["u1"], $_POST["u2"], $_POST["u3"]);
$titles = array($_POST["t0"], $_POST["t1"], $_POST["t2"], $_POST["t3"]);


//print the arrays
print_r($images);
print_r($urls);
print_r($titles);
//create the form and populate it
echo "<p><form method='post' action='".$_SERVER['PHP_SELF']."';";
$x = 0;
while ($x <= 3) {
   echo"<div>
            <input name='i".$x."' type='text' value='".$images[$x]."'>
            <input name='u".$x."' type='text' value='".$urls[$x]."'>
            <input name='t".$x."' type='text' value='".$titles[$x]."'>";
       $x++;
}
?>
<br>
<input type="submit" name="sumbit" value="Save"><br>
</form>

: foreach($_POST as $k=>$v) filter_input(INPUT_POST,$k,FILTER_SANITIZE_SPECIAL_CHARS); , XSS. , GET POST, , .

+2

, . IP- AXIS . , - , , SESSION - , , PHP:

. PHP, , apc_store() ( ).

http://php.net/manual/en/book.apc.php

+1

sessions . isset() , "" .

0

The solution you are looking for is a session. Use $_SESSIONto store the values ​​of your variables. For example, at the end of the script:

$_SESSION['images'] = $images;

and in the input form:

<input name='i".$x."' type='text' value='".(isset($_SESSION['images']) ? 
$_SESSION['images'] : '')."'>
0
source

All Articles