Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Modify HTTP header

    Hello,

    I know nothing about ASP but I made and coded my schools website and theres no PHP installed so I have to go with ASP. What I want to do is do the exact same thing the PHP code below does but in ASP. Can I do this?
    PHP Code:
    <?php

    if ((isset($_SERVER["HTTP_ACCEPT"]) and

    stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) or

    stristr($_SERVER["HTTP_USER_AGENT"], "W3C_Validator") )

    {

    header("Content-type: application/xhtml+xml");

    print(
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");

    }

    else

    {

    header("Content-type: text/html; charset=utf-8");

    }

    ?>
    Thank you very much
    Not much, but its something.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Code:
    <%
    'setting response.buffer makes sure nothing is sent to the browser before the entire script is executed. Otherwise headers dannot be changed
    Response.Buffer = True
    
    'check for the ServerVariables HTTP_ACCEPT and HTTP_USER_AGENT
    If Request.ServerVariables("HTTP_ACCEPT") = "application/xhtml+xml" Or
    Request.ServerVariables("HTTP_USER_AGENT") = "W3C_Validator" Then
        'SetHeader Name, Value
        Response.SetHeader "Content-type", "application/xhtml+xml"
        'add the string to the output stream, escaping double quotes by doubling them
        Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & VBCrLf
    Else
        Response.SetHeader "Content-type", "text/html; charset=utf-8"
    End If
    
    %>
    (just translated it, did not test)
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Nope,

    Code:
    Microsoft VBScript compilation  error '800a03ea' 
     Syntax error 
     /flclfan/index.asp, line 6 
    If Request.ServerVariables("HTTP_ACCEPT") = "application/xhtml+xml" Or
    ----------------------------------------------------------------------^
    Not much, but its something.

  • #4
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Put the line below it on the same line as the one that ends "Or".

  • #5
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok, now i get this error.
    Code:
    Microsoft VBScript runtime  error '800a01b6' 
     Object doesn't support this property or method: 'Response.SetHeader' 
     /flclfan/index.asp, line 12
    Not much, but its something.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    You are right, it must be:

    Response.AddHeader "Content-type", "application/xhtml+xml"
    I am the luckiest man in the world

  • #7
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Code:
    <%
    'setting response.buffer makes sure nothing is sent to the browser before the entire script is executed. Otherwise headers dannot be changed
    Response.Buffer = True
    
    'check for the ServerVariables HTTP_ACCEPT and HTTP_USER_AGENT
    If Request.ServerVariables("HTTP_ACCEPT") = "application/xhtml+xml" Or Request.ServerVariables("HTTP_USER_AGENT") = "W3C_Validator" Then
        'AddHeader Name, Value
        Response.AddHeader "Content-type", "application/xhtml+xml"
        'add the string to the output stream, escaping double quotes by doubling them
        Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & VBCrLf
    Else
        Response.AddHeader "Content-type", "text/html; charset=utf-8"
    End If
    
    %>
    Ok, theres the code, I now get no errors but its not doing anything. The page is still being served as text/html and its not writing "<?xml version="1.0" encoding="utf-8"?>". What do i do now?
    Not much, but its something.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Well, you can try to output the values for the variables you are checking by:

    Code:
    Response.Write "Var HTTP_ACCEPT: " & Request.ServerVariables("HTTP_ACCEPT") & "<br />"
    Response.Write "Var HTTP_USER_AGENT: " & Request.ServerVariables("HTTP_USER_AGENT")  & "<br />"
    to see what you are comparing with
    I am the luckiest man in the world

  • #9
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok, heres my results:

    Code:
     Var HTTP_ACCEPT: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    Var HTTP_USER_AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7
    Can you tell me whats wrong with the code and why its not working?
    Not much, but its something.

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Quote Originally Posted by Sman5109 View Post
    Ok, heres my results:

    Code:
     Var HTTP_ACCEPT: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    Var HTTP_USER_AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7
    Can you tell me whats wrong with the code and why its not working?
    nothing is wrong with the code. You compare the HTTP_ACCEPT with the string
    Code:
    "application/xhtml+xml"
    . The HTTP_ACCEPT you are getting is:
    Code:
    "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
    So there is no exact match here. The same goes for the HTTP_USER_AGENT. You compare with:
    Code:
    "W3C_Validator"
    , but you are getting:
    Code:
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
    So, there is no exact match either. The code is doing what it is supposed to do and goes inte the Else branch of your If statement.

    You probably want to check if the string "application/xhtml+xml" appears somewhere within the HTTP_ACCEPT variable. Then you should use the InStr() function. This function checks to see if the second provided string appears somewhere in the first provided string and returns the position where it starts. So if it returns a number larger than 0 the second string appears in the first.

    If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 OR InStr(Request.ServerVariables("HTTP_USER_AGENT"), "W3C_Validator") > 0 Then
    etcetera
    Last edited by Roelf; 10-13-2007 at 11:54 PM. Reason: typos caused by alcohol in my system ;-)
    I am the luckiest man in the world

  • #11
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts
    But what that PHP code does is serve the page as application/xhtml+xml to browsers that support it and the W3C Validator and add the xml version line, and serve the page as text/html to all other browsers that do not support application/xhtml+xml.

    My browser supports application/xhtml+xml and thus if the asp code does what it should i should see that the page is being served as application/xhtml+xml, and the xml line should print/write in there.
    Not much, but its something.

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    sorry for not exactly knowing what the stristr() function in php does. But i think my last suggestion with the InStr() function in VBScript does what you need.....

    Have you tried that yet?
    I am the luckiest man in the world

  • #13
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I added that line heres the code:
    Code:
    <%
    Response.Buffer = True
    If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/xhtml+xml") > 0 OR InStr(Request.ServerVariables("HTTP_USER_AGENT"), "W3C_Validator") > 0 Then
        Response.AddHeader "Content-type", "application/xhtml+xml"
        Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & VBCrLf
    Else
        Response.AddHeader "Content-type", "text/html; charset=utf-8"
    End If
    %>
    Is this code right?

    It now prints the "<?xml version=""1.0"" encoding=""utf-8""?>" but it is still not being served in ...../xhtml+xml.

    Edit: The W3C validator is getting application/xhtml+xml but im still not.
    Last edited by Sman5109; 10-14-2007 at 12:29 AM.
    Not much, but its something.

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    To be honest, i don't know why that happens. The fact that the xml tag is printed is an indication that the correct branch of the if then else statement is entered. But the AddHeader seems to fail for your browser. I don't know why that is. Sorry
    I am the luckiest man in the world

  • #15
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    AddHeader adds a NEW header to the HTTP response. It won't replace an existing one. ContentType is a header that always gets set, and it defaults to text/html. If you want to change it, use Response.ContentType.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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