Reg H
Reg H

Reputation: 285

URL encoding not working properly

I'm writing my first web application (still!), and have a problem activating user accounts.

The way I've got it set up is that the new user fills out a form with name, email address, and all that. They submit it, and my application sends a link to the email address they provide. They click that link, which passes (by way of a GET) a random code and their email address - activating their account.

In order to do some debugging, I thought I'd try this from different email services. Hotmail works, my own email works, but gmail doesn't. If you click on my 'activate' link in gmail, a 404 Not Found error pops up. So I've narrowed the problem down to how it encodes the link.

Here's the link in gmail (which doesn't work):

mywebsite.com/PHP+App+Files/App1/ln_activate.php?x=userx%40gmail.com&y=9c7b35bbdf1b0d24b0eda62f670c1456

When I change that one to this:

mywebsite.com/PHP%20App%20Files/App1/[email protected]&y=9c7b35bbdf1b0d24b0eda62f670c1456

... it does work. I can see what the differences are, but why does gmail do that, and does anyone have any suggestions for a workaround?

Thanks!

Upvotes: 1

Views: 5269

Answers (5)

ZoFreX
ZoFreX

Reputation: 8920

URL encoding in PHP is actually quite complicated.

There are several functions you need to know about in order to properly form a link in HTML: urlencode, rawurlencode, and htmlentities. Here's a rather contrived example of using all three:

<?php
    $query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
    echo '<a href="http://example.com/department_list_script/',
    rawurlencode('sales and marketing/Miami'),
    '?',
    htmlentities($query_string),
    '">';
?>

'; ?>

Your problem is being caused by improper encoding. Hopefully with this example and the PHP docs you can fix it!

Upvotes: 0

Paul Schreiber
Paul Schreiber

Reputation: 12599

I bet the problem is that urlencode() translates spaces to + instead of %20. Call rawurlencode() instead.

Upvotes: 1

Xtophe
Xtophe

Reputation: 2277

I suggest you NOT to use spaces in the name of folders in any webserver, I think this will solve your error 404 problem :

Rename PHP App Files into PHP_App_Files

Also I personnally don't like to use high caps as usually servers make the difference between low caps and high caps. But this is a personal habit.

You can also use urlencode() and urldecode() to process your URL parameters.

Upvotes: 0

Pramendra Gupta
Pramendra Gupta

Reputation: 14873

solution is

$url ="some url";
$encoded = urlencode($url);
$decoded = urldecode($encoded);

This is the perfect solution.

Upvotes: 0

skwigger
skwigger

Reputation: 31

Try base64 encoding the email address for the link in the email. Then, base64 decode it on the page that processes the verification.

Upvotes: 0

Related Questions