10366

Dealing with long URLs

Question:

I have a php script the generates links where-by the URL is very long (too long for IE to <a href="http://support.microsoft.com/kb/208427" rel="nofollow">handle</a> infact) such as www.somesite.co.uk/here/query?foo=bar&bar=foo&..... where somesite.co.uk gets the values <em>(which I can't change to post).</em>

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 <em>better/easier</em> way I should do this?

Answer1:

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:

<ol><li>Write a form to the page with a squillion hidden fields with your data in it, then make the link POST the form.</li> <li>The same as #1, but with javascript.</li> </ol>

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 <em>very</em> hard time imagining that there is not a far simpler/better solution where your data stays server-side.

Answer2:

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>";

Answer3:

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 <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">Ajax</a>. 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.

Recommend

  • String values look the same but don't “.equals()” each other
  • Batch For loop.. wildcards to determine length of searched files?
  • asp.net mvc user permissions and views
  • Desktop SPARQL client for Jena (TDB)?
  • Mystery issue with GIF upload?
  • | operator operation in linq?
  • C++: instantiate a templated class according to an argument
  • Using third party libraries
  • Issue with session.handler.native_file session handler in symfony2
  • PHP Post & Redirect with cURL Same As HTML Form [closed]
  • Django how to modify database records by template
  • IP and domain create different session
  • Can I put a + sign in a folder with IIS?
  • Exception handling as per java coding standards
  • jQuery and Uploadify session in the php file
  • Open hyperlink on click on an ggplot/plotly chart
  • Symfony2 redirect to https route fails (uses wrong port)
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • Skip Characters in Oracle TO_DATE function
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • Does Mobilefirst provide a provision to access web services directly?
  • uniform generation of points on 3D box
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Android screen density dpi vs ppi
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • How would I use PHP exceptions to define a redirect?
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • How to extract text from Word files using C#?
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Why joiner is not used after Sequence generator or Update statergy
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • need help with bizarre java.net.HttpURLConnection behavior
  • File not found error Google Drive API
  • Is it possible to post an object from jquery to bottle.py?
  • Does armcc optimizes non-volatile variables with -O0?