I currently have my htaccess file set to remove the file type so
www.example.com/home.php
would become,
www.example.com/home
I need to compare the url with one that I have saved in the database, I do this by,
if ($_SERVER['QUERY_STRING']) {
$pageURL = str_replace('.php', '?', 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING']);
}
else {
$pageURL = substr('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'], 0, -4);
}
When I save the url in the database it gets saved as the current page url after the htaccess file has removed the .php, it is inserted using jquery ajax.
When I come to compare it, I need this to make it match in php as php doesn't take into account the htaccess changes.
Is there a better way to do this?
You can do:
$pageURL = rtrim($_SERVER['REQUEST_URI'], '.php');
This would give your url with .php
removed whenever it appears at the end of the url.
Look into the parse_url()
function. You can use it to parse the URL into parts then modify the path component (eg remove .php). Then you can rebuild the URL from those parts using the http_build_url()
function.
在我看来,结合John的使用parse_url()
以及使用pathinfo()的建议将是最好的 - 修剪,使用substr的截断等并不总是像pathinfo()
那样优雅。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.