SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    PHP include inside IE Conditional Comments being included in all browsers

    Hello,
    I was trying to include a PHP file only when the browser is IE. So I attempted doing so by doing a PHP include inside IE conditional comments, but when i viewing the source in chrome, I saw that the file was included there too.

    Does anyone know what I could be doing wrong or if there is a better way to do this?

    Thanks in Advance & Best Regards,
    Team 1504

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    IE conditionals are not part of PHP. Therefore, they will always be physically placed on the page. If you would like to include something only for IE using PHP you would need to inspect the header server-side and use the appropriate inspection conditional(s). Though that is probably not a good approach in the first place. What is it that you are specifically trying to do?
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What is placed in conditional comments is always output to the HTML. PHP doesn't know what IE conditional comments are, nor does it care.
    If you were just writing HTML, you'd still write the IE only bits to the page, inside the IE conditional comments. It's when the browser comes to look at this code that most browsers choose not to display this bit of HTML, and IE will look at the conditionals and start making decisions. But the conditional code is always in the source code, no matter what the browser.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    html is parsed on the client computer. php is parsed on the server. So the server parses your php and sends it to the client, then the client parses the html and looks for conditional statements.

    what you want to do is have the server check which browser made the request and have the server send different data back to the client based on which browser made the request. This is called sniffing. One easy way to do this is check to see whats in the $_SERVER['HTTP_USER_AGENT'] variable.

  5. #5
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Personally, I wouldn't trust the user agent header as it can be omitted or spoofed. Even without expert technical knowledge some browsers include options to omit or change the user agent that's sent to the server. Other people may have different opinions on this.

    If the OP says what it is they're doing and what it is they want to achieve we can advise on how best to implement this.

    Depending on the content that's included, using conditional comments doesn't seem like a terrible way of implementing this, as long as you are aware that it will still show in the source code.

  6. #6
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    it is just code that only needs to run or be parsed in internet explorer. sorry if that was too vague. I am just including a lot of css and html just for ie. does that help you help me somewhat more ?

  7. #7
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by team1504 View Post
    it is just code that only needs to run or be parsed in internet explorer.
    Do you mean php code to be run, or something to be displayed?
    eg do you want to log in the database that an ie user agent has visited? This is not possible using IE conditional comments because the conditional comments are only looked at by the browser, and not the php process*
    Do you want to display an IE specific message eg 'hello, internet explorer user. What are you using that piece of rubbish for? You should get something proper like chrome'? In which case IE conditional comments would be appropriate, because the php side would all be the same:- php outputs the html which has this message, enclosed in IE conditional comments. It's always there in the html code, but non IE browsers ignore it.

    So what, more exactly, are you doing? And does the above help you understand what's going on?



    *As a side note, one way that you could log a visit from an IE user in the database is by calling a javascript file, within the conditional comments. But instead of an actual javascript file, you could have a php script. eg
    Code:
    <!--[if IE]>
    <script src='http://www.myserver.com/logIEuser.php'></script>
    <![endif]-->
    So IE would see this part of the HTML and call the file, which would execute whatever php code is included in it. All other browsers would ignore it. Note that this approach is most likely NOT what you need, I just wanted to point out that it is possible.

  8. #8
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i just want something to be displayed only when the browser is ie.

    I know I could just put the exact code inside the conditional comments instead of an include to a separate file containing it, but that is a last resort right now
    as i want to see if there is a way i can include it.

    I tried html includes and that didn't work properly, but since the page is a php document I thought I might use a php include anyway and now i got into this issue

  9. #9
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    A php include will still work, but you need to understand that it will be output in the html for every browser, but only shown to the user for ie. It sounds like you've already got it doing what you want.
    here's an example:

    display_for_ie.php:
    Code:
    echo "Hello ie user. You really ought to get with the times";
    index.php:
    Code:
    echo "Welcome to my website. There's lots of good stuff here.<br><br>
    <!--[if IE]>";
    include "display_for_ie.php";//this calls the include file that outputs the message that's intended for IE only users
    echo "<![endif]-->
    <br>
    Have a nice day.";
    The HTML that is output from PHP will look like this:
    Code:
    Welcome to my website. There's lots of good stuff here.<br><br>
    <!--[if IE]>
    Hello ie user. You really ought to get with the times
    <![endif]-->
    <br>
    Have a nice day.
    Most browsers, ignoring the conditionals, will output this to the user:
    Code:
    Welcome to my website. There's lots of good stuff here.
    
    
    Have a nice day.
    IE will interpret and display the content within the conditionals:
    Code:
    Welcome to my website. There's lots of good stuff here.
    
    
    Hello ie user. You really ought to get with the times
    
    Have a nice day.

  10. #10
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,027
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Warning: IE conditionals are deprecated and will not be honored by IE 10.

  11. #11
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Warning: IE conditionals are deprecated and will not be honored by IE 10.
    Good to know. However, according to the all knowing wiki:
    Conditional comments first appeared in Microsoft's Internet Explorer 5 browser and are supported through to version 9. Microsoft has announced that support has been discontinued in Internet Explorer 10 when processing HTML5 pages, but older pages using the technique will continue to work
    So if your pages are tagged as html4 it'll still work, apparently.

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    <!--[if IE]>
    <?php include('some_php_file.php'); ?>
    <![endif]-->

    This will only display the contents of the file in IE.
    You will still be able to see the contents when you look at the page source. This is always the case. If you want to solve this with php you need to go about it like this:

    <?php
    function figureOutIfBrowserIsIE(){
    // do stuff here to figure out if browser is IE (for example use http_user_agent inside $_SERVER)
    }
    if(figureOutIfBrowserIsIE()){
    // put the stuff you want to display only when browser is IE over here.
    }

  13. #13
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    @brense
    Quote Originally Posted by brense View Post
    <?php
    function figureOutIfBrowserIsIE(){
    // do stuff here to figure out if browser is IE (for example use http_user_agent inside $_SERVER)
    }
    if(figureOutIfBrowserIsIE()){
    // put the stuff you want to display only when browser is IE over here.
    }
    well i knew that much. i was just hoping that there was another way to detect if it was ie other than browser sniffing / agent sniffing

    @Michael Morris : I like you avatar, what does it mean?

    @Michael Morris & @hessodreamy :
    So let me get this on ie conditional straight. IE10 will ignore ie conditionals in pages with the <!Doctype HTML> doctype including ignoring the general <!--[if IE]><![endif]--> conditional.
    However IE9 and below will still recognise ie conditionals regardless of the Doctype

    right?

  14. #14
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,027
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    It's kanji for "Aki" which means "Autumn" or "Bright". It's a fairly common unisex Japanese name and I use it as a screen name on several sites.


Tags for this Thread

Bookmarks

Posting Permissions

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