Format a website´s URL as a string with http:// upfront - php

I have a comment system that allows auto linking of url. I am using cakephp but the solution is more just PHP. here is what is happening.
if the user enters fully qualified url with http:// or https:// everything is fine.
but if they enter www.scoobydoobydoo.com it turns into http://cool-domain.com/www.scoobydoobydoo.com. basically cakephp understands that http|https is an external url so it works with http|https not otherwise.
My idea was to do some kind of str stuff on the url and get it to insert the http if not present. unfortunately whatever i try only makes it worse. I am noob :) any help / pointer is appreciated.
thanks
EDIT: posting solution snippet. may not be the best but thanks to answer at least I have something.
<?php
$proto_scheme = parse_url($webAddress,PHP_URL_SCHEME);
if((!stristr($proto_scheme,'http')) || (!stristr($proto_scheme,'http'))){
$webAddress = 'http://'.$webAddress;
}
?>

$url = "blahblah.com";
// to clarify, this shouldn't be === false, but rather !== 0
if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) {
$url = "http://{$url}";
}

Try the parse_url function: http://php.net/manual/en/function.parse-url.php
I think this will help you.

I've had a similar issue, so I created the following php function:
function format_url($url)
{
if(!$url) return null;
$parsed_url = parse_url($url);
$schema = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : 'http://';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
return "$schema$host$path";
}
if you format the following: format_url('abcde.com'), the result will be http://abcde.com.

Here is the regex: https://stackoverflow.com/a/2762083/4374834
p.s. #Vangel, Michael McTiernan's answer is correct, so please, learn your PHP before you say, that something might fail :)

Related

Get url from script usage

So i'm writing a script in PHP which generates a image so other people can use it too.
But is it possible to get the url's on the pages the scripts are used ?
For example.
http://www.johnexample.com is using my image with this format
<img src="http://www.myurl.com/image.php">
Now i wan't to receive the url of http://www.johnexample.com without GET variables if possible.
It's basically a script that's suppose to track/note down all the websites that are using my image.
At first i though it was possible with this:
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
But that only get's the location of the script itself.
Thanks
Oh, that was simpler than i though.
Got it working like this now.
The page with the tag only has to load once and it will save.
Only using Session now because it's being tested local.
Gonna switch it over to a database.
Thanks guys
<?php
session_start();
$url = $_SERVER["HTTP_REFERER"];
if(!strpos($_SESSION["url"], $url)) {
if($url != '') {
$_SESSION["url"] = $_SESSION["url"] . "," . $url;
}
}
$tracker = explode(",", $_SESSION["url"]);
var_dump($tracker);
?>

PHP URL Directory

If the URL is the following :
If: http://www.imvu-e.com/products/dnr/
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/?
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/index.php
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/page.php?var=2
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr
Then: http://www.imvu-e.com/products/
How can I do this?
My attempt:
print "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/";
Have a look at parse_url() function.
It returns anything you need.
Simply print_r() the result from parse_url to see what you get back.
You probably want something like:
$ARRurlParts = parse_url($orgurl);
$newURL = $ARRelem["scheme"].
"://".$ARRelem["host"].
((isset($ARRelem["port"]))?":".$ARRelem["port"]:"").
$ARRelem["path"];
The issue with your "attempt" is that $_SERVER['REQUEST_URI'] will contain everything the user passed, including index.php and question mark and possibly more. In order to get what you are after, you need to parse the $_SERVER['REQUEST_URI']:
If it ends with a slash /, leave it as it it
Otherwise, find the last slash in the string and take the substring from the beginning up to and including this slash
Finally append the result onto the http:// (or https:// with the domain name)
Ended up going with this
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$address = $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
$parseUrl = parse_url(trim($address));
$parent = (substr($parseUrl['path'], -1) == '/') ? $parseUrl['path'] : dirname($parseUrl['path']) . "/";
return $parseUrl['scheme'] . '://' . $parseUrl['host'] . $parseUrl['port'] . $parent;
Inspired in part by Erwin Moller's answer (Why I voted it) and snipplets across web.
You can strip everything from the last backslash till the end of the string. I am pretty sure that dirname($_SERVER['REQUEST_URI']) won't do the job. You can also try using dirname($_SERVER['SCRIPT_FILENAME']). The last shoud work if you don't have some fancy .htaccess rewrite rules.

How to retrieve complete URL from address bar using PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get full URL on the address bar using PHP
I use this function, but it does not work all the time. Can anyone give a hint?
function sofa_get_uri() {
$host = $_SERVER['SERVER_NAME'];
$self = $_SERVER["REQUEST_URI"];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$ref = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
return $ref;
}
I want to retrieve the link in address bar (exactly) to use it to refer user back when he sign out. The urls are different:
http://domain.com/sample/address/?arg=bla
http://domain.com/?show=bla&act=bla&view=bla
http://domain.com/nice/permalinks/setup
But I can't get a function that works on all cases and give me the true referrer.
Hint please.
How about this?
function getAddress() {
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
echo getAddress();
You could use functions above to retrieve URL till GET parameters.
So You have string like = 'localhost/site/tmp' (example).
After that you could just loop through GET parameters if can't get anything else to work.
Add '?' at the end of string manually.
$str = 'localhost/site/tmp/?'
foreach ($_GET as $key => $value) {
$str .= $key.'='.$value.'&';
}
substr_replace($str, "", -1);
echo $str;
At the end You are deleting last symbol which is '&' and is not needed.

php add GET at last string

i want to add utm_source=twitter in the of the links
i have a link let say
http://abcd.com/news?id=1
it need to be http://abcd.com/news?id=1&utm_source=twitter
if http://abcd.com/news/1
it need to be
http://abcd.com/news/1?utm_source=twitter
any idea?
To check if your link already has URL parameters on the end of it, look for the ? character in the URL. If it's there, use a & instead.
$link = 'http://abcd.com/news?id=1'; // or http://abcd.com/news
$join_char = strpos($string, '?') !== -1 ? '&' : '?'; // determine if we need & or ?
$link .= $join_char . 'utm_source=twitter';
You can check if the URL already contains a query string and branch your logic accordingly:
if (strpos($url, '?') === FALSE) {
$url .= '?utm_source=twitter';
} else {
$url .= '&utm_source=twitter';
}
If you're simply adding it to the end of a link it would look something like
$link . "?utm_source=twitter";

PHP url validation + detection

So here is what I need to do.
If an user enters this: http://site.com I need to remove http:// so the string will be site.com , if an user enters http://www.site.com I need to remove http://www. or if the user enters www.site.com I need to remove www. or he can also enter site.com it will be good as well.
I have a function here, but doesn't work how I want to, and I suck at regex.
preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_POST['link'])
Use filter_var() instead.
if (filter_var($_POST['link'], FILTER_VALIDATE_URL)) {
// valid URL
} else {
// not valid
}
There is also parse_url function.
I don't think I'd use regex for this, since you're only really checking for what is at the beginning of the string. So:
$link = $_POST['link'];
if (stripos($link, 'http://') === 0)
{
$link = substr($link, 7);
}
elseif (stripos($link, 'https://') === 0)
{
$link = substr($link, 8);
}
if (stripos($link, 'www.') === 0)
{
$link = substr($link, 4);
}
should take care of it.
i always go with str_replace haha
str_replace('http://','',str_replace('www.','',$url))
I think what you're looking for is a multi-stage preg_replace():
$tmp = strtolower($_POST['link']) ;
$tmp = preg_replace('/^http(s)?/', '', $tmp);
$domain = preg_replace('/^www./', '', $tmp) ;
This simplifies the required regex quite a bit too.

Categories