I don't know exactly how long your hash is, but not all services (browsers, servers etc) can handle URLs longer than 255 chars. You could look into php's Pack()
You might want to use urlencode() for the url
parameter.
It is also recommendable to check fgets()
against false
. Then, you could save the empty()
function call by just comparing the response to an empty string, like:
$line = fgets($fp);
if ($line !== false && $line !== '') {
// ...
}
Generally, it is advisable to check everything against false
first, if the function returns values of different types such as integer
or boolean
. This can be crucial because 0
and false
mean in comparisons the same. Because of PHP's lack for type safety, it is strongly recommended to always check for type equality. There are even cases when the documentation recommends this explicitly, e.g. in the case of strpos(). Meanwhile, I've forced myself to use ===
over to ==
, same for `!=' etc. It requires more typing but it is definitely worth the effort because you eliminate possible pitfalls.
The rest of your code looks good to me.
What is the point of using a "super-long hash", if you are immediately shortening it to a 7-8 character tinyurl?
Nobody would bother with guessing the long hash, and would crack the tinyurl instead.
Use a 10-character hash yourself and be more secure than you are now.