?
Solved

How to detect address bar address

Posted on 2009-04-22
16
Medium Priority
?
353 Views
Last Modified: 2013-12-13
How to detect address bar address and develop a function for instance if url is www.microsoft.com it will automaticaly print "you are coming from microsoft.com"

or if url is www.mac.com it will print "you are coming from mac.com"
0
Comment
Question by:Maranellas
  • 8
  • 4
  • 2
  • +1
16 Comments
 
LVL 10

Expert Comment

by:cyberstalker
ID: 24207116
The URL of the currently opened page you can find in $_SERVER['PHP_SELF']

Your question sounds more like you want to get the referrer. This you can get with $_SERVER['HTTP_REFERER']
0
 
LVL 3

Expert Comment

by:SPARC-DESIGN
ID: 24207141
hi there,

here's your solution
echo 'you are coming from ' . $_SERVER["HTTP_REFERER"];

Open in new window

0
 

Author Comment

by:Maranellas
ID: 24212419
i don't need referer i need to detect address bar address.Lets say i type www.something.com and press enter it will open php script that will detect if domain is www.something.com or www.nothing.com
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:cyberstalker
ID: 24212429
I have already posted the solution to this: $_SERVER['PHP_SELF']
0
 

Author Comment

by:Maranellas
ID: 24221722
How can i make function something like this

lets say if PHP_SELF location is something www.something.com

it will print "you are comming from our second site"

or if PHP_SELF location www.nothing.com

it will print "you are comming from different place"
0
 
LVL 3

Expert Comment

by:SPARC-DESIGN
ID: 24223629

<?php
function functionName() {
     if (strpos($_SERVER['PHP_SELF'], "www.something.com") >= 0) {
            return "you are comming from our second site";
     } else if (strpos($_SERVER['PHP_SELF'], "www.nothing.com") >= 0) {
            return "you are comming from different place";
     }
}
 
 
// use it like this
echo functionName();
?>

Open in new window

0
 

Author Comment

by:Maranellas
ID: 24223804
It doesn't seam to work.what it does it basicly loads either google or yahoo into iframe.Instead of going to directory.
<?php
function detectdomain() {
     if (strpos($_SERVER['PHP_SELF'], "www.google.com") >= 0) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/mb/\" width=\"100%\" height=\"100%\"></iframe>";
     } else if (strpos($_SERVER['PHP_SELF'], "www.yahoo.com") >= 0) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/sf/\" width=\"100%\" height=\"100%\"></iframe>";
     }
}
 
echo detectdomain();
?>

Open in new window

0
 

Author Comment

by:Maranellas
ID: 24223829
I mean the function doesn't work when domain is not www.google.com it redirects to folder anyway.it suppose not to
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24232184
There are a couple of problems here... first, when using strpos() you can not test with >=0, because strpos() returns false when the string is not found, and false == 0, therefore (false>=0) is also true. Instead, you must use (strpos(...) !== false).

Secondly, unless you are working for google, $_SERVER['PHP_SELF'] will never be 'www.google.com'. $_SERVER['HTTP_REFERER'] might contain 'www.google.com', if the user clicked a link in the google search result page to get to your page.

If you explain in more detail what you are trying to do, it would be easier to help you.
0
 

Author Comment

by:Maranellas
ID: 24232881
i have two domains linked to my server lets say if customer comes from www.firstdomain.com it will be redirected to certain folder on the server and he comes from www.seconddomain.com it will be redirected to another site.Referrer is not good here i need php to detect actual domain name whats in address bar.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24233325
Then use "strpos(...)!==false" instead of "strpos(...)>=0". Actually, you should use stripos() to make it case insensitive:
if (stripos($_SERVER['PHP_SELF'], "www.firstdomain.com") !== false) {
}
else if (stripos($_SERVER['PHP_SELF'], "www.seconddomain.com") !== false) {
}

Open in new window

0
 

Author Comment

by:Maranellas
ID: 24234527
ok this is how it looks but doesn't seem to work it always redirects to "/easy/" folder even if domain is first or second.


Also another question

lets say if user types "netfasthost.com" istead of "www.netfasthost.com" do i need to use "or" this case,so it will catch both TLD's?
<?php
function detectdomain() {
     if (stripos($_SERVER['PHP_SELF'], "www.netfasthost.com") !== false) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/nfhost/\" width=\"100%\" height=\"100%\"></iframe>";
     } else if (stripos($_SERVER['PHP_SELF'], "www.xvydesign.com") !== false) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/design/\" width=\"100%\" height=\"100%\"></iframe>";
     }
	 else {print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/easy/\" width=\"100%\" height=\"100%\"></iframe>";}
}
 
echo detectdomain();
?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24235674
Instead of PHP_SELF, use HTTP_HOST.

Instead of "www.domain.com", use just "domain.com".
<?php
function detectdomain() {
     if (stripos($_SERVER['HTTP_SELF'], "netfasthost.com") !== false) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/nfhost/\" width=\"100%\" height=\"100%\"></iframe>";
     } else if (stripos($_SERVER['HTTP_SELF'], "xvydesign.com") !== false) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/design/\" width=\"100%\" height=\"100%\"></iframe>";
     }
         else {print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/easy/\" width=\"100%\" height=\"100%\"></iframe>";}
}
 
echo detectdomain();
?>

Open in new window

0
 

Author Comment

by:Maranellas
ID: 24235882
doesn't work either if i configure my windows host file to netfasthost.com and certain ip.It still goes to the "easy" folder...
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24236264
Sorry, I misspelled HTTP_HOST!

If you use an ip address in the address bar, it will go to the /easy/ folder.
<?php
function detectdomain() {
     if (stripos($_SERVER['HTTP_HOST'], "netfasthost.com") !== false) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/nfhost/\" width=\"100%\" height=\"100%\"></iframe>";
     } else if (stripos($_SERVER['HTTP_HOST'], "xvydesign.com") !== false) {
            print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/design/\" width=\"100%\" height=\"100%\"></iframe>";
     }
         else {print "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" src=\"/easy/\" width=\"100%\" height=\"100%\"></iframe>";}
}
 
echo detectdomain();
?>

Open in new window

0
 

Author Closing Comment

by:Maranellas
ID: 31573409
Perfect!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question