I have to display link into my website like this :
$url = 'google.com';
Check it !
What I don't understand now is that, the output href is not google.com but mydomain.com/google.com.
I don't know how to resolve this issue.
You need to append http:// before your url
Try below code:
$url = 'http://www.google.com';
Check it !
Please use this code for site url .
$url = 'http://google.com';
Check it !
If you don't add http then it will open your domain name and then your $url variable name so please add http in your url.
Related
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.
Suppose my current url is:
www.example.com?id=10
if($_GET['id']==10):
//replace current $_SERVER[HTTP_HOST]; with www.example1.com without removing other parts of url and redirect.
else :
//replace current $_SERVER[HTTP_HOST]; with www.example2.com without removing other parts of url and redirect.
endif:
I need to replace current $_SERVER[HTTP_HOST].
How to do this using php.
If you want to redirect then here is the code-
$request = $_SERVER[REQUEST_URI];
if($_GET['id']==10):
$url = "www.example1.com/".$request;
else :
$url = "www.example1.com/".$request;
endif;
else I would like to know for what purpose you want to change current url.
Creating a website and in the header I want the URL a link to be different depending on which directory it is.
I want it to be:
- If directory1 then link to directory1 URL
- If directory2 then link to directory2 URL
Can someone help with it?
I found the code below which might be helpful.
Thanks
<?php
$homepage = "/";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
echo '/';
}
?>
Now i understood your question you wanted to get the main directory. If this is so than explode the url and than replace the http:// or https:// section from the array and than output the variable
<?php
$url="http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$url_orignal= explode('/', str_ireplace(array('http://', 'https://'), '', $url));
echo "www.website.com/".$url_orignal[1];
?>
Hope this helps you
In my website, users can put an URL in their profile.
This URL can be http://www.google.com or www.google.com or google.com.
If I just insert in my PHP code $url, the link is not always absolute.
How can I force the a tag to be absolute ?
If you prefix the URL with // it will be treated as an absolute one. For example:
Google.
Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.com).
Use a protocol, preferably http://
Google
Ask users to enter url in this format, or concatenate http:// if not added.
If you prefix the URL only with //, it will use the same protocol the page is being served with.
Google
I recently had to do something similar.
if (strpos($url, 'http') === false) {
$url = 'http://' .$url;
}
Basically, if the url doesn't contain 'http' add it to the front of the string (prefix).
Or we can do this with RegEx
$http_pattern = "/^http[s]*:\/\/[\w]+/i";
if (!preg_match($http_pattern, $url, $match)){
$url = 'http://' .$url;
}
Thank you to #JamesHilton for pointing out a mistake. Thank you!
With this code:
if(empty($aItemInfo['url'])) {
$url = '<p> </p>';
} else {
$url = ' | LINK';
}
I've got this as output:
http://localhost/tester/www.google.com
In db there is only www.google.com and ofcourse it's fictional.
What am I doing wrong?
You need to add http:// while parsing your code, before using it in the <a> tag.
If all your URLs will be without http:// use this code:
$url = 'http://'.$aItemInfo['url'];
Then use $url
Not too sure what you're trying to link to. If you're linking to an external site you'll need to add http:// in front of the link. If not, the link will be added to the end of the current domain name as shown above
You can put links are relative or absolute paths. If you don't include the "http://" part, then it assumes that it is a relative path. Add href="http://'.$aItemInfo['url'].'"