Solved

PHP Conditional on part of URL

Posted on 2010-11-16
8
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 110

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 110

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 110

Accepted Solution

by:
Ray Paseur earned 500 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 110

Expert Comment

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

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

626 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