Url Based Queries

mcomo

New Member
Joined
Jun 15, 2008
Messages
4
Reaction score
0
Hello,
I am very new a this, but I am wondering if someone could tell me how to do this:

I currently use .php includes on my site. I want to be able to include a file based on which url is accessed. I have many different landing pages, but I want the include files to be dependant on which page is pulled up.

I believe I can do it based on the entire url or any portion of it. I want to do it in a manner that takes the least amount of time. I have a feeling either way will be the same.

Thank you for your help.

Mcomo
 
There are a few ways to do it. The easiest (and stupidest) is to use GET.

index.php:
Code:
<?php
include($_GET['page_id']);
?>

then you would visit http://your.domain.here/index.php?page_id=home.htm

Or something like that.

This is only an example!!! DO NOT USE THIS! It's very insecure. Imagine using this to access /etc/passwd ! ! ! Bad stuff
A better way to do it would be to use POST instead of GET

You could also check out

http://www.webmasterworld.com/forum88/6022.htm

for some extra insight
 
I think I may not have been clear in my request or I am so new I don't understand, which is much more likely. :)

Lets say I have the following landing pages:

1. www.mypage.com/prices.php
2. www.mypage.com/selling.php
3. www.mypage.com/advertise.php


I use <?php include("counters.php"); ?> currently.

What I want to do is this

If url = www.mypage.com/prices.php then <?php include("prices.php"); ?>

else if url=www.mypage.com/selling.php then <?php include("selling.php"); ?>

else if url=www.myhpage.com/advertise.php then <?php include("advertise.php"); ?>

endif

I realize the code is incorrect, but that is what I'm hoping to accomplish.

Thank you.
 
I think I may not have been clear in my request or I am so new I don't understand, which is much more likely. :)

Lets say I have the following landing pages:

1. www.mypage.com/prices.php
2. www.mypage.com/selling.php
3. www.mypage.com/advertise.php


I use <?php include("counters.php"); ?> currently.

What I want to do is this

If url = www.mypage.com/prices.php then <?php include("prices.php"); ?>

else if url=www.mypage.com/selling.php then <?php include("selling.php"); ?>

else if url=www.myhpage.com/advertise.php then <?php include("advertise.php"); ?>

endif

I realize the code is incorrect, but that is what I'm hoping to accomplish.

Thank you.

Well, bearing in mind the issues with this listed above, you could do that with the following:

<?php

$this_page_url = $_SERVER['REQUEST_URI'];

if ($this_page_url == "/prices.php")
{
include("prices.php");
}
if ($this_page_url == "/selling.php")
{
include("selling.php");
}
if ($this_page_url == "/advertise.php")
{
include("advertise.php");
}

?>

This doesn't really make a whole lot of sense, though, because your webserver should handle these things (assuming those files are the main dynamic content in your pages), and if not, there are much better ways to handle this! Really!
 
Well, bearing in mind the issues with this listed above, you could do that with the following:

<?php

$this_page_url = $_SERVER['REQUEST_URI'];

if ($this_page_url == "/prices.php")
{
include("prices.php");
}
if ($this_page_url == "/selling.php")
{
include("selling.php");
}
if ($this_page_url == "/advertise.php")
{
include("advertise.php");
}

?>

This doesn't really make a whole lot of sense, though, because your webserver should handle these things (assuming those files are the main dynamic content in your pages), and if not, there are much better ways to handle this! Really!


Wow, Thank You. I think that is exactly what I need.

I have a main page that I copy it's content into multiple landing pages so I can track (PPC) Paid Per Click traffic from each search engine. So other than the code you just gave me (thank you) the pages are identical, but the code you gave me will allow me to bring in search engine specific conversion tracking code. For example if my page is www.mypage.com/msnselling.php then I will use the code you gave me to include the tracking code for msn and not yahoo, google or any of the others.

Any ideas are appreciated.

Thank You again for your time.

mcomo

P.S. I am assuming I can use your code exactly as it is. By that I mean I don't have to put the complete url in the $this_page_url == "/advertise.php". I don't need to put $this_page_url == "www.mypage.com/advertise.php").
 
Last edited:
You can use the code "as is". $_SERVER["REQUEST_URI"] returns the path to the file that's requested without the part up until the top-level domain.

Again, I'd like to mention that you should try to find another mechanism.
And DO NOT think about shortening the code by doing include

Code:
($_SERVER['REQUEST_URI'])

This would allow people to execute off-site code, which is VERY VERY BAD.

At the end of the day, almost anything dynamic in PHP requires that the code be slightly insecure, but you should do what you can to secure your site.
 
You can use the code "as is". $_SERVER["REQUEST_URI"] returns the path to the file that's requested without the part up until the top-level domain.

Again, I'd like to mention that you should try to find another mechanism.
And DO NOT think about shortening the code by doing include

Code:
($_SERVER['REQUEST_URI'])

This would allow people to execute off-site code, which is VERY VERY BAD.

At the end of the day, almost anything dynamic in PHP requires that the code be slightly insecure, but you should do what you can to secure your site.


Is it the part where you are getting the url that's bad or the include part that causes it to be insecure?

As I mentioned all I am doing is including a java script from msn, looksmart etc for conversion tracking. I use the code to determine which of the codes to include.

mcomo
 
Well using the "if" statement makes it better, but simply including whatever is in the URL is bad. Imagine someone including scripts on other sights? Or using it to display privileged information? At least the "if" statement limits it (you are technically still statically including files).

Its actually both parts that are insecure, but as I said before: dynamic pages are inherently insecure
 
Top
Sign up to the MyBroadband newsletter
X