www.webdeveloper.com
Results 1 to 15 of 15

Thread: How to block all other browsers except chrome?!

  1. #1
    Join Date
    Feb 2014
    Posts
    9

    How to block all other browsers except chrome?!

    Hello,

    i want to know, how can i block all other browsers on my website except chrome users? I would like to allow just chrome browser users, all other users i would like to redirect to some example link. I think it can be done with javascript, so please provide me some code example, so i can use it on my website.

    Thanks

  2. #2
    Join Date
    Dec 2012
    Posts
    164
    You could check the user agent to work out which browsers being used, but it isn't 100% reliable.

    PHP Code:
    var chrome = /chrome/i.test(navigator.userAgent);
    if(!
    chrome) {
        
    //user probably isn't using chrome


  3. #3
    Join Date
    Feb 2014
    Posts
    9
    I used your code like this:
    Code:
    var chrome = /chrome/i.test(navigator.userAgent); 
    if(!chrome) { 
        window.location = "1st-brskalnik.php"; 
    }
    And looks like it's working, but is there more reliable way to do it?

    Thanks

  4. #4
    Join Date
    Dec 2012
    Posts
    164
    As far as I know checking the user agent is the only way of detecting the browser, it can be spoofed though so people can easily get past the check.

  5. #5
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    It's recommended in the documentation for the navigator browser object to ideally search for feature detection rather than browser detection depending what it is you want to do. Certain browsers do not return information for navigator.vendor, such as IE, making it a little more browser specific.

    Code:
    <script type = "text/javascript">
    	if((navigator.vendor.length > 5) && (navigator.vendor.toLowerCase().indexOf("google inc") > -1)) {
    		window.location.href = "somepage"; // if chrome
    	} else {
    		window.location.href = "someotherpage"; // all other browsers
    	}
    </script>

  6. #6
    Join Date
    Oct 2013
    Posts
    597
    The USER_AGENT can also be checked and acted upon with .htaccess. This method doesn't rely upon someone having JavaScript turned on in their browser. A "belt and suspenders" approach is to use both. But as iBeZi stated, it can still be spoofed.

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Or you could use PHP

    However, you may want to consider that blocking other browsers is a throw back to those users who claim you should get a real web browser and that the person trying to view may not be in a position to install chrome or have the technical ability to do so.

    Restricting user access to a single type of browser is a virtual version of cutting your nose off to spite your face.

    You should also know that if JavaScript is not present like some browsers out in the WWW do not have javascript or understand javascript (yes they do exist) or even turned off would mean that your blocking script would fail.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Quote Originally Posted by Kevin2 View Post
    The USER_AGENT can also be checked and acted upon with .htaccess. This method doesn't rely upon someone having JavaScript turned on in their browser. A "belt and suspenders" approach is to use both. But as iBeZi stated, it can still be spoofed.
    The term kevin2 is "Belts and Braces" approach.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  9. #9
    Join Date
    Oct 2013
    Posts
    597
    Quote Originally Posted by \\.\ View Post
    the term kevin2 is "belts and braces" approach.
    Ha! Good catch!

  10. #10
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Code:
    <span id='needChrome' style='font-weight:bold; color:red'>This site is intended only for the Google Chrome browser. <a href='notforchrome.com'>Click here.</a></span>
    
    <script type='text/javascript'>
    
    if( !window.chrome )
      location.href = 'http://notforchrome.com';
    else
      document.getElementById( 'needChrome' ).style.display = 'none';  
    
    </script>
    The PHP get_browser function may have issues: http://uk1.php.net/get_browser

  11. #11
    Join Date
    Feb 2014
    Posts
    9
    Quote Originally Posted by Error404 View Post
    It's recommended in the documentation for the navigator browser object to ideally search for feature detection rather than browser detection depending what it is you want to do. Certain browsers do not return information for navigator.vendor, such as IE, making it a little more browser specific.

    Code:
    <script type = "text/javascript">
    	if((navigator.vendor.length > 5) && (navigator.vendor.toLowerCase().indexOf("google inc") > -1)) {
    		window.location.href = "somepage"; // if chrome
    	} else {
    		window.location.href = "someotherpage"; // all other browsers
    	}
    </script>
    Thank you, i like your way. But, can u please edit this code like this: if its chrome just allow access, if its all other browsers then redirect to someotherpage?

  12. #12
    Join Date
    Feb 2014
    Posts
    9
    I did it this way:
    Code:
    <script type = "text/javascript">
    	if((navigator.vendor.length > 5) && (navigator.vendor.toLowerCase().indexOf("google inc") > -1)) {
    		
    	} else {
    		window.location.href = "1st-brskalnik.php"; // all other browsers
    	}
    </script>
    Is this okay?

  13. #13
    Join Date
    Feb 2014
    Posts
    9
    I did it this way:
    Code:
    <script type = "text/javascript">
    	if((navigator.vendor.length > 5) && (navigator.vendor.toLowerCase().indexOf("google inc") > -1)) {
    		
    	} else {
    		window.location.href = "1st-brskalnik.php"; // all other browsers
    	}
    </script>
    Is this okay?

  14. #14
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    Try the following. I changed .href (for clicking on a link) to .replace which should re-direct. all you have to do is paste in the actual names of the desired websites.

    [CODE]
    <script type = "text/javascript">
    if((navigator.vendor.length > 5) && (navigator.vendor.toLowerCase().indexOf("google inc") > -1)) {
    window.location.replace("somepage.html"); // if chrome
    } else {
    window.location.replace("someotherpage.html)"; // all other browsers
    }
    </script>
    </CODE]

    The code you showed will do nothing if the user is using Chrome (i.e. will stay on the current page) but will go to 1st-brskalnik.php if the browser isn't Chrome.

  15. #15
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Quote Originally Posted by Kevin2 View Post
    Ha! Good catch!
    although it does sound sexier!
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles