Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 12th, 2004, 04:13 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default redirect netscape 4.7 to netscape 7.1

Hi folks,

If someone views a page with netscape 4.7, can i redirect their browser to netscape 7.1 or IE? Is it possible?


Thank you for any input

trang

Reply With Quote
  #2 (permalink)  
Old January 12th, 2004, 04:31 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Man I wish!

Nope! Not possible.
You can suggest it to the user though... by finding the browser in the $_SERVER["HTTP_USER_AGENT"] variable... if it is set!

The following is what I use, but modify it to your liking.

Code:
        function browser()
        {            
            if (isset($_SERVER["HTTP_USER_AGENT"])) 
            {
                if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera 3"))                 $browser = "opera";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera 4"))            $browser = "opera";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera 5"))            $browser = "opera";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera 6"))            $browser = "opera";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera/6"))            $browser = "opera";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera"))             $browser = "opera";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "MSIE 6"))            $browser = "ie";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "MSIE 5"))             $browser = "ie";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "MSIE 4"))            $browser = "ie";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "MSIE 3"))             $browser = "safe";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "MSIE"))                 $browser = "ie";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Netscape/7"))        $browser = "netscape";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Netscape6"))            $browser = "netscape";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Mozilla/5"))         $browser = "netscape";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Mozilla/4"))            $browser = "safe";
                else if (stristr($_SERVER['HTTP_USER_AGENT'], "Mozilla/3"))            $browser = "safe";
                else                                                                $browser = "ie";    
            }

            else
            {                
                $browser = "ie";        
            }

            return $browser;

        }
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old January 12th, 2004, 06:39 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you

I'll try to tell the users.

trang

Reply With Quote
  #4 (permalink)  
Old January 12th, 2004, 07:11 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Some additional unrequested self-critique..

Based on previous advice I've seen given by Nikolai, the following would be a better approach the the supplied function:

Code:
        function browser()
        {
            if (isset($_SERVER['HTTP_USER_AGENT']))
            {
                switch (true)
                {
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Opera 3')):         return 'opera';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Opera 4')):         return 'opera';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Opera 5')):         return 'opera';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Opera 6')):         return 'opera';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Opera/6')):         return 'opera';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Opera')):           return 'opera';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 6')):          return 'ie';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 5')):          return 'ie';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 4')):          return 'ie';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 3')):          return 'safe';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE')):            return 'ie';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Netscape/7')):      return 'netscape';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Netscape6')):       return 'netscape';
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Mozilla/5')):       return 'netscape';   # Netscape 6 / Mozilla 1
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4')):       return 'safe';       # Netscape 4.x
                    case (stristr($_SERVER['HTTP_USER_AGENT'], 'Mozilla/3')):       return 'safe';       # Netscape 3
                    default:                                                        return 'ie';
                }
            }

            else
            {
                return 'ie';
            }
        }
Don't allocate memory for a variable if you don't have to, use single quotes if complicated string parsing isn't necessary i.e. variable substitution, escaping of special characters...etc. As discussed in: http://www.php.net/manual/en/languag....syntax.single

What do you think Nik? Is the switch statement a better solution in this case, or the if/else if/else chain? Seem identical to me. I think this example is a little better for readability.

Just food for thought!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #5 (permalink)  
Old January 13th, 2004, 03:00 AM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, it looks good, but you're making a whole lotta calls to stristr(), especially if you fall through to the default case.

I'd use a regular expression function to extract the browser name, and switch on that.

function browser()
{
   $agent = isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_AGENT'] : '';

   // Browser name followed by an optional space or forward slash
   // followed by zero or more digits.
   $pattern = "!(Opera|Netscape|MSIE|Mozilla)(\s+|/)?\d*!";

   if (preg_match($pattern, $agent, $matches))
   {
      switch ($matches[0])
      {
         case 'Opera 3': // Fall through
         case 'Opera 4': // Fall through
         case 'Opera 5': // Fall through
         case 'Opera 6': // Fall through
         case 'Opera/6': // Fall through
         case 'Opera':
         {
            $agent = 'opera';
            break;
         }
         case 'Netscape/7': // Fall through
         case 'Netscape6': // Fall through
         case 'Mozilla/5': // Netscape 6 / Mozilla 1
         {
            $agent = 'netscape';
            break;
         }
         case 'Mozilla/4': // Fall through
         case 'Mozilla/3': // Fall through
         case 'MSIE 3':
         {
            $agent = 'safe';
            break;
         }
         case 'MSIE 6': // Fall through
         case 'MSIE 5': // Fall through
         case 'MSIE 4': // Fall through
         case 'MSIE': // Fall through
         default:
         {
             $agent = 'ie';
             break;
         }
      }
   }
   else // no regex matches
   {
       $agent = 'ie';
   }

   return $agent;
}


This code also has the nice property that it fits well in an editor or terminal window -- no line is wider than 80 chars.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #6 (permalink)  
Old January 13th, 2004, 03:02 AM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I should also mention that you can use the get_browser() function for this as well.
  http://www.php.net/get_browser


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #7 (permalink)  
Old January 13th, 2004, 04:06 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

And I thought I was being clever!
You do have quite a talent for simplification.. : )

I didn't know about get_browser, but it seems that for this application, that's a bit of overkill. It returns an object with 19 member variables.

Well it says an object, but they have their example accessing the result of the function with foreach so it must be an array. Strange.

It also requires the browscap php.ini setting set to point to a browscap.ini file.. the former commented out by default and the latter not bundled with php by default.. which is no big deal.. but puts a damper on portability.

Wouldn't it be better to avoid the regular expression until confirmation that the variable exists?

function browser()
{
   if (isset($_SERVER['HTTP_USER_AGENT']))
   {

      // Browser name followed by an optional space or forward slash
      // followed by zero or more digits.

      $pattern = "!(Opera|Netscape|MSIE|Mozilla)(\s+|/)?\d*!";

      if (preg_match($pattern, $_SERVER['HTTP_USER_AGENT'], $matches))
      {
         // blah blah blah

      }

      else
      {
         // default
      }
   }

   else
   {
      // default
   }
}

Thanks Nik!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #8 (permalink)  
Old January 13th, 2004, 04:45 AM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by quesadilla5
Wouldn't it be better to avoid the regular expression until confirmation that the variable exists?
I don't think so, since the regular expression won't take any time to execute since the source string will be empty if $_SERVER['HTTP_USER_AGENT'] is empty.

Basically, we only have point of exit from our function. That is, there is only one return statement.

I fall through all the case statements where you originally returned the same thing for several cases.

Instead of returning the value, I assign the value to the $agent var and break out of the case statement.

The last thing the function does is return this value, which is guaranteed to be set to one of your choices. If the HTTP_USER_AGENT is not set, or if it is set to something not found in the case statements, then the default case will assign the value of "ie" to $agent.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #9 (permalink)  
Old January 13th, 2004, 03:54 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Oh, Ok, I see what you're doing now.
Absolutely brilliant.

I suppose I should rethink my selections. This actually used to work quite well for most of my projects, I implement a stripped down version of my mark-up if the selection was "safe" including deprecated tags/attributes instead of a style sheet, adjust for idiosyncrasies in style sheets if the selection was "opera" or "netscape", but I'm noticing things are looking pretty crappy on IE 5 and earlier.. Ah! What a pain in the..

Alright enough rambling.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #10 (permalink)  
Old January 14th, 2004, 11:43 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hey Nik, I got around to trying out your browser finding function,
After I got it in there I realized something.. when I originally designed my function a couple of years ago, there was a logic in the order of the calls to stristr().

The logic was designed that way because
User agent Opera contains three of the phrases used in other user agents,
Opera 7: contains MSIE 6, Mozilla/4 and Opera/7

MSIE 6: contains both MSIE 6 and Mozilla/4

Netscape 7: contains both Mozilla/5 and Netscape/7

And so on.. the most difficult to detect in all this being Mozilla v1.

So you can see the delimna here!
When I ran your function, of course, MSIE 6 was considered "safe".

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript+netscape sasidhar79 Javascript How-To 1 October 19th, 2004 05:52 AM
Netscape stu9820 HTML Code Clinic 24 May 19th, 2004 01:56 AM
Netscape and activex cirudinezidane Javascript How-To 1 March 11th, 2004 04:34 AM
CSS in Netscape cjo XML 4 October 26th, 2003 05:23 PM



All times are GMT -4. The time now is 05:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.