tags:

views:

257

answers:

5

I would like to pass a long string to a second page. Normally I pass short strings as variables

www.example.php?var=something&var2=somethingelse

In this case tho I would like to pass a long sentence to a second page and not have to replace all the white spaces with dashes deal with commas and apostrophes.

Is there a simple way to do this that Im missing? Do I have to re-query the database?

Thanks

+3  A: 

base 64 encode it, and then decode again when the other page receives the GET param

$str = "I'm an obnoxious string, meh meh"
$str = base64_encode($str);

** Echo link **

Then on the receiving page

$str = base64_decode($_GET['str']);
code_burgar
very cool, I've never heard of those functions before.thanks
chris
Just to clarify: This method is not secure. It will not make the text secret or shield it from tampering. Its not an encryption, and just adds extra layers compared to urlencode.
OIS
+1  A: 

use $_SESSION array

Ilya Biryukov
Would this method be lighter or more secure then the base64 method?
chris
$_SESSION is the same for all pages, it times out and is different from different browsers (use webpage at work, get link on email, try it from home). At least you would need to send a key along with the link. So SESSION is an alternative to other server storage methods like db and file.
OIS
@chris base64 is not secure in any way
OIS
+2  A: 

Use urlencode or http_build_query. No need to decode the message then.

base64 is not an encryption, and just about every programmer can spot it and convert it. You should not relay on that as some secure method of sending message from page to page.

If you just want it to be untampered with, you can also send along a hash of the string, with a secret salt for your server.

$salt = 'longsecretconstanttexthere'; 
$url .= '?' . http_build_query(array('text' => $str, 'hash' => md5($str . $salt));

Reciveing end:

$str = $_GET['text'];
if ($_GET['hash'] !== md5($str . $salt)) {
  //not the original string
}

For a secret method, store the text in $_SESSION or db with a key. Send that key to the next page.

OIS
This is an option, however there are plenty of situations in which having the raw and still meaningful string in the url is not the best option possible.
code_burgar
@codeburger: in that situation its not different from base64 encode.
OIS
+2  A: 

There is no problem in passing long values, that's what the urlencode() function is for:

$link = 'www.example.com?longValue=' . urlencode($arbitraryLengthString);

And you don't need to decode it manually, that's done automatically by PHP. But if it's a really long value that you intend to use on many pages, it might be better to put it into a session.

soulmerge
This is the same as my answer, minus http_build_query ....
OIS
Yes, but your answer was *really* short at the time I posted this.
soulmerge
+1  A: 

Depending on the length of the string in question, you may want to look at the database method, sessions or using POST to send your data instead of GET.

GET has limits and is typically is not meant to pass large amounts of data, you may end up running into situations where your string becomes truncated if it's long enough.

Crazy Joe Malloy
thanks for the tips
chris
no problem, it's little things like this that seem to slip through the cracks most times it seems, you don't get to find them out until you're in the nitty gritty of something - where, all of a sudden these distinctions "matter".
Crazy Joe Malloy