开发者

Accessing a variable from a previous page load?

I haven't touched PHP in years and have been asked to create an wordpress plugin. I'm trying to figure out the "correct" way to do the following:

page1.php
---------
some_plugin(array(
    'option1' => 'some_data',
    'option2' => 'http://do_not_expose_to_client/'
});

gene开发者_开发百科rates
---------
<script language="text/javascript">
$.get('/page2.php', { data_set : 1 });
</script>

page2.php
---------
var options = get_options_from_page_1( $_GET['data_set'] );

Make sense I hope? Basically I want to pass a PHP array from page1 --> page2 and am looking for a clean mechanism to do so. I know I can drop data in a session var, but that just seems hacky and if I have multiple instances of this plugin on the page, I need to start tracking instance IDs, etc.


This is basically a PHP "problem". Each script is executed in it's own process in your example and these do not share data.

An easy way to do so would be infact using $_SESSION, and you're right you must manage the data on your own. But you must so with any other method (e.g. via the database). I think session is pretty handy then.

Another method would be to encrypt the data, pass it with the request and then decrypt it again.


There are only a few ways to get a variable from one page to pass to another:

  • Form variables -- GET/POST/(and to a lesser extent) PUT/DELETE
  • Cookies
  • Session
  • HTTP Authentication headers (DO NOT USE THESE FOR CONVEYING INFORMATION BETWEEN PAGES. THEY ARE FOR AUTHENTICATION ONLY).

It sounds like you'll either want to use GET (add ?<varname>=<value> (or &<name>=<value> if appending to other variables) to your URL -- it looks like you're close to getting that down) or you'll need to use Cookies or (personally, I think it better) SESSION.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜