I have a php
script the generates links where-by the URL
is very long (too long for IE to handle infact) such as www.somesite.co.uk/here/query?foo=bar&bar=foo&.....
where somesite.co.uk
gets
the values (which I can't change to post
).
So in my index.php
:
echo "<a href='$url'>".$link."</a>";
All works fine for values of $url
that are under the limit the page is seen as expected however if over the limit the data on the page is truncated.
I created this long_url_test.php
script and it's loads the page as expected for long values of $url
<?php
header('Location: $url');
?>
I need help putting this all together, something like:
index.php:
echo "<a href='long_url_test.php'>".$link."</a>"; # and POST $url to script
long_url_test.php:
<?php
$url=$_POST['url']
header('Location: $url');
?>
Where the link displayed on index.php
posts
the url to long_url_test.php
which fetches the actual required page, or indeed is there a better/easier way I should do this?
Why are you generating such ridiculously long URLs? At this point you're clearly missing out on a better solution like sessions.
If we just want to slap a bandaid on the solution:
- Write a form to the page with a squillion hidden fields with your data in it, then make the link POST the form.
- The same as #1, but with javascript.
Slightly better: Take whatever data you want to pass and run it through base64_encode(serialize($your_data))
and see if you can embed that in the URL without running into the length limit. If you have Zlib installed/enabled you can throw a gzcompress()
in there too.
Still, I have a very hard time imagining that there is not a far simpler/better solution where your data stays server-side.
Not clear how Header('Location:')
works with long urls (possibly - IE miss to test length of URLs recevied with location header?), but once you really need those URLs with all parameters (so they could be shared by people) I'm afraid you will need to use some kind of URL shortening. For instance, create unique ID for each set of parameters and save real params into database. Once you receive a get request - take unique ID and request params from database. Otherwise someone may copy URL (which worked fine because of Location header) and share it with another person. long_url_test.php
wont be executed and IE will fail to open such URL.
Once you do not really need that url to be available for sharing - than just simply replace it with form:
echo "<form action=\"url without params\" method=\"POST\"><a href='#' onclick=\"this.parentNode.submit(); return false;\">".$link."</a>
<input type=\"hidden\" name=\"param1\" value=\"".$val1."\" />
</form>";
You can make the link POST instead of submit.
<a href="#" onclick="document.myform.submit()">Click me</a>
<form name="myform" action="/here/query">
<input type="hidden" name="foo" value="bar">
<input type="hidden" name="bar" value="foo">
</form>
Or if you need it to be a GET. Not sure if this will work for long URLs, though.
<a href="#" onclick="location.href='/here/query&foo=bar&bar=foo'">Click me</a>
Or you can try jQuery and Ajax. I am unsure if this will work either.
<a href="#" id="mylink">Click me</a>
<script>
$(document).ready(function() {
$("#mylink").click(function() {
$.ajax('/here/query&foo=bar&bar=foo').done(function(result) {
//do something
});
});
});
</script>
By the way, some browsers, including versions of IE, have a 2000 URL length limit.