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

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.

Related

Getting url and make the menu active

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);

How to get full url along with # symbol in php

I am using AngularJS routing with php,
so that my urls look like
http://localhost/admin/home#/categories
When ever I am trying to get url it wont gave full url. It gives only /admin/home.
I am using $_SERVER['REQUEST_URI'] and codeigniter segment. Both are not working.
Can any one help?
This information is not being past to the server I'm afraid.
The url part after # is only for browser use. You can add GET or POST parameters to pass this information to your server like:
http://localhost/admin/home?hash=categories#/categories
use:
$_SERVER[PHP_SELF]
It will give the full url, including everything.
If you want to strip stuff from the url, use things like this:
$url = trim(strtok("$url", '?'));
$url = str_replace("#!/", "", "$url");

How to detect if user go to a link look like our rule?

example:
I have a webpage abc.com/index.php
I want to detect if user connect to abc.com/index.php/bbdh,
abc.com/index.php/bb/saggfd/gjasgd/hsahgd, abc.com/index.php/bb/saggfd/gjas and so on without having a page like that on my host.
I'm using php and need some way to do that without using .htaccess file.
Thanks for any help!
have a look at $_SERVER['REQUEST_URI'], every thing you need will be accessible here. E.g. for:
abc.com/index.php/bb/saggfd/gjas
it would be:
/index.php/bb/saggfd/gjas
strip the /index.php with something like:
echo substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
and you're set.
Update on how to handle GET-parameters:
// get the full request uri
$requestUri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
// let php parse that url (though it's just a part of the full url)
$chunks = parse_url($requestUri);
// let php parse the query string
parse_str(isset($chunks['query']) ? $chunks['query'] : '', $chunks['query']);
// debug output
print_r($chunks);

PHP - Get string from URL

by having an URL like this: mysite.com/subfolder/helloworld - Is it possible to read the "helloworld" from within a PHP page?
I would like to use the string as a part to load some content.
end( explode( '/', $_SERVER['REQUEST_URI'] ) )
without the end() call it will give you all the parts of the URL
You should read about URL rewriting.
This is more clean than "reading" the current URL. Basically you can redirect your URL (transparently) to something like :
mysite.com/index.php?folder=subfolder&category=helloworld
Then in PHP, you can access the URL parameter with :
$folder = $_GET['subfolder']
$category = $_GET['category']
This is maybe not the kind of answer you were expecting, but it can be interesting to know.
The request url (everything from the first / after the domain name) can be found in $_SERVER["REQUEST_URI"]
Hello have u used 'strstr' keyword of php?
please try like this
if(strstr($_SERVER["REQUEST_URI"], "helloworld"))
return true;
else
return false;
May be this will be helpful to you.

How to detect if link is from a particular domain

I need to be able to detect with PHP if a link is from a particular domain. I can not just check if domain is present in the link because it can be faked by appending domain.
Thanks.
Just use parse_url() as konforce mentioned. For example:
$url = "http://www.google.com/";
$parts = parse_url ($url);
print $parts["host"]; // will print www.google.com
// Or, for PHP 5.1 and above
$host = parse_url ($url, PHP_URL_HOST); // returns www.google.com
Now, the good thing about this is that appending the domain to the end of an url like this:
http://www.google.com/?www.foo.com
Wont work as the host element will still say that the link points to www.google.com and not www.foo.com.
Hope this helps.
I believe you'd want check the referrer and make sure you check with double forward slashes, since that's part of the protocol (HTTP/HTTPS) and can't be faked.
Check this link for extra reference: Determining Referer in PHP
I would check against something like...
//www.mydomain.com
//mydomain.com

Categories