Getting url and make the menu active - php

hi lets assume there are two websites:
Website link-1: http://website.com/ABC
Website link-2: http://newwebsite.com
to make the menu active have written the code in this format:
$uri = $_SERVER["REQUEST_URI"];
$uriArray = explode('/', $uri);
$currentPage = $uriArray[3];
The results of First is working fine
But the result of second one is not coming because
this $uriArray[3]; is calling something else so when i change it to [2] it is working fine
So how can i make this code to run in any application or server..?
i have changed my application path to test directory to public so i am
getting this issue
`

Use php end method. http://php.net/manual/en/function.end.php
end($uriArray);

Don't use explode to extract a path from a url, use parse_url that is designed for this task:
$path = parse_url($url, PHP_URL_PATH);

Related

Q: How to check the extension of an URL website?

I would like to know if there is a way to check the extension of an URL website ? For example, do something when the website is like http://example.es/ and do another thing when the website is like http://example.fr/
I have check that there is something like
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
which returns the current URL of the web page.
Thanks for help.
Use parse_url() function to get host part of the url then explode by . and get last element of an array
Example below:
$url = 'http://' . $_SERVER['SERVER_NAME'];
echo end(explode(".", parse_url($url, PHP_URL_HOST)));
// echos "com"
From your example I assume that you are using PHP, then you can use parse_url to get the components.
https://www.php.net/parse-url
For example you can get the host example.fr and example.com, then do explode on host string to get the tld, .fr or .com, which should help you to do further if-else.

Get custom URL PHP

I'm working on a simple app to retrieve a JSON file info, but to do so I need an ID, I fixed that problem by adding it to the URL like this:
http://localhost:8000/comic_library/comic.php?id=2097
Since this is not a friendly URL, I change so it looks like this:
http://localhost:8000/comic_library/comic/2097
But when I retrieve the URL, the only thing I got back is:
http://localhost:8000/comic_library/comic.php
But I need the id in order to retrieve the JSON file, is there a way of getting the custom URL and not just the absolute URL
As you are using core php, it is not possible for you to have an url like http://localhost:8000/comic_library/comic/2097, if you used a framework like codeigniter or cms like wordpress then you would get that url. Core php urls have .php extension.
You should Pass the URL like this http://localhost:8000/comic_library/comic.php/2097
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);
echo $uri_segments[2]; // for http://localhost:8000/comic_library/comic.php/2097 you will get '2097'
First split the URL segments and Get the desired segment array value.
if your url is this
http://localhost:8000/comic_library/comic/2097
you can get your id by the following process
$fullurl = 'http://localhost:8000/comic_library/comic/2097';
$exploded_url = explode('/',$fullurl);
echo $final_id = end($exploded_url);

Php get last part of url and remove /

Im using an angular script for my site.
I need dynamic titles so I decided to use php to fetch the last part of the url to display for the page title.
This is the code im using. This code is out putting my full url.
<?php
function curPageURL() {
$url = $_SERVER['REQUEST_URI'];
$url_array = explode('/',$link);
$lastPart = array_pop($url);
return $lastPart;
}
echo curPageURL();
?>
The problem is its fetching the whole url.
For example here is my url http://www.istreamradio.ie/category/Music
I need to display just the last part of the url in this case is Music.
Thanks In advance.
You can use end() function after exploding the URL . Please try end instead of array_pop.

Get current name for path in url

How can i grab everything that is after http://www.domain.com/somefolder/ in php.For e.g if http://www.domain.com/somefolder/login is accessed , i just need the login part.
I tried the following
basename($_SERVER['PHP_SELF']); and basename(__FILE__); both gives me index.php
You can do it using strlen and substr functions:
<?php
$url = 'http://www.domain.com/somefolder/login';
$remove = 'http://www.domain.com/somefolder/';
echo substr($url, strlen($remove));
EDIT And you should make sure that your urls always start with www. subdomain. Otherwise you can get unexpected results.
I found the answer
$_SERVER['REQUEST_URI'] was what i need
P.S Ty for the downvotes

How to get the same $_SERVER['REQUEST_URI'] on both localhost and live server

So this is the thing, Im building dispatcher class that needs to dispatch http requesd based on Request URI, so Im interesed only in part of URI that comes behind domain.
Now when im working on live server on URI like this:
www.domain.com/controller/method/params/
REQUEST_URI would return:
/controller/method/params/
But when im working on local machine i have URI like this:
localhost/project/controller/method/params/
and REQUEST_URI would return:
/project/controller/method/params/
So is there an elegant and neat way to tell php to get me only the params I need fromu URI?
It dosent have to be $_SERVER['REQUEST_URI'], but I need that string the same on both live and local server?
Thanks!
Dispatcher compares current request URI with URI defined in config file. And on live server it matches, but on local machine there is one segment more then on live server.
How could I deal with this?
Usually I configure my local webserver to respond on "test.mysite.com" and in hosts file i set test.mysite.com to 127.0.0.1, in that way i better simulate production environments, and i'm not forced to use subpaths in urls
If you use apache webserver, you can simply setup N virtual hosts.
You can simply remove the path from the start of the URL if it exists.
$path = '/project/'; // for localhost/project/controller/method/params/
//$path = '/'; // for domain.tld/controller/method/params/
$url = getenv('REQUEST_URI');
if(strpos($url, $path) === 0)
{
$url = str_replace($path, '', $url, 1);
}
print $url;
I haven't tested the above code so stray "/" might mess it up. You can always trim the URL with something like $url = trim($url, '/') if needed.
$url = $_SERVER['REMOTE_ADDR'].$_SERVER['REQUEST_URI'];
is that what you want?
I use the built in array_shift() function if localhost is detected.
if ($_SERVER['HTTP_HOST'] == 'localhost') {
$array_uri = explode('/', $_SERVER['REQUEST_URI']);
array_shift($array_uri);
$uri = $array_uri;
} else {
$uri = explode('/', $_SERVER['REQUEST_URI']);
}

Categories