1

I have a need where I go to a given page, log the user on if authenticated, and then redirect to another page. My question revolves around including the url to the other page in the url which goes to the first page.

I have a link which looks like index.php?pg=page1&controller=default&data=123&nextPg=xxx.

While I showed nextPg as xxx, I want it to be pg=page2&controller=whatEver&data=432.

I obviously can't do this as the names are duplicated. I could change the names of the second URL or make the first and second ones arrays, however, would rather not. Ideally, when getting to the first page, my $_GET could be converted to something like the following:

array(
  'pg'=>'page1',
  'controller'=>'default',
  'data'=>123,
  'nextPg'=array('pg'=>'page2','controller'=>'whatEver','data'=>321)
)

How is this best accomplished?

user1032531
  • 24,767
  • 68
  • 217
  • 387
  • Do you have a listener if someone is logged in, and redirects to login if they're not, then sends them on their way? If that's the case you could use `$_SERVER['HTTP_REFERER']`. If not and you just want another `$_GET` string stored, your exact example should be fine (as a string or json). – thepratt Aug 03 '14 at 16:12
  • @nuc. Some pages they don't need to be logged on to view, others they do. If they go to a restricted page when not logged on, it will redirect to the hope page. I don't understand how that makes a difference. Please elaborate on your comment. Thanks – user1032531 Aug 03 '14 at 16:34

1 Answers1

1

One option is to store them in a session. Check the concept of flash messages in you want to store them in the session only for the next request.

If you really need to pass this information via the URL, then you can do the following.

Assuming that you have the data in an array, you can first serialise the array, and then base64_encode it, and then pass this value to the URL:

$nextPg_data = array('pg'=>'page2','controller'=>'whatEver','data'=>321);
$nextPg = base64_encode(serialize($nextPg_data));
echo '<a href="index.php?pg=page1&controller=default&data=123&nextPg=' . $nextPg . '">link</a>';

Then, you can retrieve, decode, and unserialise the value in the next page, and you'll get the same array:

$nextPg = unserialize(base64_decode($_GET['nextPg']));

var_dump($nextPg); produces:

array(3) {
  ["pg"]=>
  string(5) "page2"
  ["controller"]=>
  string(8) "whatEver"
  ["data"]=>
  int(321)
}

By the way, if you decide to use the above method, try it with some real data to make sure you won't exceed any characters limit.

Community
  • 1
  • 1
StathisG
  • 1,159
  • 1
  • 10
  • 18
  • Humm, a session. I suppose that would work. Also, never heard about flash sessions til now. I will check them out. In regards to your serial encoded solution, makes sense. Could a similar solution but using json work? – user1032531 Aug 03 '14 at 16:38
  • @user1032531 I meant "flash messages" (session-based) instead of "flash sessions" (I just edited my answer), a concept which I have seen implemented in most (if not all) of the frameworks I've used. Regarding your json question, I don't see how it could it be different than the array you already have. – StathisG Aug 03 '14 at 17:27
  • Didn't know about "flash messages" either. Before seeing your edited post, http://sureshdotariya.blogspot.in/2014/01/implement-flash-messages-using-session.html is one of the few that came up for "flash sessions" Seems reasonable, no? – user1032531 Aug 03 '14 at 17:46
  • @user1032531 I didn't read the code in detail, but it seems that it is what I meant. Here's is another example: http://mikeeverhart.net/php/session-based-flash-messages/ – StathisG Aug 03 '14 at 18:04