External links add domain to front of link, even when using http:// - php

Problem: External links have our domain name to the front of the link.
In database the following string is stored:
To learn more about Rich Habits <a href=”http://www.externaldomain.com”>click here.</a>
In our PHP File we echo the string as such:
</p><?php echo Author::getAuthorBio( $post->author1 ) ?></p>
The resulting HTML from a browser is as such:
<p>To learn more about Rich Habits <a href=”http://www.externaldomain.com”>click here.</a></p>
But, when clicking on link, the url is:
mydomain.com/”http://www.externaldomain.com”
How do I make link correct?

You are trying to quote the value of the attribute with ” instead of ". The ” is not a valid character for quoting attributes in HTML, so it is being treated as part of the URL.
Since ”http:// is not a valid URL scheme, it is being treated as a relative URL.
Replace the ” with ".
Your problem is most likely caused by writing your HTML in something other than a text editor. Word processors have a habit of replacing straight quotes with typographic quotes. This is mistake when dealing with code instead of English.

Your ” around the href attribute are not double quotes. They are special characters. Replace them with " and it'll fix it.

Related

Is it possible to create a hyperlink to a page that has Double quotes within the path?

I want to create hyperlinks to pages on my site but the page address have double quotes within them?
Eg:
the above just links to mysite.com/search.php?q= as I would expect as it is written.
The API returning results allows phrase searches by placing them in double quotes.
Is there a way to escape these within the href tag?
Simple solution: use altenative quotes:
<a href='mysite.com/search.php?q="sales+manager"&l=usa'></a>
This will work fine (the browser will make sure the URL gets properly formatted when a user clicks it), but you should really be urlencoding special characters because there's a whole bunch of stuff that you're not allowed to use in URLs, and some stuff that has a different meaning (in a URL, spaces become +, for instance, so you can't drop in a + and get it to stay that once you parse it. URL magic!).
Have a look at urlencode and use that when generating the link URL server side. This will turn things like spaces into %20, double quotes into %22, etc., and is how you send literal string data from a client to a server.
Yo must encode the quotes &quot
mysite.com/search.php?q="sales+manager"&l=usa
Is there a way to escape these within the href tag?
Yes, with the escape character. \.
Although, the current state of your code would produce:
effectively breaking the href since you are breaking the string.
What you want, is just:
...
you are considering whether the characters need to be escaped in order to work in your HTML.
however, you should also consider whether they need to be escaped in order to be sound URLs.
to work in your HTML you may do
<a href="mysite.com/search.php?q='sales+manager'&l=usa">.
however, the ' character cannot be in a URL.
"Uniform Resource Locators may only contain the displayable characters in the standard ASCII character set. Nondisplayable characters or characters in the extended ASCII set (128 through 255) are specially encoded."
See here for a list of URL escape codes.
perhaps you want to retain the quotes in the get-request of your URL. in that case, you might want:
<a href="mysite.com/search.php?q=%22sales+manager%22&l=usa">

Appending a string to the end of a URL in a variable in PHP

So I have this code for a youtube link in a wordpress widget
$title = "<h5 class='widget_title sidebar_widget_title'><a
href='http://www.youtube.com/user/".$options['username']."'
target='_blank'>".$options['title']."</a></h5>";
So I'm trying to append
?sub_confirmation=1
after my youtube user name so that a subscribe confirmation comes up but wow I have tried everything and am just not a good enough coder yet.
You just insert the query string parameter to the string.
$title = "<h5 class='widget_title sidebar_widget_title'><a href='http://www.youtube.com/user/".$options['username']."?sub_confirmation=1' target='_blank'>".$options['title']."</a></h5>";
The reason this is possibly confusing to you is because the author of the code you're changing used " to tell PHP that the next sequence of characters form a string, and ' to tell HTML that this is the value for the href attribute (like <a href='youtube.com'>...</a>). This works because HTML supports both " and ', and it saves you the hassle of having to escape quotes.
Personally, I'd rather use ' for PHP-strings and regular " for HTML, but that's a matter of taste.

Query string (with space in between) in php html

I am trying to do a query string in html.
String that I want to pass is "Book Cover".
But I only managed to get Book.
How should I go about doing it?
Below is my code:
<a href=book.php?category=Book Cover>Book Cover</a>
You need to encode all your query string vars, For example with rawurlencode / rawurldecode
Book Cover
And in PHP:
$category = rawurldecode($_POST['category']);
In HTML the value stops at the space:
<a href=book.php?category=Book Cover>Book Cover</a>
^
If you want to include a space inside a value in HTML you need to add quotes:
Book Cover
^ ^
In HTML both single and double quotes are allowed.
Now the value itself has a problem, too:
book.php?category=Book Cover
`- URL stops here.
This is a relative HTTP URL and as for any HTTP URL the space character is a special value. It can normally not be part of the URL, therefore you need to encode it. This can be done as with any other special character in a HTTP URL with triplet encoding / percentage-encoding replacing the binary value of the character(s) with their hexadecimal number:
book.php?category=Book%20Cover
For the space you have, historically it is even a special-case, you can also encode it with the plus sign.
The later problem is often dealt with by the user agents, but the quotes in HTML are needed otherwise the value gets cut.
And it is generally good practice to place attribute values in HTML inside (double) quotes. So I suggest you to do that.
Why not convert to UTF-8 before encoding?
urlencode(utf8_encode($string));
Looks like you are missing double quotes
Book Cover

php/javascript Best way to escape quotes in querystring

I use ajax with php to output hyperlinks enabling user to email some text. The link points to a mail script and the querystring contains the text to mail. By clicking on the link, the user mails the text. However, if text (also user created) contains a quote mark, this quote mark appears in the querystring and breaks the code. Specifically, the browser thinks the querystring ends when it encounters the quote. The text after the quote spills out onto the page. I could enclose the hyperlink in an apostrophe instead of a quote, but then I'd have the same problem if the user included an apostrophe in the text.
I have tried to escape these special characters using json_encode but that did not work. I know there is addslashes or maybe I could hack somethin with str_replace, but would like to do this the right way.
Thanks for any suggestions on the right way to do this.
php
go to dbase
$text = $row['text'];
echo 'email text';
/*
say for sake of argument text is 'the dimensions of the painting are 24" x 36"'
then above link is
echo 'email text';
which breaks code
*/
urlencode should be used, since you're encoding the text to put it in a URL.

Single quotes turning url into twins?

Just noticed that when using single quotes to echo a basic link in php, the url repeats itself.
<?php
echo 'Link URL - Single Quotes<br />';
?>
The above code outputs the link as:
http://example.com/"http://example.com/"
Can anyone shed some light on the reason for this?
You shouldn't \-escape your " when you're using ' to surround the string as a whole. This couldn't create that output itself, but it might confuse a parser somewhere down the line, producing the problem. Try this instead:
echo 'Example.com<br />';
Use PHP to output dynamic data and leave the HTML out of it. This will save you hours of quotation frustration
?>
Example.com<br />
<?php
// carry on with the PHP
echo 'Example.com<br />';
outputs
Example.com<br />
The backslashes are included in the final output and most likely trip up the HTML parser.
You're escaping the double quotes. It isn't necessary when using single quotes and vice-versa.
<?php
echo 'Example.com<br />';
?>
The above code outputs the link as:
http://example.com/"http://example.com/"
No, it doesn't produce that output.
This is what you see in the browser when you put the cursor over the link and when you click on the link. It's part of the browser's job to resolve the relative and incomplete links, but what it shows to the user is, most of the times, not what it is written in the HTML code.
Use the browser's "View Source" functionality to see the HTML generated by your code.
The (invalid) HTML produced by your code is:
Link URL - Single Quotes<br />
The browser interprets \"http://example.com\" as the value of the href attribute. The HTTML attribute values can be either enclosed in quotes (") or apostrophes (') or unquoted at all and the quoting character must be the first non-space character after the equal sign (=). Because it finds a backslash (\) after the equal sign, it concludes the attribute value is not quoted and read everything until the first whitespace or until the tag ends (>) as the attribute's value.
The value \"http://example.com\" is not a valid URL and the browser handles it as an incomplete URL. An incomplete URL needs to be resolved to a complete URL in order to be used. It doesn't look like a relative URL (doesn't start with ..), it doesn't look like an absolute path without a host name either (doesn't start with /). The only way to resolve it is to treat it as a file name located in the same directory as the page that is currently loaded. Chances are that your offending code runs in a page located in the root of your website (http://example.com/index.php, for example).
I won't provide a fix for your problem here. The question already have plenty of answers that provide you various ways to avoid this happen.
However, take a look at the strings documentation page in the PHP manual. All you need to know is explained there.

Categories