maximum URI length for file_get_contents() - php

Is there a maximum length for the URI in the file_get_contents() function in PHP?

I suppose there is a maximum length, but you'll be hard pressed to find it. If you do hit the maximum, you're doing something wrong. :)
I haven't been able to find a number for PHP specifically, but MS IIS, Apache and the Perl HTTP::Daemon seem to have limits between 4,000 and 16,384 bytes, PHP will probably be somewhere around there as well.
What you need to consider is not really how much your side can handle, but also how much the other server you're querying can handle (which is presumably what you're doing). As such, any URL longer than ~1000 characters is usually already way too long and never really encountered in the real world.

As others have stated, it is most likely limited by the HTTP protocol.
You can view this answer for more info on that : What is the maximum length of an url?

In HTTP there's no length-limit for URI,and there's no note of file_get_contents() about this in the manual .So I think you needn't to consider about this problem.
BTW,the length of URI is limited by some browser and webserver.For example,in IE,the length should be less than 2083 and in FF it's 65,536.I tried to test this I found that only not more than 8182 is OK when I visited my apache on ubuntu because of limit of my apache.

Related

PHP_NORMAL_READ cutting off the data being sent

I need some help please,I have problem in reading the binary data that was sent by the device via socket.I could not receive the exact data that was sent. I am using this code
$data = #socket_read($read_sock,2048,PHP_NORMAL_READ);
I am using PHP_NORMAL_READ because it will stop reading with this "\r\n".
but when I receive,the data is not exact it only receive few binary data.
The length parameter specifies the maximum length that will be read from the stream. The PHP documentation is a bit misguiding on this subject, but what I think it means is, that you will get:
less than or exactly 'length' bytes
at least one byte
no '\r' or '\n' in the response, unless it is the only character
Most of the Socket APIs you encounter work this way, they may give you less bytes than requested, because more bytes may not be available and the data may arrive in smaller parts than that the device sent them in. The solution is to read from the socket repeatedly, until you get what you want (that means until you get string ending with newline, in your case).
You also may want to consult http://php.net/manual/en/function.socket-read.php, where the commenters suggest the functions is somewhat buggy when used with PHP_NORMAL_READ. It might be worth searching for some socket library for PHP that supports readLine.

Max Length For Get Variable

I was wondering if there is a max length on a $_GET variable. I plan on using ajax with a get command and part of it is an encoding of an access key using one of my encoding methods. This method has returned back roughly 1530 characters and I was wondering if this it too long for a get variable as long as it's all URL encoded?
Thanks in advance,
Spencer
Browser would greatly affect the max length of your $_GET param
MAXIMUM LENGTH FOR BROWSERS REFERENCE
Some versions of PHP have a limitation of length of GET params:
PHP.NET REFERENCE
Max URL length is around 2000 charactors
In IE is around 2048 - http://support.microsoft.com/kb/208427
Similar to this? https://stackoverflow.com/a/7725515/2827152
Please note that PHP setups with the suhosin patch installed will have a default limit of 512
characters for get parameters. Although bad practice, most browsers (including IE) supports URLs up
to around 2000 characters, while Apache has a default of 8000.
To add support for long parameters with suhosin, add suhosin.get.max_value_length = in php.ini
Source: http://www.php.net/manual/en/reserved.variables.get.php#101469

Generate the same MD5 using javascript and PHP

I am trying to build an application that needs to compare the MD5 hash of any file.
Due to specific issues, before the upload, the MD5 must be generated client side, and after the upload the application needs to check it server side.
My first approach was to use, at the client side, the JavaScript File API and the FileReader.ReadAs functions. Then I use the MD5 algorithm found here: http://pajhome.org.uk/crypt/md5/
Server side, I would use PHP's fopen command and the md5 function.
This approach works fine when using simple text files. But, when a binary file is used (like some jpg or pdf), the MD5 generated at the client side is different from the server. Using md5sum command-line tool I figured out that the server MD5 is correct and the problem occurs at client side.
I've tried other MD5 API's I found with the same results. I suspect that FileReader.ReadAs functions are loading the file content slightly differently (I have tried all ReadAs function variants: text, binary and so on), but I can't figure out what is the difference.
I'm missing something but don't know what, maybe I need to decode the content somehow before generating the MD5.
Any tips?
Edit 1:
I followed the idea given by optima1. Took each character and printed the unicode number both on javascript and PHP. I could see only one difference at the end on all the cases (used vimdiff).
PHP: 54 51 10 37 37 69 79 70 0
Javascript: 54 51 10 37 37 69 79 70
Maybe this extra zero at PHP is some kind of "string end". On both cases the binary strings have the same length. Adding a String.fromCharCode(0) to the end of the JS content do not solve the problem. I will keep investigating.
If i can't find a solution i will try to build a giant string by concatenating those charcodes and using it to build the MD5. It is a crap solution but will serve for now and i will just need to add a zero to the end of the JS string...
Edit 2:
Thank God! This implementantion works like a charm: http://www.myersdaily.org/joseph/javascript/md5.js
If you need to generate a MD5 hash from binary files, go for it.
Thanks in advance!
http://membres-liglab.imag.fr/donsez/cours/exemplescourstechnoweb/js_securehash/
javascript md5 and php md5 both are same but we need to use some functions...that functions we can get from above url....
I would suggest doing a quick sanity check: have your client-side code report the first and last bytes of the binary data. Repeat in your PHP code. Compare first and last bytes from both methods to ensure that they are in fact reading the same data (which should result in the same MD5 hash.)
Then I would suggest posting code here so that we can review.

What is the maximum URI length in codeigniter?

I'm wandering what the maximum URI length is in codeigniter, and if URI segments being used as arguments to a controller function count towards the browsers GET length limit? I think most browsers cap there GET parameter length to about 2000?
Currently if my total URI length (inc. https://domain/folder/controller/function/argument) exceeds around 1560 characters I get a forbidden message.
'Forbidden You don't have permission to access /folder/controller/function/argument on this server'
If I trim the characters back to under around 1550~1560 it works fine again. I realise 1500+ is alot anyway, which is why I was wandering if URI counts towards the GET limit.
Has anyone experienced this problem? Is there a solution aside from POSTing all data?
BTW: I'm using the URI protocol AUTO in the config
As far as I remember the whole URI is limited to a more or less specific length. Something is already mentioned here: What is the maximum length of a URL in different browsers?
However, it feels a little bit curious, that you require such long uris. If you append a query string of around 1000 characters length, thats already 1kB of data. In my oppinion a query string is not the right place to transport data around.

How much data can be sent via $_GET

How much data can be sent via $_GET in PHP5? Is there a maximum number of variables, string length etc? Thanks in advance.
Although the specification of the HTTP protocol does not specify any maximum length, practical limits are imposed by web browser and server software.
There is no limit defined in the RFC, but browsers limit the URL length (including get variables). IE for instance limits the URL length to 2083 characters, Opera about 4,050, Netscape 6 about 2,000 characters.
A general rule of thumb is, that you shouldnt use more than 256 characters.
There is not only the PHP limitation, but you should also consider 'in between' proxies and the client software.
The http standard doesn't pose a limitation, though.
(I got this from here, where advice is to not exceed 255 char's urls!)

Categories