I am trying to grab statistics on my site and I need a way in PHP to assign just the referrer URL to a variable. All I want is just the "www" or subdomain, the domain, and the tld... I.E. www.facebook.com instead of http://www.facebook.com/BLAH-BLAH/?blah=blah
Try something like the following:
$referer = $_SERVER['HTTP_REFERER'];
$parts = parse_url($referer);
echo $parts['host']; //should echo example.com, or whatever
<?php
$ref = $_SERVER["HTTP_REFERER"];
$host = null;
if ($ref != null) {
$parse = parse_url($ref);
$host = $parse["host"];
}
?>
and then you have $host to work with
Related
I am attempting to redirect example.com to subdomain.example.com using codeigniters (3) redirect($domain). But what I end up with in the browser address bar is base_url/$domain when all I want is $domain.
The controller is set to look at the url and determine if the user has visited without a subdomain. If that is the case they are redirected to a view that asks them where they are from. This data is then passed to the same controller with a $_POST telling it where they are from.
$this->load->helper('url');
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain = $subdomain_arr[0];
if ($_POST != NULL)
{
$post = $_POST;
$subnames = $this->sendy->subName($post);//returns subdomain
foreach ($subnames as $subname)
{
$sub = $subname->subdomain;
}
$subdomain = $sub;
$period = ".";
$domain = $subdomain.$period.$subdomain_arr[0].$period.$subdomain_arr[1];
redirect($domain, 'auto');
}
There are thousands of possible subdomains. I just need codeigniter to prepend the subdomain and reload the page so that the url appears in the browser address bar as subdomain.example.com.
Derp! I added http:// to the $domain var and now it works as expected. Sorry for the asshatery.
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain = $subdomain_arr[0];
if ($_POST != NULL)
{
$post = $_POST; //this is the library name
$subnames = $this->sendy->subName($post);
foreach ($subnames as $subname)
{
$sub = $subname->subdomain;
}
$subdomain = $sub;
$period = ".";
$http = "http://";
$domain = $http.$subdomain.$period.$subdomain_arr[0].$period.$subdomain_arr[1];
redirect($domain, 'location');
}
With the addition of the $http now the redirect works properly.
I'm getting a url from a form, this way:
$input_website = isset($_POST['website']) ? check_plain($_POST['website']) : 'None';
I need to get back a naked domain name(for some API integration), for example: http://www.example.com will return as example.com
and www.example.com will return example.com etc.
I have this code now, that returns the correct url for the first case http://www.example.com but returns nothing for www.example.com or even example.com:
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
Can you please advice on the matter?
As per discussion with you:
$url = 'www.noamddd.com';
$arrUrl = explode("/", $url);
echo $arrUrl[0];
Old Answer:
Make a function with the following code block and get the domain names.
Try this
more about parse_url
$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
print $parse['host']; //google.com
Also you can do this in another way:
echo $domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));//google.com
If you just have the URL (and not want the current domain name like frayne-konok suggests) and want to extract the server name, you can use a regular expression like this:
$serverName = preg_replace('|.*?://(.*?)/.*|', '$1', $url);
I ended up doing something a bit different - checking if there is http and if not, i'm adding it using this function:
function addHttp($website) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $website;
}
and only then i'm sending it to my other function that return the domain.
For sure not the best way, but it works.
I have the following problem: I have a set of domains with the same url structure:
domain-a.com/london/
domain-b/london/
domain-c/london/
I want to do the following thing:
If you are on domain-a.com/london/, I want "related" links underneath pointing to domain-b.com/london/ and domain-c.com/london/
I want these links to appear automatically using the URL of the current page, remove the domain so that only the rest is left - in my example: /london/ and add the other domains in front of this.
I know I have to use echo $_SERVER['REQUEST_URI']; to get the rest of the URL but I don't know how to create a link using this function.
<?php
$url = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
function generateLink($url, $uri){
if(strpos($url,'domain-a.com') !== false){
$link = 'http://domain-b.com' . $uri;
return $link;
}else if(strpos($url,'domain-b.com') !== false){
$link = 'http://domain-c.com' . $uri;
return $link;
}else if(strpos($url,'domain-c.com') !== false){
$link = 'http://domain-a.com' . $uri;
return $link;
}
}
?>
Link
If trying to add a extra path in URL after my domain in all urls
$extra = "en";
$domain = "http://domain.com";
$current = currentURL();
$rest_path = str_replace($domain,'',$current);
$result = $domain."/".$extra.$rest_path;
// $result is "http://domain.com/en/mysub/mysub"
After this so I redirect my site via using PHP redirect
To get current url is doing like..
function currentURL() {
$pageURL=(#$_SERVER["HTTPS"]=="on")?"https://":"http://";
if($_SERVER["SERVER_PORT"]!="80"){
$pageURL.=$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}else{
$pageURL.=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
It's look like many steps , Any easier to do that ? Or review my bad coding pls.
PS : Try to do without using .htaccess
I would use just:
$extra = "en";
$domain = "http://domain.com";
$result = $domain."/".$extra.$_SERVER['REQUEST_URI'];
since you are not using protocol or domain name.
$extra = 'en';
$domain = $_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI'];
$new_url=str_replace('.com','.com/'.$extra,$domain);
No?
I'm trying to figure out how to check if the referral url to one of my inner pages is the homepage. This would be easy if the homepage was always www.mysite.com/index.php but what happens when it's simply www.mysite.com?
I know I could simply do
$url = $_SERVER['HTTP_REFERER'];
$pos = strrpos($url, "/");
$page = substr($url, $pos+1, (strlen($url)-$pos+1));
if (substr_count($url, 'index')) echo 'from index ';
but I don't have the index.php in my $url variable.
parse_url() can help you here.
// An array of paths that we consider to be the home page
$homePagePaths = array (
'/index.php',
'/'
);
$parts = parse_url($_SERVER['HTTP_REFERER']);
if (empty($parts['path']) || in_array($parts['path'], $homePagePaths)) echo 'from index';
N.B. This should not be relied upon for anything important. The Referer: header may be missing from the request, and can easily be spoofed. All major browsers should do what you expect them to, but hackers and webcrawlers may not.
Use this
if($_SERVER["REQUEST_URI"] == "/" || $_SERVER["REQUEST_URI"] == "/index.php")
echo "Home";
$url = parse_url($_SERVER['HTTP_REFERER']);
$url = explode('/',$url['path']);
if ($url[1]=='index.html'||empty($url[1])) echo 'from index ';
$referer = $_SERVER['HTTP_REFERER'];
$homepage = "index.php";
$ref_array = explode("/", $referer);
if(trim($ref_array[1]) == trim($homepage) || trim($ref_array[1]) == "") echo "From URL";
You should note that yoursite.com/index.php and yoursite.com/ is the same!
This would work:
if ($_SERVER['REQUEST_URI'] == '/')