【问题标题】:get full url form server获取完整的 url 表单服务器
【发布时间】:2020-03-27 00:09:39
【问题描述】:

我想获取服务器端的 URL 表单。

我正在使用该请求调用 ajax 到服务器:

$.ajax({
  type: "post",
  url: "includes/pages_includes/func_infos.php",
  data: {
    "...."         : "..."
  },
  beforeSend: function() {

  },
  success: function(data) {

  }
});

如果我使用 $_SERVER['REQUEST_URI'](在 func_infos.php 中)获取 URL 它给了我:includes/pages_includes/func_infos.php。 p>

我想获取 URL:http://localhost/insc/web_/view_page.php?id_page=10

如何获得所需的网址?

【问题讨论】:

标签: javascript php url server


【解决方案1】:

将此方法用于 javascript:

var url = window.location.href;

对于 PHP:

<?php 
// Program to display URL of current page. 

if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
    $link = "https"; 
else
    $link = "http"; 

// Here append the common URL characters. 
$link .= "://"; 

// Append the host(domain name, ip) to the URL. 
$link .= $_SERVER['HTTP_HOST']; 

// Append the requested resource location to the URL 
$link .= $_SERVER['REQUEST_URI']; 

// Print the link 
echo $link; 
?> 

【讨论】:

    【解决方案2】:

    如果您想获取完整的 url 或者您的服务器同时支持 http 或 https,请尝试

    $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码使用 php 获取当前完整 url

      $pageURL = 'http';
      if ($_SERVER["HTTPS"] == "on") {
          $pageURL .= "s";
      }
      $pageURL .= "://";
      if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
      } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
      }
      

      参考链接:http://webcheatsheet.com/PHP/get_current_page_url.php

      【讨论】:

        猜你喜欢
        • 2018-11-09
        • 2010-10-27
        • 2014-01-20
        • 2010-09-16
        • 1970-01-01
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多