Jump to content

[SOLVED] What is the best way to change page content depending on GET variables


Recommended Posts

On several sites, I have noticed that different content sometimes loads on a page when there is a certain GET variable set in the URL of the page. For example, on an FAQ page, the URL might be "..phphowto/faq.php?page=mail". This would maybe load content related to the PHP mail function.

 

I have three ideas on how to do this:

 

1. When the page loads, get the value of the "page" GET variable, and use include() and this value together to load content from an external PHP file. (For example, if you see "../faq.php?page=mail", we would get that value, add ".php" to the end of it, and load that page... in this case it would be mail.php.)

 

2. Use "if" statements or a "switch" statement to get the value of the GET variable, and depending on its value, either use include() to load content from a certain PHP file, or 3. use echo() to output the desired text without any external files.

 

If I'm not making sense, here are examples of each method above (all in PHP code);

 

1.

//check if the "page" GET variable is set in the URL
if (isset('page')) {
//include the PHP file with the name of the "page" GET variable
include($_GET['page'].".php");
}
else {
//if the variable is not set, load the "main page" content
include("main.php");
}

 

2.

if (isset('page')) {
//using "if" statements
if ($_GET['page']=="mail") include("mail.php");
if ($_GET['page']=="include") include("include.php");
//and so on...

//or using a "switch" statement
switch($_GET['page']) {
case "mail": include("mail.php");
case "include": include("include.php");
//and so on...
}
}
else {
include("main.php");
}

 

3.

if (isset('page')) {
//using "if" statements
if ($_GET['page']=="mail") echo "Stuff about the mail function";
if ($_GET['page']=="include") echo "Stuff about the include function";
//and so on...

//or using a "switch" statement
switch($_GET['page']) {
case "mail": echo "Stuff about the mail function";
case "include": echo "Stuff about the include function";
//and so on...
}
}
else {
echo "Welcome to our PHP FAQ.";
}

 

I haven't tested any of these, but I take it they will all work, and that the first method is the best and simplest, and that the last method is the most disorganized and hardest to maintain.

 

Any tips would be great.

 

Thanks!

Try something like this, that way you're ensuring that people can't just load any random page on your site (a good thing) and you have the default there in case it's not one of your allowed pages. 

 

switch($_GET['page']) {
    case "mail":
    case "include":
        $page = $_GET['page'];
        break;
    default :
        $page = 'main';
}

include($page.'.php');

I always use the second (with a switch) as it's the safest way. (and a switch is easier to read imo than a long if/else statement.

 

If you use the first you'll need to sanitize the input and filter out things like ../, or people might start messing around and visit phphowto/faq.php?page=../admin and it will end up including for example the administrator backend.

Ah, I did not think about people just being able to load any page.

 

T_T Argh, my server is down right now, but I will be sure to test soak's method once it's up and running again.

 

Thanks guys; I will post back here once I've tested it out.

Well, the Web site I had originally wanted to use this on still has its server down, so I tried it with another Web site of mine (with a different server, but the same host). Soak's method works fantastically!

 

Thanks a lot to everyone that helped out!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.