Reputation: 8334
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? i tried $_SERVER[PHP_SELF] and it worked on localhost but not on my server. I get: http://test.nl/test.nl/admin/xxx.php?id=423
Upvotes: 0
Views: 5483
Reputation: 1634
You should use URL rewriting. Take a look at the component Zend_Router
in the Zend framework.
And for the purposes of getting the current file 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();
?>
Upvotes: 1
Reputation: 297
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. $2
gives preg replace a direct reference to the entire capturing group. Therefore admin has to be the first folder from the web root in which the capturing group resides.
Upvotes: 0
Reputation: 17072
You not need current url, you can link new query string:
with html link:
<a href="?foo=bar">
or php redirection:
Header("Location: ?foo=bar");
Upvotes: 0
Reputation: 3845
You can use $_SERVER['SCRIPT_NAME'] variable to retrieve the current script's path i.e fullpath and filename of current file
For more details about $_SERVER variable please refer the documentation mentioned in below url
https://www.php.net/reserved.variables.server
Upvotes: 0
Reputation: 4778
I don't see what it wouldn't work on a production site, but does locally.
header("Location: {$_SERVER['REQUEST_URI']}?$rest");
Upvotes: 0
Reputation: 3558
Just redirect to /xxx.php?$rest
. You don't need the full address if you aren't leaving the directory.
Upvotes: 0
Reputation: 4983
Try the following:
$basename = basename($_SERVER['REQUEST_URI']);
This should give you the name of the file in the URL including the extension. 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']));
Upvotes: 1