Re: www. not working

From: Date: Sat, 16 Feb 2008 09:29:08 +0000
Subject: Re: www. not working
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-269403@lists.php.net to get a copy of this message
Jim Lucas wrote:
Valedol wrote:
On Fri, 15 Feb 2008 23:46:57 +0300, nihilism machine <nihilismmachine@gmail.com> wrote:
this still does not work, if a domain has no preceeding www. it redirects to http://www.www.site.com, if it has a www. it goes to www.www.mydomain.com, any ideas? <?php class URL {
    // Public Variables
    public $ServerName;
    public $WWW;
        // Public Functions
        public function __construct() {
        $this->checkWWW();
        $this->ServerName = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    }
        // Check if site is preceeded by 'WWW'
    public function checkWWW() {
        $myDomain = $_SERVER['SERVER_NAME'];
        $FindWWW = 'www.';
        $POS = strpos($myDomain, $FindWWW);
        if ($POS === 1) {
            $this->WWW = true;
        } else {
            $this->WWW = false;
        }
    }
    // Redirect to WWW
    public function WWWRedirect() {
        if ($this->WWW == false) {
            $redir = "Location: http://www." . $this->ServerName;
            header($redir);
               }
       }
} $myURL = new URL(); $myURL->WWWRedirect(); ?>
just try "if ($POS !== 0) {"
well, that said, isn't the first position in a string 0 ? So, in the above example the OP would need // This would mean that www. was found any where in the string if ( $POS ) or // Would mean that www. was found at the beginning of the string if ( $POS === 0 )
Remember that in PHP land $var = 0 evaluates to FALSE. So strpos('foo', 'f') will return 0 (first char), which in an if() expression would turn to false: if(strpos('foo', 'f')) { echo 'f found in foo'; } else { echo 'f not found in foo, or maybe it is the first character; we can not tell'; } That's the reason people use strpos('foo', 'f) !== false since that differentiates the false result (nothing found) from an integer result (for the position it was found on). - Tul

« previous php.general (#269403) next »