Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP Arrays passed via URL

PHP Arrays passed via URL
Thread Tools
ppmax
Dedicated MacNNer
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jan 22, 2004, 03:42 PM
 
This is a really stoopid question but I'm having a mjor brain lock right now.

I have an array ($files) that contains the set of file names in a particular directory. there are 14 items in this array.

I want to pass this array to another php page (filelist.php). The code for my url:
a href="bla.com/filelist.php?files=$files"

when filelist.php loads i do:
echo count($_GET[files]);

...and for some reason it's returning 1 instead of 14. also, i try to iterate thru the array and echo the filenames--but no dice.

when i mouse over the link it says:
http://www.bla.com/filelist.php?filelist=Array

what am I doing wrong?? passing arrays is just like passing any other variable right?

thx

ppmax
( Last edited by ppmax; Jan 22, 2004 at 03:47 PM. )
     
skalie
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Jan 22, 2004, 03:47 PM
 
At the risk of being totally innaccurate (again).......

Arrays I've dealt with needed to be "imploded" and then "exploded" to be shuffled along.
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jan 23, 2004, 06:51 AM
 
Originally posted by ppmax:
This is a really stoopid question but I'm having a mjor brain lock right now.

I have an array ($files) that contains the set of file names in a particular directory. there are 14 items in this array.

I want to pass this array to another php page (filelist.php). The code for my url:
a href="bla.com/filelist.php?files=$files"

when filelist.php loads i do:
echo count($_GET[files]);

...and for some reason it's returning 1 instead of 14. also, i try to iterate thru the array and echo the filenames--but no dice.

when i mouse over the link it says:
http://www.bla.com/filelist.php?filelist=Array

what am I doing wrong?? passing arrays is just like passing any other variable right?

thx

ppmax
Nope. Passing arrays via a request variable requires you to 'pack' it first, then 'unpack' it when you load the next page. Arrays can't be described by request variables unless you manually break them down into their component form before you send the form (e.g. < input name="form[files][a]" value="filea" /><input name="form[files][b]" value="fileb" />... etc...)

The 'serialize' and 'unserialize' functions are what you need if you want a simple way or utilising GET request variables. Have a squiz on php.net for a quick tutorial.

Something like:-

[php]

$file = serialize($file);

$link = "http://www.server.com/index.php?filelist=".urlencode($file)."";

[/php]

and on the receiving page...

[php]

$file = $_REQUEST['filelist'];
$file = unserialize($file);
if (!is_array($file) || empty($file)) { die("Not valid"); }

Your code...

[/php]

Does this help?
Computer thez nohhh...
     
ppmax  (op)
Dedicated MacNNer
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jan 23, 2004, 02:40 PM
 
Thanks for the reply--Ill give that a go.

It's also been suggested that I store the contents of the $files array as part of a session. So I would just start a session and then define $_SESSION['$files'] and then call it later whenever I need it.

Do you see any probs with that approach? I dont expect the array $files to change during the session...
     
Mithras
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Jan 24, 2004, 02:07 PM
 
Also be careful to check that your code is not a security risk -- could other files be read or modified if someone tinkers with the URL?
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:01 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,