1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Cleaning up script.

Discussion in 'PHP' started by Matt Ridge, Nov 28, 2011.

  1. #1
    Is there a way instead of putting OS && Browser as 'If commands', making it so that it would work as an array?

    If not is there a way of cleaning this up so that instead of doing a lot of "if commands" making it so it goes through a list and says you are using x OS and y browser?

    Or is this something that I should be using with PHP/SQL to make it easier?

    
    <?php
    
    
    //Operating Systems
    function xp(){return(eregi("Windows NT 5.1", $_SERVER['HTTP_USER_AGENT']));}
    function vista(){return(eregi("Windows NT 6.0", $_SERVER['HTTP_USER_AGENT']));}
    function win7(){return(eregi("Windows NT 6.1", $_SERVER['HTTP_USER_AGENT']));}
    
    
    
    
    //Web Browsers
    function chrome(){ return(eregi("chrome", $_SERVER['HTTP_USER_AGENT']));}
    function ie9(){ return(eregi("MSIE 9.0", $_SERVER['HTTP_USER_AGENT']));}
    function ie8(){ return(eregi("MSIE 8.0", $_SERVER['HTTP_USER_AGENT']));}
     
    if(xp() && chrome())
    {
    // do something if XP and Chrome
    echo 'You are using Windows XP with a Chrome Web Browser';
    }
    
    
    if(xp() && ie8())
    {
    // do something if XP and Chrome
    echo 'You are using Windows XP with a Internet Explorer 8 Web Browser';
    }
    
    
    ?>
    Code (markup):

     
    Matt Ridge, Nov 28, 2011 IP
  2. 7thand43rd

    7thand43rd Member

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Are you only wanting to output a statement that says you are using X operating system and Y web browser? Or do you want to perform another function depending on what they are using?
     
    7thand43rd, Nov 28, 2011 IP
  3. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Well ultimately I will want to want to output a line of code... which I think I know how to do, but what I am looking to do is make it say you are using x OS and y browser... but what I want to do is make it so that it does it in one line instead of having a lot of if statements all over the place. The amount of variables really is too extensive to be worth typing.

    Basically what I am doing is writing a if x OS and y browser, use z CSS script. But I also want to use it to make a script in where I can use it for other functions as well.
     
    Matt Ridge, Nov 28, 2011 IP
  4. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #4
    Just set the common stuff (like variables, business logic etc) outside of your if statements, then anything specific which uses them variables inside.

    I can't see how you'll have too many items of code (other than css) which are written for a specific browser.
     
    mfscripts, Nov 30, 2011 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    You should be using the preg* set of functions (preg_match ?) instead as ereg* is deprecated.

    Also don't bother with what you are doing - no need to reinvent the wheel....I've seen tons of classes and functions lying around code databases online - google...
     
    danx10, Nov 30, 2011 IP
  6. GokuDBZ

    GokuDBZ Well-Known Member

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    140
    Digital Goods:
    2
    #6
    GokuDBZ, Dec 1, 2011 IP
  7. mrhrk01

    mrhrk01 Well-Known Member

    Messages:
    664
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    145
    #7
    The function below will help you to get the OS the user is using.
    http://www.php.net/manual/en/function.get-browser.php

    Using the $_SERVER['HTTP_USER_AGENT'] - you can get the User agent i.e. the browswer the user is using.

    Combing these together - you should be able to construct any line of code you want to output. I think this could be done within 5 lines of code or fewer.
     
    mrhrk01, Dec 7, 2011 IP