简体   繁体   English

在PHP中获取当前Url而不是文件根文件夹URL

[英]Get Current Url in PHP instead of files root folder url

Hello Friends I want to get the Current Page Url instead of capturing root url of file 您好朋友我想获取当前页面URL而不是捕获文件的根URL

I want to capture Like this http://localhost/tester/ 我想捕获像这样的http:// localhost / tester /

but coming like this 但就是这样来的

http://localhost/tester/views/inc/readcountry.php HTTP://localhost/tester/views/inc/readcountry.php

I have tried like this but it's returning 我试过这样但是它又回来了

$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]

localhost/tester/views/inc/readcountry.php 本地主机/测试/视图/ INC / readcountry.php

这个很好地工作而不是很多

$_SERVER['QUERY_STRING']

A little handy function I found somewhere else might help you out. 我在其他地方找到的一个方便的功能可能会帮助你。

function url() {
  return sprintf(
    "%s://%s%s",
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
    $_SERVER['SERVER_NAME'],
    $_SERVER['REQUEST_URI']
  );
}

To use it: 要使用它:

echo url();

if you want the full url, try this: 如果你想要完整的网址,试试这个:

<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

$_SERVER['HTTP_HOST'] - header from the current request, if there is one. $ _SERVER ['HTTP_HOST'] - 来自当前请求的标头(如果有)。

$_SERVER['REQUEST_URI'] - the URI which was given in order to access this page; $ _SERVER ['REQUEST_URI'] - 为访问此页面而给出的URI; for instance, '/index.html'. 例如,'/ index.html'。

$_SERVER['QUERY_STRING'] - it is your parameters and values in URL $ _SERVER ['QUERY_STRING'] - 它是您在URL中的参数和值


My htaccess for MVC app: 我对MVC app的htaccess:

RewriteEngine on RewriteEngine on

RewriteRule !.(js|gif|jpg|png|css)$ index.php RewriteRule!。(js | gif | jpg | png | css)$ index.php

Plus in each directory (model, views, controllers) i putted htaccess too: 另外在每个目录(模型,视图,控制器)中我也推出了htaccess:

Deny from all 拒绝所有人

不知道你需要停止捕获的树点是什么,我建议你看一下[$ _SERVER]数组的所有内容,转储它,以及php魔术常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM