Solved

PHP Conditional on part of URL

Posted on 2010-11-16
8
331 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 109

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 109

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 109

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 109

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now