简体   繁体   English

你如何收到你当前的php文件网址

[英]How do you recieve your current php file url

I want to make navigation easier, so lets say when i have page called 我想让导航更容易,所以当我有页面调用时,让我们说

http://test.nl/admin/xxx.php?id=423

if i want to redirect to the same page with a different parameter. 如果我想用不同的参数重定向到同一页面。 i can do 我可以

Header("Location: xxx.php$rest");

but is there a way to get the xxx.php dynamic? 但有没有办法让xxx.php动态? i tried $_SERVER[PHP_SELF] and it worked on localhost but not on my server. 我尝试了$ _SERVER [PHP_SELF]并且它在localhost上工作但在我的服务器上没有。 I get: http://test.nl/test.nl/admin/xxx.php?id=423 我得到: http//test.nl/test.nl/admin/xxx.php?id = 423

Try the following: 请尝试以下方法:

$basename = basename($_SERVER['REQUEST_URI']);

This should give you the name of the file in the URL including the extension. 这应该为您提供URL中文件的名称,包括扩展名。 Of course, if you wanted to have the name of the file without the file extension then try: 当然,如果你想拥有没有文件扩展名的文件名,那么试试:

$basename = str_replace(".php","",basename($_SERVER['REQUEST_URI']));

You should use URL rewriting. 您应该使用URL重写。 Take a look at the component Zend_Router in the Zend framework. 看一下Zend框架中的Zend_Router组件。

And for the purposes of getting the current file URL: 并且为了获取当前文件URL:

<?php
function curPageURL() {
     $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"];
     }
     return $pageURL;
}

echo curPageURL();

?>

Just redirect to /xxx.php?$rest . 只需重定向到/xxx.php?$rest You don't need the full address if you aren't leaving the directory. 如果您不离开目录,则不需要完整地址。

我不知道它在生产站点上不起作用,但在本地发生。

header("Location: {$_SERVER['REQUEST_URI']}?$rest");

You can use $_SERVER['SCRIPT_NAME'] variable to retrieve the current script's path ie fullpath and filename of current file 您可以使用$ _SERVER ['SCRIPT_NAME']变量来检索当前脚本的路径,即当前文件的完整路径和文件名

For more details about $_SERVER variable please refer the documentation mentioned in below url 有关$ _SERVER变量的更多详细信息,请参阅以下url中提到的文档

http://in3.php.net/reserved.variables.server http://in3.php.net/reserved.variables.server

You not need current url, you can link new query string: 您不需要当前的URL,您可以链接新的查询字符串:

with html link: 使用html链接:

<a href="?foo=bar">

or php redirection: 或php重定向:

Header("Location: ?foo=bar");

I like using regex for these cases. 我喜欢在这些情况下使用正则表达式。 (Or rather cases where none of the other answers apply, like accessing a file relative to this one.) (或者更确切地说,其他答案都不适用,例如访问相对于此文件的文件。)

$foldername = preg_replace('/(.*)(\\/admin\\/.*)/', '$2', dirname(__FILE__));

$filename = preg_replace('/(.*)(\\/admin\\/.*)/', '$2', __FILE__);

Note that \\/admin\\/ is at the start of the second capturing group. 请注意\\/admin\\/位于第二个捕获组的开头。 $2 gives preg replace a direct reference to the entire capturing group. $2给preg替换了对整个捕获组的直接引用。 Therefore admin has to be the first folder from the web root in which the capturing group resides. 因此,admin必须是捕获组所在的Web根目录中的第一个文件夹。

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

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