2

The email verification link I create to validate a user is as follows:

site_url/user@example.com/hash_to_verify

I'm trying to route a URL like the one above to my activate_user method in my registration controller. I tried using the same regex used in the valid_email method in CodeIgniter, but it doesn't seem to work (doesn't route to activate_user method):

$route['registration/(/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix)/(:any)'] = 'registration/activate_user/$1';

1) Where did I go wrong in my route?

2) Is formatting my URL this way acceptable to verify emails?

Thanks

kittycat
  • 14,983
  • 9
  • 55
  • 80
massaskillz
  • 283
  • 3
  • 10

1 Answers1

2
$route['registration/(/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix)/(:any)']

should be:

$route['registration/([\w+-]+)(\.[\w+-]+)*@([a-zA-Z\d-]+\.)+[a-zA-Z]{2,6}/(:any)']

The entire string is a regex without delimiters or modifiers. You were putting delimiters, modifiers and were also using ^ and $.

I simplified the regex, however email validation should not be done via URL. Instead your controller should be checking that segment and performing validation on it. If it does not match redirect to re-register or output error message, otherwise continue on with registration.

Btw don't re-invent the wheel when validating an email address. PHP has built-in validation via the filter_var() function.

Example:

$email = 'joe@example.com';

if (filter_var($email, FILTER_VALIDATE_EMAIL)) 
{
    echo 'valid';
}
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • Also, I think you misunderstood me on the validating email question. I was following this: http://stackoverflow.com/questions/3794959/easiest-way-for-php-email-verification-link , but since CodeIgniter doesn't use GET by default, I'm simulating the "..php?/email=$email" parameter as "site_url/user@example.com/..." I was wondering if this is okay to do. – massaskillz Feb 14 '13 at 22:17
  • Found something else. I had to put additional ( ) around ([\w+-]+)(\.[\w+-]+)*@([a-zA-Z\d-]+\.)+[a-zA-Z]{2,6} to get the full email to pass into the $1 variable. – massaskillz Feb 15 '13 at 14:53