Solved

PHP Conditional on part of URL

Posted on 2010-11-16
8
329 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 108

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 108

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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 108

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 108

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 86
Do we need servers??? 5 131
Paging Using PHP 7 32
confirm popup before record delete takes place 87 23
This article discusses four methods for overlaying images in a container on a web page
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

706 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

21 Experts available now in Live!

Get 1:1 Help Now