• ASP php获取文件URL地址等方法


    $_SERVER["HTTP_REFERER"] rss中可用

    echo next(explode("=", $_SERVER["QUERY_STRING"]));
    echo $_GET[$id];一样的哦

    http://www.biuuu.com/index.php?p=222&q=biuuu
    结果:
    $_SERVER["QUERY_STRING"] = “p=222&q=biuuu”
    $_SERVER["REQUEST_URI"] = “/index.php?p=222&q=biuuu”
    $_SERVER["SCRIPT_NAME"] = “/index.php”
    $_SERVER["PHP_SELF"] = “/index.php”
    $_SERVER["QUERY_STRING"]获取查询语句,实例中可知,获取的是?后面的值
    $_SERVER["REQUEST_URI"] 获取http://www.biuuu.com后面的值,包括/
    $_SERVER["SCRIPT_NAME"] 获取当前脚本的路径,如:index.php
    $_SERVER["PHP_SELF"] 当前正在执行脚本的文件名

    $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    echo $url;
    $filename= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    echo $filename;

    // 只取 ? 前面的内容
        if (strpos($this_page, "?") !== false) 
            $this_page = reset(explode("?", $this_page));

    如果字符串中只有域名:

    1
    2
    3
    $str 'aaa.baidu.com';
    preg_match("/[w-]+.w+$/"$str$arr);
    echo $arr[0];
     用到

    如果是完整的URL:

    1
    2
    3
    $str 'fdsa.fdsa.baidu.com/fdsa/rewl.html';
    preg_match("/[w-]+.w+(?=/)/"$str$arr);
    echo $arr[0];
    追问
    请问如何提取降一级的域名呢如xxx.xxx.com就提取xxx.com而xxx.xxx.xxx.com就匹配xxx.xxx.com
    回答
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $str 'http://we.aa.baidu.com/dfsa.html';
    $str2 'http://baike.baidu.com/';
    //正则
    $reg "#(?<=\.)([\w-]+\.)+?\w+(?=/)#ix";
    //匹配第一组
    preg_match($reg$str$arr);
    echo $arr[0];
     
    echo '';
    //匹配第二组
    preg_match($reg$str2$arr);
    echo $arr[0];

    嗯,使用正则的断言,很一句代码就实现了。

    =================================================================================

    =======================================================================================

    ASP获取文件URL地址方法  

    ASP如何获取文件地址呢?直接看例子:

    如果要想获取这样的地址:http://192.168.0.5/super/super_article.asp?id=4
    那么我们就只要获取:
    192.168.0.5---><%=Request.ServerVariables("HTTP_HOST")%>
    /super/super_article.asp-----><%=Request.ServerVariables("URL")%>
    id=4----><%=Request.ServerVariables("QUERY_STRING")%>
    那么我们把上面的地址合起来就可以完成任务了:
    http://192.168.0.5/super/super_article.asp?id=4----->;http://<;%=Request.ServerVariables("HTTP_HOST")&request.ServerVariables("URL")&"?"&Request.ServerVariables("QUERY_STRING") %>
    使用获取url中的文件名和传过来的值:
    本文件ip路径:<%="http://" & request.servervariables("server_name")&request.servervariables("script_name") %> 就可以了..
    下面是具体其它一些获取服务器信息的一些方法
    几个常用Request.ServerVariables的中文
    本文件ip路径:<%="http://" & request.servervariables("server_name")&request.servervariables("script_name") %>
    本机ip:<%=request.servervariables("remote_addr")%>
    服务器名:<%=Request.ServerVariables("SERVER_NAME")%>
    服务器IP:<%=Request.ServerVariables("LOCAL_ADDR")%>
    服务器端口:<%=Request.ServerVariables("SERVER_PORT")%>
    服务器时间:<%=now%>
    IIS版本:<%=Request.ServerVariables("SERVER_SOFTWARE")%>
    脚本超时时间:<%=Server.ScriptTimeout%>
    本文件路径:<%=server.mappath(Request.ServerVariables("SCRIPT_NAME"))%>
    服务器CPU数量:<%=Request.ServerVariables("NUMBER_OF_PROCESSORS")%>
    服务器解译引擎:<%=ScriptEngine & "/"& ScriptEngineMajorVersion&"."&ScriptEngineMinorVersion&"."& ScriptEngineBuildVersion %>
    服务器操作系统:<%=Request.ServerVariables("OS")%>
    支持的文件类型:<%=Request.ServerVariables("HTTP_Accept")%>
    访问的文件路径:<%=Request.ServerVariables("HTTP_url")%>
    用户代理的信息:<%=Request.ServerVariables("HTTP_USER_AGENT")%>
    获取url中的文件名和传过来的值:request.ServerVariables("script_name")+"?"+request.ServerVariableS("QUERY_STRING")
    ASP如何获取文件所在目录
    在ASP中,我们都知道文件的路径怎么获取,但是文件所在目录却不知道怎么办?
    我们获取文件的路径是:<%=Request.ServerVariables("PATH_TRANSLATED")%>
    既然我们都获取了文件的路径了,那么我们就可以使用函数来处理一下刚才获取的路径,
    下面就是我们的处理:
    <%=Left(Request.ServerVariables("PATH_TRANSLATED"),instrRev(Request.ServerVariables("PATH_TRANSLATED"),""))%>
    那么这个输出是什么呢?它就是你要的文件所在的目录路径。

    $_SERVER[PHP_SELF] - $_SERVER[SCRIPT_NAME] - $_SERVER['REQUEST_URI']

    $_SERVER[PHP_SELF], $_SERVER[SCRIPT_NAME], $_SERVER['REQUEST_URI'] 在用法上是非常相似的,他们返回的都是与当前正在使用的页面地址有关的信息,这里列出一些相关的例子,帮助确定哪些是在你的脚本最适合的。

    $_SERVER[’PHP_SELF’]

    • http://www.yoursite.com/example/ — – — /example/index.php
    • http://www.yoursite.com/example/index.php — – — /example/index.php
    • http://www.yoursite.com/example/index.php?a=test — – — /example/index.php
    • http://www.yoursite.com/example/index.php/dir/test — – — /dir/test

    当我们使用$_SERVER['PHP_SELF']的时候,无论访问的URL地址是否有index.php,它都会自动的返回 index.php.但是如果在文件名后面再加斜线的话,就会把后面所有的内容都返回在$_SERVER['PHP_SELF']。

    $_SERVER['REQUEST_URI']

    • http://www.yoursite.com/example/ — – — /
    • http://www.yoursite.com/example/index.php — – — /example/index.php
    • http://www.yoursite.com/example/index.php?a=test — – — /example/index.php?a=test
    • http://www.yoursite.com/example/index.php/dir/test — – — /example/index.php/dir/test

    $_SERVER['REQUEST_URI']返回的是我们在URL里写的精确的地址,如果URL只写到”/”,就返回 “/”

    $_SERVER['SCRIPT_NAME']

    • http://www.yoursite.com/example/ — – — /example/index.php
    • http://www.yoursite.com/example/index.php — – — /example/index.php
    • http://www.yoursite.com/example/index.php — – — /example/index.php
    • http://www.yoursite.com/example/index.php/dir/test — – — /example/index.php

    在所有的返回中都是当前的文件名/example/index.php

  • 相关阅读:
    教你如何自定义组件
    android应用开发小技巧
    改变Vim在iTerm2中的光标
    Mac添加bash alias
    tmux常用命令
    javascript Date 总结
    ES6箭头函数
    npm常用命令
    ES6 import export
    gitingore
  • 原文地址:https://www.cnblogs.com/alex-13/p/3506896.html
Copyright © 2020-2023  润新知