Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2004, 03:38 PM
Authorized User
 
Join Date: Jul 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Domain name validation - backwards??

I know almost nothing on PHP, but am I ever learning fast ;)

I have this application that I download. Using my VB skills, I cleaned up all the bugs but one, and for the life of me I can't figure it out. The attached routine is supposed to strip off the .com (or any suffix) from a domain name entered into a search field. Logically (using my VB skills) it looks like it should work; however, it strips off the domain part and leaves the suffix. Backwards from what is intended. Could someone please point out where the logic is skewed??

function Check_Domain_Format()
    {
        if (empty($this->domain)) Error_Handle(LANG_ERR_no_domain);

        while (substr($this->domain, -1) == ".")
        {
            $this->domain = substr($this->domain, 0, -1);
        }

        $tdomain = strrchr($this->domain, ".");
        if($tdomain)
            $this->domain = substr($tdomain, 1);
        $this->domain = ereg_replace("[^a-zA-Z0-9-]*([a-zA-Z0-9-]*)[^a-zA-Z0-9-]*", "\\1", $this->domain);

        if (empty($this->domain)) Error_Handle(LANG_ERR_no_domain);
        if (strlen($this->domain.$this->ext) > 67) Error_Handle(LANG_ERR_too_long);
    }
__________________
I\'m all in favor of keeping dangerous weapons out of the hands of fools. Let\'s start with computers.
 
Old July 19th, 2004, 04:01 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Why don't you just use this?

$domain = explode(".",$this->domain);
$this->domain = $domain[0];

HTH,

Snib

<><
 
Old July 19th, 2004, 04:04 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Oops- messed up in my post, but it's fixed now.

:-)

Snib

<><
 
Old July 19th, 2004, 04:15 PM
Authorized User
 
Join Date: Jul 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank Snib!! :D
That did the trick.

As this is the first time working with PHP I could not see a solution. My forte is VB and ASP. Your solution is MUCH simpler.

Thanks:)
 
Old July 19th, 2004, 09:20 PM
Authorized User
 
Join Date: Jul 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The above solution worked great until a sub domain was added in the search. Doh! So I added the following in front of it.

$cnt = substr_count($this->domain, ".");
if ($cnt > 1)
{
  $domain = explode(".",$this->domain);
  $this->domain = $domain[1];
}

If there is a better way to do this I am still open to suggestions. It works as expected but I can see a problem if someone adds even more subdomains or periods in there, in which case I don't think they should expect realistic results anyway ;)

Thanks again,
 
Old July 19th, 2004, 10:10 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Actually, that looks to be a pretty clean solution! :-)

(Rich might have a better way ;) ).

Snib

<><
 
Old July 20th, 2004, 04:10 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hmmm, well I'm not sure. What exactly is the point of this again??

How do you account for multiple subdomains?

www.one.two.three.mydomain.com

I think I'd take a different approach to it.

I'd build a database of known suffixes, by visiting the ICANN website,
http://www.icann.org
Or by doing a google search
http://www.google.com/search?q=top+l...omain+suffixes

The idea being that since the TLD suffixes can vary, and the URL itself can potentially vary also, this approach leaves less margin for error.

Then I'd build an algorithm that checks the name based on those suffixes.

Then I'd use a package like Net_URL, http://pear.php.net/Net_URL to parse the different parts of the domain. Separate any potential file path, query string, anchor or other parts that can potentially appear in a URL into different member variables of the Net_URL object. Now I have only the host address to deal with, which is availble in Net_URL object as the host member variable.

Code:
require_once(Net/URL.php);
$url =& new Net_URL($domain);

$tld[] = '.com';
$tld[] = '.net';
$tld[] = '.org';
$tld[] = '.co.uk';

// etc

foreach ($tld as $suffix)
{
   // check each suffix against the end of the host name
   if ($suffix == substr($url->host, (0 - strlen($suffix)))) // make that a negative number!
   {
       // The suffix exists in the domain.
       // Remove the suffix from the domain.
       $domain_no_suffix = substr($url->host, 0, (strlen($url->host) - strlen($suffix)));
   }
}
Of course there's more than one way of approaching it and it really depends on your needs. I think this is a cleaner method with less margin for error. Though is does require gathering the possible TLD suffixes. I know it is possible to connect to various whois services inside of PHP, it may be possible that the TLD suffix information can be gathered that way as well.

I didn't test this so I'm not sure if I have logic or parse errors.

HTH!


Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old July 20th, 2004, 04:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But it's worth saying that PHP does have fairly advanced URL parsing functions as a native part of the language, too:
http://uk2.php.net/manual/en/function.parse-url.php
http://uk2.php.net/manual/en/function.pathinfo.php
(parse_url and path_info two very useful functions! Take a look into the likes of realpath(), too.)

(Indeed, part of Net_URL is just an object-oriented wrapper around these native functions).

Take it easy,
Dan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Same pages for sub domain from main domain vivek_inos ASP.NET 1.0 and 1.1 Professional 1 February 13th, 2007 10:15 AM
Backwards compatable XSLT 2.0 boen_robot XSLT 4 October 4th, 2006 03:36 PM
Domain Password Validation Cr0vaX Pro VB 6 3 June 21st, 2004 02:16 PM
Chapter 6 Array's "Going backwards" mjennings VB.NET 2002/2003 Basics 2 November 19th, 2003 02:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.