PHP - 如何将数组发送到另一个页面? [英] PHP - How to send an array to another page?

查看:88
本文介绍了PHP - 如何将数组发送到另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数组发送到另一个页面.

I'm trying to send an array to another page.

我之前尝试的是:

第 1 页

<input type='hidden' name='input_name' value='<?php print_r($array_name); ?>' />

和 page2

<?php 
$passed_array = $_POST['input_name'];
?>

现在如何让 $passed_array 像数组一样工作?

Now how do I make $passed_array act like an array?

或者你知道解决这个问题的其他方法吗?

Or do you know of any other way of solving this problem?

谢谢,迈克.

我想这样做的原因是因为我需要避免会话和 cookie.

The reason I want to do it this way is because I need to avoid sessions and cookies.

推荐答案

你可以把它放在会话中:

You could put it in the session:

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

或者,如果您想通过表单发送它,您可以序列化:

Or if you want to send it via a form you can serialize it:

<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />

$passed_array = unserialize($_POST['input_name']);

会话的优点是客户端看不到它(因此不能篡改它)并且如果数组很大,它会更快.缺点是如果用户打开多个选项卡可能会造成混淆.

The session has the advantage that the client doesn't see it (therefore can't tamper with it) and it's faster if the array is large. The disadvantage is it could get confused if the user has multiple tabs open.

很多答案都建议使用name="input_name[]".这在一般情况下不起作用 - 它需要修改以支持关联数组,并进行大量修改以支持多维数组(icky!).坚持序列化要好得多.

a lot of answers are suggesting using name="input_name[]". This won't work in the general case - it would need to be modified to support associative arrays, and modified a lot to support multidimensional arrays (icky!). Much better to stick to serialize.

这篇关于PHP - 如何将数组发送到另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆