Solved

PHP Conditional on part of URL

Posted on 2010-11-16
8
333 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

735 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