HTML::link doesn't point to the right website - php

I have this:
{!! HTML::link($item->website, $item->website) !!}
$item->website is the website inserted by the user. But, if the user inserts something like www.mysite.com the result is localhost:8000/www.mysite.com
If in database I put http:// in front of the website, I get the right result.
Is there any other way than HTML::link to properly show the links ?
Thanks.

You have to pass a full URL to the HTML::link(); method, else Laravel will automatically prepend the URL with your local website root. In your case you have to add http:// to your variable and pass the whole URL to the method link();
Check this link ;)
http://laravel-recipes.com/recipes/186/generating-a-html-link

HTML::link() expects the first parameter to be a relative path in the current site, or a fully qualified domain name. You need to check if $item->website starts with "http" and if it doesn't, then prepend it to the string.
<?php $prepend = (strpos($this->website, "http") === 0) ? "" : "http://"; ?>
{!! HTML::link($prepend.$this->website, $this->website) !!}
Notice that I'm checking for "http" instead of "http://". This allows "https" websites to work as well.

Url has to be start with "http://".
If user input does not start with it, then you have to add.

Related

Get current URL with PHP and change a part of it for a new url

I am working on a WordPress website for a client. I want to create a switch element through a current URL change.
For instance;
https://mywebsite.com/shop/CUSTOMER
should change to
https://mywebsite.com/shop/OWNER
echo $ _SERVER ['REQUEST_URI']; ?>
I have found a php code that provides me the URL of the current page. So if the visitor is on the /shop/customer page, I've found a way to create a shortcode and add my own string to it. But it creates a new link like; /shop/customer/owner
I want to replace customer in owner, not adding owner to the current link. Because it has to be a dynamic solution. For example, if the visitor is on https://mywebsite.com/shoppingcart/customer, they should change this to https://mywebsite.com/shoppingcart/owner
Sorry for the bad english, hope you understand what i need.
Regards,
Rick
May be this could work!
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$change_url = str_replace("customer","shopping",$actual_link);
echo $change_url;

external link sends me internally

I am have a form that requests a user to submit a website and then on a different page I send a mysql query to retrieve that website an and turn it into a link by doing this in PHP (V=5.6)
$link = '' . $school_website . '';
the problem is that when i try to click this link, instead of sending me to www.google.com for example, it directs me to www.mydomain.com/www.google.com.
I fixed it originally by using substr to see if the first part was "www" and if it was adding "http://" to it and it worked but now i realize that not all websites will start out with that.
I have tried googling this problem but Im not quite sure how to word the problem so I am not getting anything.
My apologies if this is a duplicate. I did not see anything here that fits my problem, so any help would be greatly appreciated. Thanks.
Could always check if it has http/s:// with regex, if it hasn't then add http:// and the link will work as it should. Or make it ugly but simple.
Simplest way is to remove any protocol and prepend // - that would mark the link as absolute and adopt your current protocol. Even if it didn't have http/s:// it would work as it should.
$school_website = '//' . str_ireplace(['https://', 'http://'], '', $school_website);
Example:
https://google.com becomes //google.com
google.com becomes //google.com
www.google.com becomes //www.google.com
In any of the above cases it would become a absolute url.
A better but longer way would be to validate the url with regex.
Until you add http:// or https://in front of url. It will remain the relative
Like if you re on www.mydomain.com and your href attribute value is www.google.com, The attribute value remain the relative and will target to
you url.
You need http:// or https:// at the beginning of the URL of external links - in fact that should be part of your variable "$school_website", and if that one is for example retrieved from a database, that value has to be changed in the database.

ID variable from URL

Ok so when somebody types this into the URL mywebsite.com/?s1=affiliateid
I want to take the affiliateid part out of the URL. Every affiliate will put a different username into the address.
Then I want to create a link will point to differentwebsite.com/?id=affiliateid based on the username typed into the address bar.
Now so far, I know that I have to have something like this will get that affiliate id
$aff_id = $_GET['s1'];
Then I can use that variable to create a link or just redirect it to the next page
differentwebsite.com/?id=$aff_id
My question is, where do I place this code at? $aff_id = $_GET['s1'];
Do I have to make a page called ?s1.php or something?
Assuming s1 isn't used anywhere else but just to create a link:
<?php
$s1 = isset($_GET['s1']) && !empty($_GET['s1'])
? $_GET['s1'] // it's populated, use the passed value
: ''; // default value in case it's not present
//
// Maybe check $s1 is indeed valid
//
$newurl = sprintf('http://differentwebsite.com/?id=%s', urlencode($_GET['s1']));
?>
Then you can output that link somewhere on the page, like:
New Url Here
urlencode will make sure that if s1 has characters like &, =, ?, / (or others) it won't break the integrity of the url.
If you want the concise approach:
<a href="http://differentwebsite.com/?id=<?= urlencode($_GET['s1']); ?>">
New Url Here
</a>
You could place $aff_id = $_GET['s1'] anywhere before you want to use $aff_id. I tend to put stuff like that at the top of the page.
Or, simply put. "differentwebsite.com/?id=$_GET['id']"
I would suggess you do a check to see if the id parameter exists in the URL before you try to use it. Maybe even make sure it is the data type you expect, integer, string, etc. So as when you redirect users, you don't send them somewhere else in a broken way.
If you are not using this for SQL then no SQL Injection could occur #BlackHatShadow.
Append the $aff_id that you get from mywebsite.com to the url of the new web site. Presumably, $newurl = "differentwebsite.com/?id=".$aff_id.
Edit:
Do I have to make a page called ?s1.php or something?
You need to make a page that the user will land on when they hit the url: www.mywebsite.com/
I assume you are running a web server that can process PHP code. The code can go into a file called index.php in your server's document root directory. If you don't know what this is, I suggest googling a "how to" guide for your specific server.
Get the value of "s1" from the url and store it in $aff_id:
$aff_id = $_GET['s1'];
If you want to pass this variable into another web site which accepts an "id" parameter, then you can simply append $aff_id to the new web URL and redirect the user there.
Redirect the user to differentwebsite.com and also sends the $aff_id from mywebsite.com to the other URL:
header('Location: http://www.differentwebsite.com/?id='.$aff_id);

How to make a website in PHP work both in HTTP and HTTPS?

I have a website that was written assuming http:// is one and only protocol forever. Now i bought a SSL certificate but when i visit site calling it with https:// i get info in browsers that part of site is insecure. As i found i have some JS, CSS and images and files that i refer to using http:// in the HTML of the site.
So what is best practice to enable full https? Should i change my website in every place when i refer to image, CSS or JS, check if site was loaded with http or https and load the resource with according protocol? It seems like a lot of work for me and bit error prone. Is there any other way, easier to make the whole site fully secure?
Rather than linking to your css, js, and images with http://yoursite.com/css/file.css just use relative paths such as /images/image.jpg and /css/file.css this way it will work with both http and https, also if you change domains or copy your content to another domain, you shouldn't have to change all those links either.
Use relative paths. If you are pointing to something that is on the same site as yours, then you should not be using http://
If for some reason you still need to have http:// then just switch them all to https://. An http:// will never complain because it is pointing to https:// stuff, but an https:// page will complain if it is pointing to non-https stuff.
If you are pointing to content outside of your control, on another site for example, then you need to hope that you can get at that content via https instead. If you can't, then you're hosed and you either need to live with the error, get the content from somewhere else, or proxy the content through your own https connection.
To complement #drew010 's answer, you could use other domains and still refer to the current protocol with //, something like:
<img src="/pics/home.png" />
<img src="//my-cdn.com/pics/info.png" />
The latter example will point to https://.. from https://your-site.com and http://... from http://your-site.com.
the best practice would be either using relative path rather than absolute but sometimes absolute is a better option so you can do the following :
as I can imagine you have a file called config.php or common.php (a file that stores your common used vars and you include it in every page), so put this code there :
function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? ''
: ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}
and then you can assign a var called $http to get the value of the function like :
$http = selfURL();
and then whenever you want to include anything like images, css, etc do something like :
<img src="<?=$http?>images/sample.png" />
this method is reliable as it works in any situation.

Get full page URL with PHP

I am trying to get the entire page URL as a string in PHP - so, if the requested URL is ./foo.php?arg1=test&arg2=test2, I get "./foo.php?arg1=test&arg2=test2".
I know that I can get the ./foo.php part from $_SERVER and the variables from $_GET, but I was wondering if there's an easy way to do it in just one fell swoop.
TIA.
$url = (isset($_SERVER['HTTPS']) == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
This should return the full url based on what was typed in the address bar.
If I open the following URL in my browser :
http://tests/temp/temp.php?a=145&b=glop
The following piece of code :
var_dump($_SERVER['REQUEST_URI']);
Gives me :
string '/temp/temp.php?a=145&b=glop' (length=27)
So, $_SERVER['REQUEST_URI'] might be what you are looking for...
$_SERVER['REQUEST_URI']
http://au2.php.net/manual/en/reserved.variables.server.php

Categories