2

I want to know the way of passing multidimensional in URL with php.
I have an array like this

$number  = $_SESSION["number"];  
$number = $number+1;  
$_SESSION["number"] = $number;  
$_SESSION['count']$number]=array($_POST['buy_app_page'],$_POST['x'],$_POST['y'],$_POST['w'],$_POST['h'],$_POST['selected_values'],$number);
$pixels_detail=$_SESSION['count'];
$pixels_detail=$_SESSION['count'];

I want to pass the session data stored in the $pixels_detail variable to url. I tried to to this but it show a blank parameter without any value in the url.

Actually am storing cart data in an session array and have two buttons when the user done adding products he/she clicks on the continue button this is where I want to the whole session data to be passed to the next page in any way, using url or someother I haven't any idea now!

Please Help.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
Jaguar
  • 403
  • 2
  • 10
  • 22
  • 1
    Be aware that there is a limit to how much data you can pass through `GET`. – Daniel Vandersluis Aug 24 '10 at 17:11
  • @Daniel Vandersluis: If I remember correctly the standards say 512 bytes, but in practice I've never found a limit to it. While I don't wish to encourage people to break standards or anything, I'm curious, have you run into the standard limit actually being respected by any (post-2000) browser? I'm just curious, not picking a fight here. :) – Teekin Aug 24 '10 at 17:17
  • 1
    @Helgi: IE6 has a cap around 256 characters. I haven't found a limit outside of that, but then again I don't try to push the limit too often... – ircmaxell Aug 24 '10 at 17:20
  • 1
    @Helgi I forget the exact numbers, but different browsers implement the limit in different ways. I pretty distinctly remember running into it once in IE6 (released in 2001 ;) ). – Daniel Vandersluis Aug 24 '10 at 17:21

2 Answers2

6

You can pass arrays through URL using the following notation:

somepage.php?testarray[0]=element_one&testarray[1]=element_two

Similarly, you can send multiple arrays like this:

somepage.php?testarray[0][0]=element_one&testarray[0][1]=element_two&testarray[1][0]=element_three&testarray[1][1]=element_four

I tested it locally and it works just fine.

NOTE: Sending lots of content this way is bad practice. I would examine other methods if I were you, that work through POST.

Teekin
  • 12,581
  • 15
  • 55
  • 67
  • I want a clean url like this http://www.mysite.com/somepage.php?req=somesinglevar – Jaguar Aug 24 '10 at 17:18
  • I have not added any action page for this my action page is the page from which am posting data. How can I check and send form data if the continue button is clicked and to next page in one click.? – Jaguar Aug 24 '10 at 17:23
  • I mean if the continue button is clicked the data should be passed to next page using any redirect with all post data otherwise if the add anther button is clicked the session data should be passed to the next page. Thanks a lot please help – Jaguar Aug 24 '10 at 17:25
4

Why do you want to pass data to the URL? Usually storing it in the session is the best way to do it. If you want to pass complex data in the URL you might have a look at the serialize() and unserialize() functions of PHP.

There is also the really nice function http_build_query() that converts complex data. But be aware of the 4096 character limit of a query string. I would really recommend to read the data from the session as far as you don't have any argument against it. You might pass only one parameter with the button and then read the corresponding data from the session.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
2ndkauboy
  • 9,302
  • 3
  • 31
  • 65
  • I did it with serialize and unserialize with php it works but in url it has all the serialized string. Actually what am doing is that I have a page where user comes to select a product there are two buttons e.g. Add another and Continue I have to check if the user just clicked the continue button then the form should take the data to next page in a normal way else if the user clicked and on add another button and added any no of products then it should go to next page with all the data in the session array. Thanks for your quick answer please tell me again. thanks – Jaguar Aug 24 '10 at 17:15
  • (would +1 but out of votes) yeah, this really doesn't make much sense: Sessions exist to persist data across requests. If you want to prevent multiple requests from the same client mixing up your data, put the data into a session variable with a random key, and pass through that key. GET parameters are seriously limited in what they can carry. – Pekka Aug 24 '10 at 17:15
  • 1
    URLs in general have limits as well. IE6 supports up to only 256 characters (Ask me how I found that out ;-) )... Most modern support a lot, but Apache only supports up to [around 8177 bytes](http://stackoverflow.com/questions/1289585/what-is-apaches-maximum-url-length) – ircmaxell Aug 24 '10 at 17:17
  • I didn't completely understand your comment. But as I said, I would just send two diffrent values on the two buttons and than run the action (e.g. adding something to the cart) on the very next page according to the parameter from the button. – 2ndkauboy Aug 24 '10 at 17:20