-2

I have form with 4 drop downs. I am first placing the values into an array named $gdc. I would like to send that array to another page (say nextpage.php). However, I don't want to use sessions. Is there some other way to do this?

Here's my code:

<form name="f1" method="post">
<?php
include("dbconnect.php");
for($b=0;$b<4;$b++)
{
$w=mysql_query("select * from gdc");
$x=mysql_num_rows($w);
if($x>0)
{
echo "<td><select name='gdc".$b."'>";
while($y=mysql_fetch_array($w))
{
echo "<option>".$y['GDC_CD']."</option>";
}
}
echo "</select>";
?>
<p><input type="submit" name="btn" value="OK"/></p>
<?php
if(isset($_POST['btn']))
{
for($b=0;$b<4;$b++)
{
$gdc[$b]=$_REQUEST['gdc'.$b];
}
}

I found some code which might work. (See below) However, I can't implement it in my form since my array offsets are not like the one in this example:

page1:

<?php
 $arr = array();
    $arr[1] = "one value here";
    $arr[2] = "second value here";
    $arr[3] = "third value here";
header('Location:page2.php?' . http_build_query($arr, null, '&'));
?>

page2:

<?php
echo $_GET['one'];
?>
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • 2
    Why don't you want to use a session? Why make this harder than it needs to be? – andrewsi Jul 12 '13 at 18:12
  • you can save the serialized data to a random filename and pass the filename arround – Orangepill Jul 12 '13 at 18:13
  • @andrewsi Race conditions involving multiple tabs, I expect – Izkata Jul 12 '13 at 18:14
  • if you're concerned about some kind of security thing or something you can store the serialized array in a database using a unique key, then pass that key to the next page by $_GET, $_SESSION, or $_COOKIE. All perfectly valid. Just saying I don't wanna doesn't make sense unless you explain why you can't use sessions. – Kai Qing Jul 12 '13 at 18:14
  • possible duplicate of [how to pass an array in GET in PHP?](http://stackoverflow.com/questions/5098397/how-to-pass-an-array-in-get-in-php) – vascowhite Jul 12 '13 at 18:16

3 Answers3

1

If it's really just arrays and you do not have the $_SESSION option, you can json_encode the array, then urlencode and pass, then decode it on the next page.

Example

sender.php

<?php

$data = array('a' => 'b');
$param = urlencode(json_encode($data));
header("Location: receiver.php?data={$param}");

receiver.php

<?php

if (isset($_GET['data'])) {
    $data = json_decode(urldecode($_GET['data']), TRUE);
    // do whatever you want with your array
}
VPZ
  • 741
  • 8
  • 19
0

$_SESSION variables are the best way to keep data specific to the user. Adding URL parameters (e.g., http://www.example.com/default.php?foo=bar, where $_GET['foo'] = bar) are a risk as they can be edited by the user, or lost completely.

Mooseman
  • 18,763
  • 14
  • 70
  • 93
0

You can serialize an array. This turns it into a string, which can be passed by any normal means in an HTTP request. i.e. encoded in the URL or as a hidden post variable:

<?php
$serializedVersion = serialize ($myArray);
echo ("<input type = 'hidden' name = 'html-version-of-array' value = '$serializdVersion' />");

Then have the php other file:

<?php
$serializdVersion = $_POST['html-version-of-array'];
$myArray = unserialize($serializdVersion);
Jim Maguire
  • 1,020
  • 1
  • 8
  • 19