Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Conditional on part of URL

Posted on 2010-11-16
8
Medium Priority
?
338 Views
Last Modified: 2012-05-10
Hey Experts!!

I need to have a conditional statement based on a URL.  But, I only need the www.mydomain.com part of the URL.  Anything pre or post that part is irrelevant.  

I'm poking around in PHP so please be gentle.  Any and all help is appreciated!!
0
Comment
Question by:LZ1
  • 4
  • 4
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34148837
Please do us a favor - post the test case data, OK?  What are we starting with here?  Thanks, ~Ray
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34148848
And then tell us what kind of conditions to look for.

This may be something we can do with REGEX or with some combination of string functions.
0
 
LVL 30

Author Comment

by:LZ1
ID: 34148882
Sorry for the incomplete question Ray.

Actually the only code I have is getting the current page URL, which is below.

This is basically what I need:

if PageURL contains abc.com
        echo ('image')
elseif PageURL contains xyz.com
        echo('image2')


<?php
					function curPageURL() {
					 $pageURL = 'http';
					 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
					 $pageURL .= "://";
					 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;
					}
					?>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Author Comment

by:LZ1
ID: 34149230
I came across this.  Not sure if it's useful or not.  
<?php
						if (substr($_SERVER['REQUEST_URI'], 7, 15) == 'mydomain.com') {
    						echo 'We should see this if we are on mydomain.com';
							}
					?>

Open in new window

0
 
LVL 30

Author Comment

by:LZ1
ID: 34149644
I think I have it.  

I came up with this by searching for a string inside of the URL.  If anyone sees room for improvement, please let me know!!
<?php
	$url = $_SERVER['HTTP_HOST'];//gets current page url
	if (stristr($url, 'mydomain1.com') == true)
		echo ('<a href="http://mydomain.com/">
			<img src="http://content-1.s3.amazonaws.com/images/skis/tag-cloud.jpg" alt="Save On Packages" title="Save On Packages"  width="250px"/>
		</a>');
	elseif(stristr($url, 'mydomain2.com') == true)
		echo "Womens Skis are better "; 
	elseif(stristr($url, 'mydomain3.com') == true)
		echo "SNOWBOARDS.NET"; 
?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 34150318
ID:34149644 -- that looks right to me.  In almost all instances, $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; will give you the current, fully qualified URL, including the GET arguments.  To skip the GET arguments, change REQUEST_UTI to PHP_SELF.
0
 
LVL 30

Author Closing Comment

by:LZ1
ID: 34159024
Perfect Ray! Thanks!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34159031
Thanks for the points - excellent question, ~Ray
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

916 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