欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Get current domain 当前域名 有大用

 



I have to get the domain url (ex. http://www.example.com/) in laravel blade. I've tried using {{ url() }} but it returns the path to my public directory. Is there any one line function to get this? How do I get the domain in blade? Need help. Thanks.

shareimprove this question
 
   
you mean now it returns 'var/www/app/public' or 'www.app.com/public' ? – naneri Jan 17 '16 at 9:12
   
try {{ Request::root() }} – JLPuro Jan 17 '16 at 9:20
   
no i just need 'www.example.com' – Paul Jan 17 '16 at 10:31

1 Answer 正确答案

You can also try

{{ Request::server ("SERVER_NAME") }} # 得到 "aaa.com"

Or go with  {{ Request::root() }} # 得到 "http://aaa.com"

shareimprove this answer
 

来自 https://stackoverflow.com/questions/34836469/how-to-the-domain-url-in-laravel5

I have my site on server

http://www.myserver.uk.com

For this i have two domain:

http://one.com

and

http://two.com

i would like get with PHP current domain, but if I use $_SERVER['HTTP_HOST'] then this show me

myserver.uk.com

instead of:

one.com or two.com

How can i get domain, not the server name? I have PHP version 5.2

shareimprove this question
 
   
You can only get primary URl. Which one is Primary out of those three ? – jolly.exe May 23 '12 at 9:39
1 
Exactly how your two domains 'redirects' requests to your server? – infgeoax May 23 '12 at 9:41
   
@infgeoax probably a frame... – CodeCaster May 23 '12 at 9:41
   
primary is myserver.uk.com. so how can i get current domain name? If i open site with address one.com i would like get one.com instead of myserver.uk.com – Tony Evyght May 23 '12 at 9:53
   
@TonyEvyght that's the point infgeoax and I try to make, you should get the host name you're connecting with in $_SERVER['HTTP_HOST']. If the sites one.com and two.com are "redirecting" using an (i)frame, the page itself still comes from myserver.uk.com, so you won't get the real domain. What is the HTML source for one.com? – CodeCaster May 23 '12 at 11:29

6 Answers 正确答案

up vote100down voteaccepted

try using this: $_SERVER['SERVER_NAME']

or parse

$_SERVER['REQUEST_URI']

EDIT:

apache_request_headers()

shareimprove this answer
 
   
this show me also name server instead of name domain – Tony Evyght May 23 '12 at 9:52
   
ok, try with apache_request_headers() – onehalf May 23 '12 at 10:01
3 
-1: With this answer alone, I do not know exactly what the different suggestions I am looking at do. Sure, this gives me a point to continue looking from, but by itself this is really not a good answer... – Jasper Oct 27 '15 at 13:35
4 
just print_r(apache_request_headers()) and you'll understand all :) – onehalf Nov 25 '15 at 11:01
   
This answer helped me identify that $_SERVER['HTTP_X_ORIGINAL_HOST'] had the value I need. – Sarah Lewis Jun 21 at 16:57

Using $_SERVER['HTTP_HOST'] gets me (subdomain.)maindomain.extension. Seems like the easiest solution to me.

Edit: if you're actually 'redirecting' through an iFrame you could add a get parameter which states the domain.

<iframe src="myserver.uk.com?domain=one.com"/>

And then you could set a session variable that persists this data throughout your application.

shareimprove this answer
 
1 
mostly importantly it includes the port number so that I do not need to concat it afterwards. phpinfo suggested by bsdnoobz helps me to find the right solution though. – Dummy Aug 8 '14 at 4:14

The best use would be

echo $_SERVER['HTTP_HOST'];

And can be used like this

if(strpos( $_SERVER['HTTP_HOST'], 'banana.com') !== false){
    echo "Yes this is indeed the banana.com domain";
}

This code below is a good way to see all the variables in $_SERVER in a structured HTML output with your keywords highlighted, that halts directly after execution. Since I do sometimes forget which one to use myself - i think this can be nifty.

<?php 
// change banana.com to the domain you were looking for.. 
$wordToHighlight= "banana.com";
$serverVarHighlighted = str_replace( $wordToHighlight, '<span style=\'background-color:#883399; color: #FFFFFF;\'>'. $wordToHighlight .'</span>',  $_SERVER );
echo "<pre>";
print_r( $serverVarHighlighted );
echo "</pre>";
exit();
?>
shareimprove this answer
 

Try $_SERVER['SERVER_NAME']. Tips: Create a PHP file that calls the function phpinfo() and see the "PHP Variables" section. There is a bunch of useful variables we never thought there.

shareimprove this answer
 
   
this show me also name server instead of name domain – Tony Evyght May 23 '12 at 9:52
   
you can always try print_r-ing the $_SERVER and search – user1299518 May 23 '12 at 10:08

I know this might not be entirely on the subject, but in my experience, I find storing WWW-ness of current URL in a variable useful.

Edit: In addition, please see my comment below, to see what this is getting at.

This is important when determining whether to dispatch Ajax calls with "www", or without:

$.ajax("url" : "www.site.com/script.php", ...

$.ajax("url" : "site.com/script.php", ...

When dispatching an Ajax call the domain name must match that of in the browser's address bar, otherwise you will have Uncaught SecurityError in console.

So I came up with this solution to address the issue:

<?php
    substr($_SERVER['SERVER_NAME'], 0, 3) == "www" ? $WWW = true : $WWW = false;

    if ($WWW) {
        /* We have www.example.com */
    } else {
        /* We have example.com */
    }
?>

Then, based on whether $WWW is true, or false run the proper Ajax call.

I know this might sound trivial, but this is such a common problem that is easy to trip over.

shareimprove this answer
 
   
OP explicitly asked for the domain, and not the SERVER_NAME. – Sebastian G. Marinescu Aug 23 '16 at 18:17
   
True, but these days you have to worry about the www issue too. – InfiniteStack Aug 23 '16 at 18:19
   
Why? In JS you could look in window.location. In PHP you got SERVER_NAME. – Sebastian G. MarinescuAug 23 '16 at 18:23
3 
SERVER_NAME returns "www.site.com" even when "site.com" is entered into the address bar. If you are using SERVER_NAME throughout your code, inevitably you will run into the www/no-www security issue, especially when it comes to making Ajax calls. But to answer your question, in advanced PHP programming, sometimes PHP needs to dynamically generate code that makes an HTTP call to the server. If the target URL contains "www" on a page that doesn't, it will generate a security error. – InfiniteStack Aug 23 '16 at 18:28
   
Ok ok... I read about it and you are right. So your answer might be relevant for someone. Good job :) – Sebastian G. Marinescu Aug 23 '16 at 18:35

Late answer, but here it goes:

$currentDomain = preg_replace('/www\./i', '', $_SERVER['SERVER_NAME']);

It will give you a clean domain name, without www


来自 https://stackoverflow.com/questions/10717249/get-current-domain
普通分类: