zoukankan      html  css  js  c++  java
  • PHP获取当前页面的URL作为参数以供下一层的页面可以返回上一层页面

    1.基础url的获取

    #测试网址:     http://localhost/blog/testurl.php?id=5
    
    //获取域名或主机地址 
    echo $_SERVER['HTTP_HOST']."<br>"; #localhost
    
    //获取网页地址 
    echo $_SERVER['PHP_SELF']."<br>"; #/blog/testurl.php
    
    //获取网址参数 
    echo $_SERVER["QUERY_STRING"]."<br>"; #id=5
    
    //获取用户代理 
    echo $_SERVER['HTTP_REFERER']."<br>"; 
    
    //获取完整的url
    echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
    #http://localhost/blog/testurl.php?id=5
    
    //包含端口号的完整url
    echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
    #http://localhost:80/blog/testurl.php?id=5
    
    //只取路径
    $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 
    echo dirname($url);
    #http://localhost/blog

    2.由于使用CI框架,所以url的 $_SERVER['QUERY_STRING'] 可能为空,在此情况下,表单的内容无法传递到下一个页面。故需要分情况处理

    $now_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?';
                        if ($_SERVER['QUERY_STRING']) {  //不为空 
                            $now_url .= $_SERVER['QUERY_STRING'];
                        } else {  //添加表单内容作为参数
                            $query_string = "";
                            if (array_key_exists("name", $search_array)) {         
                                $query_string .= "name=".$search_array['name']."&";
                            }
                            if (array_key_exists("accounts", $search_array)) {
                                $query_string .= "accounts=".$search_array['accounts']."&";
                            }
                            $now_url .= $query_string;
                        }

    获取到完整的url后,需要把它编码,要不然格式会错乱

    $url_encode = urlencode($now_url);

    然后作为url的参数传给下一个页面

    href = "要跳转页面的url"."?pre_url=".$url_encode

    在下个页面中获取到传递过来的url信息,并解码

    $pre_url = $this->input->get_post('pre_url', true);
    $url_decode = urldecode($pre_url);   //原生的url,作为返回上一面的超链接的地址

     程序猿必读

  • 相关阅读:
    软件工程课堂二
    软件工程第二周总结
    软件工程第一周开课博客
    软件工程课堂一
    开学第一次考试感想
    以Function构造函数方式声明函数
    document.scrollingElement
    标识符
    变量声明语句的提升
    用that代替this
  • 原文地址:https://www.cnblogs.com/longzhongren/p/4687000.html
Copyright © 2011-2022 走看看