Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long can a URL query argument be?

http://site.com/?status=4387hg843hg89473gh87934h89g734hg8973hg9873hg8973h4987g3h489g7h89h849g5

What's the maximum size of status that PHP can read?

I want to pass error / success messages upon login or signup, and the only way I can do this is by redirecting to a URL and append the messages as arguments to that URL.

like:

<?php

 $errors = array(
   1 => 'Password must have between 6 and 20 characters',
   2 => 'User name must contain only A-Z, a-z and 0-9 characters',
   3 => 'Captcha code does not match',
 );

 $errors =  base64_encode(serialize($errors));

 header("Location: http://www.example.com/?status={$errors}");
 die();

(if you know different ways of doing this please tell me ;)

like image 344
Alex Avatar asked Jul 07 '11 22:07

Alex


People also ask

How long can a URL parameter be?

The official documentation specifies a maximum length of 2048 characters for the <loc> element, which is used to submit URLs: URL of the page. This URL must begin with the protocol (e.g. “http”) and end with a trailing slash if required by the web server. This value must not exceed 2,048 characters.

Is there a maximum URL length?

The maximum length of a URL in the address bar is 2048 characters. However, the successful processing of extra-long links by the browser doesn't mean that a search robot will be able to process them too. The appropriate page URL is 75 characters long.

How long can a URL be API?

The REST API supports Uniform Resource Locators (URLs) with a length of up to 6000 characters. To avoid exceeding this limit, it is important to be aware of URL encoding. Some frameworks and HTTP clients automatically encode URLs.

How many query parameters we can pass with URL?

There can be one or more query parameters in the URL.


2 Answers

According to RFC2616 Section 3.2.1:

The HTTP protocol does not place any a priori limit on the length of a URI. Servers MUST be able to handle the URI of any resource they serve, and SHOULD be able to handle URIs of unbounded length if they provide GET-based forms that could generate such URIs. A server SHOULD return 414 (Request-URI Too Long) status if a URI is longer than the server can handle (see section 10.4.15).

With that said, many browsers don't allow URLs of infinite length. For instance, Internet Explorer has a limit of 2,083 characters.

In your case, I would suggest using a session variable to store the error and once it's been displayed remove it.

The file that produces the errors:

<?php

 $errors = array(
   1 => 'Password must have between 6 and 20 characters',
   2 => 'User name must contain only A-Z, a-z and 0-9 characters',
   3 => 'Captcha code does not match',
 );

 session_start();
 $_SESSION['errors'] = $errors;

 header("Location: http://www.example.com/");
 die();

The other page:

<?php

 session_start();

 if ( ! empty($_SESSION['errors']) )
 { 
   // Do something with the errors.

   // Remove the errors from the session so they don't get displayed again later.
   unset($_SESSION['errors']);
 }
like image 118
Francois Deschenes Avatar answered Oct 08 '22 13:10

Francois Deschenes


This is more dependent on the user's browser than anything else. For example, Internet Explorer does not support URLs that have more than 2083 characters. PHP should be fine handling up to (and well past) that limit. Avoiding base64_encode (which is unnecessary) will help. Use urlencode instead (assuming you're not passing any binary data).

I'm sure people would love to help with different ways of doing this, but you'd have to provide more code.

like image 24
Michael Mior Avatar answered Oct 08 '22 14:10

Michael Mior