-3

I will validate this URL with an email address inside.

These two domains are allowed:

  • https://www.example.com/secure/index.php?ID=john@example.com
  • https://www.example.com/secure/index.php?ID=john@example-test.com

All names before the @ in the email address allowed.

When the user inserts another domain after the @, like this:

https://www.example.com/secure/index.php?ID=john@gmail.com

they will get an error. How can I do this?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 1) please explain in more detail what are you trying to achieve. 2) add the code you have tried or at least an approach that you have tried – Auris Apr 19 '17 at 13:08

3 Answers3

0

Try this:

$email = $_GET['ID']; // remember to filter this!
$regex = '#\w+@(?<domain>\w+\-?\w+\.\w+)#';

preg_match($regex, $email, $matches);
$domain = $matches['domain'];

if ($domain !== 'example-test.com') {
    // Unauthorised
}

See a working example here https://3v4l.org/SorhQ See the regex and tweak if required here https://regex101.com/r/uDzOzm/1/

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

You can use the simple explode method to extract the domain name. see the code.

$parts = explode("@", "johndoe@domain.com");
$domain = $parts[1];
if(!in_array($domain, array('domain.com')))
{
    //Redirect it wherever you want
}
Agam Banga
  • 2,708
  • 1
  • 11
  • 18
0

You can do it:

if (isset($_GET['ID'])) {        
    $domain_name = substr(strrchr($_GET['ID'], "@"), 1);        
    if ($domain_name != "example-test.com"){
        Forbidden....
    }
}