Solved

Query String and Array

Posted on 2002-07-23
15
437 Views
Last Modified: 2008-02-01
Is it possible to send an array as a query string (serialize, then urlencode), and also attach an element to it while actually sending it as an <a href> tag?
0
Comment
Question by:junkyboy
  • 9
  • 6
15 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 7174045
<?php

foreach($HTTP_POST_VARS as $k => $v)
     {
     echo "$k = $v<br>";
     }
     
$s = serialize($HTTP_POST_VARS);

echo "Serialized = $s<br>";

$e = urlencode($s);

echo "URLEncoded = $e<br>";

echo "<br><br><a href=\"SomePlace.php?$e\">Someplace to decode this stuff</a><br><br>";

$d = urldecode($e);

echo "$d<br>";

$a = unserialize($d);

foreach($a as $k => $v)
     {
     echo "$k = $v<br>";
     }
     
?>

0
 

Author Comment

by:junkyboy
ID: 7174662
Maybe my wording wasn't quite good.  What I have is this:
<?
$param = array( USER=>"a1", PASS=>"a2", IP=>"a3", );
?>
<a href="test.php?<?echo "param=".urlencode(serialize($param))."&param[PAGE]=".urlencode(serialize("2"));?>">test</a>
?>

Whenever I do this, the $param array gets overwritten, and it only contains the PAGE key in it.  The USER, PASS, and IP keys are destroyed.  Is there anyway I can add it while I pass the <a> tag?  What I ultimately want to do is do:

next page:     ..."&param[PAGE]=".urlencode(serialize($param[PAGE]+1))...
previous page: ..."&param[PAGE]=".urlencode(serialize($param[PAGE]-1))...
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7174725
Assuming you have the current page number,

$iNextPage = $iPage++;

and

$iPrevPage = $iPage--;

So you can ...

<a href="test.php?<?echo "param=".urlencode(serialize($param))."&Page=".urlencode(serialize($iNextPage));?>">test</a>

sort of thing.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7174727
Sorry.

<a href="test.php?<?echo "param=".urlencode(serialize($param))."&iPage=".urlencode(serialize($iNextPage));?>">test</a>

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7174737
I would suggest using sessions for the username work and NOT putting username/passwords in the URL. These are EASY to spot as the urlencode will become plain text in IE6 on the status bar and in some cache readers.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7174742
<?
$param = array( USER=>"a1", PASS=>"a2", IP=>"a3", );
$param["PAGE"] = 3;
$next = urlencode(serialize($param));
$param["PAGE"] = 1;
$prev = urlencode(serialize($param));

echo <<<END_HTML
<a href="test.php?param=$prev">prev</a>
<a href="test.php?param=$next">next</a>
END_HTML;

?>

May be better to your needs!

Richard.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7174847
Slight update ...

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );
$param["PAGE"] = 3;
$next = urlencode(serialize($param));
$param["PAGE"] = 1;
$prev = urlencode(serialize($param));

echo <<<END_HTML
<a href="test.php?param=$prev">prev - $prev</a>
<a href="test.php?param=$next">next - $next</a>
END_HTML;

echo "When I put the mouse over these links, the status bar shows ...<br>" . urldecode($next) . "<br>and<br>" . urldecode($prev) ."<br>";
?>

I was getting errors on USER rather than "USER".

Also added a note about the mouseover.

Richard.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:junkyboy
ID: 7176081
So, there's no direct way of doing it within an <A> tag?
0
 

Author Comment

by:junkyboy
ID: 7176144
So, there's no direct way of doing it within an <A> tag?
0
 

Author Comment

by:junkyboy
ID: 7176592
So, there's no direct way of doing it within an <A> tag?
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7176618
You can simply break in and out of PHP ...

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );
?>
<a href="test.php?param=<?php $param["PAGE"] = 1; echo urlencode(serialize($param)); ?>">Previous</a>
<a href="test.php?param=<?php $param["PAGE"] = 3; echo urlencode(serialize($param)); ?>">Next</a>

So yes, but it is a little harder to read.

0
 

Author Comment

by:junkyboy
ID: 7184412
That's going to make PAGE = 3 because it goes through the whole script..
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7184739
You would have a local page variable surely?

Let's call it $CurrentPage.

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );

?>
<a href="test.php?param=<?php $param["PAGE"] = $CurrentPage - 1; echo urlencode(serialize($param)); ?>">Previous</a>
<a href="test.php?param=<?php $param["PAGE"] = $CurrentPage + 1; echo urlencode(serialize($param)); ?>">Next</a>

?>

But remember, HTML and PHP are read at different times.

The PHP happens on the server and can produce HTML.

The HTML (entered in the file or created by PHP) is read by the browser.

Doing things inline like this are OK, but they DO make it a little harder to read.

You could (and probably should) actually use the Page parameter (me being stupido!) ...

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );

?>
<a href="test.php?param=<?php $param["PAGE"]--; echo urlencode(serialize($param)); ?>">Previous</a>
<a href="test.php?param=<?php $param["PAGE"]++; echo urlencode(serialize($param)); ?>">Next</a>

?>

But you have to watch out for being on Page 1 and having a link to Page 0.

I would recommend ...

<?php

$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );

if ($param["Page"] > 1)
 {
 $param["PAGE"]--;
 echo '<a href="test.php?param=' . urlencode(serialize($param)) .'">Previous</a>';
 $param["PAGE"]++;
 }

if ($param["Page"] < $MaxPages)
 {
 $param["PAGE"]++;
 echo '<a href="test.php?param=' . urlencode(serialize($param)) .'">Next</a>';
 $param["PAGE"]--;
 }

?>

Richard.
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 150 total points
ID: 7189071
If you already have a current page variable, then ...

$param["Page"] = $currentPage + 1;

and

$param["Page"] = $currentPage - 1;

would be better.

Richard.

P.S. I put "PAGE" instead of "Page" in my previous reply. Oops!
0
 

Author Comment

by:junkyboy
ID: 7283308
Sorry for the delay - was out of town a bit.  I don't know why I didn't think of something like that...  Thanks though.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now