URL output from database - php

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'].'"

Related

remove site url from links Wordpress

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.

Using file_get_contents And Changing Domain of Links on the Included Page?

On my page I have this piece of code:
<?php
$homepage = file_get_contents('http://www.nbc.com/');
echo $homepage;
?>
It includes the NBC website perfectly, but I notice that all of the links on the NBC.com website start with my domain name instead of http://nbc.com so they don't work.
So for example instead of http://nbc.com/the-blacklist/episodes being displayed on http://nbc.com website, its displaying http://my-domain.com/the-blacklist/episodes.
Is there any way to use file_get_contents do include a page into my URL, but make sure all the links on the page are the original links so they work fine?
The links inside of pages you are pulling are using relative links (/page.html) instead of the full URL, you will need to do a Regex on the variable or string replace. To test it out you could do the following:
$domain = 'http://www.nbc.com';
$pull = file_get_contents($domain);
echo str_replace('href="/', 'href="' . $domain . '/', $pull);

file_get_contents() returns all local/short links as 404

I am currently setting up a site, which requires some sort of "proxy" work. Basically through $_GET['url'] I can grab a site's content using file_get_contents($url). However, when links are shown like: <a href="images/image.png".../>, they will link to my site instead of theirs, which makes all images, links, etc. load from my site, which returns a 404 not found error.
I have not been able to find anything about this anywhere. How I do the "proxying" in theory, but not as a final product:
$url = $_GET['url'];
$content = file_get_contents($url);
echo $content;
What could I possibly do to change this, so all links doesn't depend on what the browser sees, but where they actually come from (the site link in $_GET['url']), which basically turns relative links into absolute? Thanks!
You would have to know what their site is in order to make a request from it.
To do this, you can parse the url:
$urlParsed = parse_url($url);
$urlHostOnly= $urlParsed['scheme'] . "://" . $urlParsed['host'] . "/";
Then, the tricky part, you have to prepend the host only url to each link.
Most links in html are in hrefs and src values so here is a simple replacer to deal with those.
$content = file_get_contents($url);
$replaced_content = preg_replace(
"/(href|src)=\"((?!http[s]:\/\/[a-z\.]{2,6}).*)\"/",
"$1=\"$urlHostOnly$2\"",
$content
);
Now that you have the replaced contents, echo it to the client
echo $replaced_content;
Note: There can be some conflict with respect to stylesheets and ssl if you do not specify the correct protocol (http / https) when entering the url.
See: http://i.imgur.com/tz6Hn28.png for an example of this.
Seems like I've solved this from an advice from a friend.
//grabs the URL of the site I am working with (the $_GET['url'] site basically)
$fullUrl = basename($url);
//Replaces <head> with <head> followed by a base-tag, which has the href attribute of the website.
//This will make all relative links absolute to that base-tag href.
$content = str_replace("<head>", "<head>\n<base href='http://" . $fullUrl . "' />", $content);
echo $content;
Voilá, this site now functions perfectly.
EDIT: Okay, it did not work perfectly.. for some reason. If the URL linked to a file like help.asp, basename() would return with help.asp. I went with a different route:
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
$url = addhttp($url);
preg_match('/^(?:https?:\/\/)?(?:[^#\n]+#)?(?:www\.)?([^:\/\n]+)/', $url, $fullUrl);
$fullUrl = $fullUrl[1];
No more wrong URLs being loaded. This all work... for now.

PHP - different URL link depending on the web page

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

How to force an HTML link to be absolute?

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!

Categories