[PHP] Get function, not working as it should.. Or?

Joined
May 12, 2005
Messages
251
Reaction score
9
Okay short and simple..

PHP:
<?PHP
if(isset($_GET['page']))
{
	switch($_GET['page'])
	{
		case 'test1':
			include('moduler/test1.php');
			break;
		case 'test2':
			include('moduler/test2.php');
			break;
		default:
			include('default.php');
			break;
	}
}
?>

The page doesn't load "default" by default i need to call it by enter index.php?page=default

And my question is why doesn't god love me and make it work? ^^
The default page is both included in root and in a subfolder called "moduler" Have i done something wrong?

Please help! :)

EDIT:

By changing the code to

PHP:
<?PHP
if(isset($_GET['page']))
{
	switch($_GET['page'])
	{
		case 'test1':
			include('moduler/test1.php');
			break;
		case 'test2':
			include('moduler/test2.php');
			break;
			
		    default: echo "Error 404 this page doesn't exist on xxxxxxx.com" ;
	}
}
?>

It gives me the error message when i try to enter a module that doesn't exist.. Wich is good and bad since i want it to load a separate module when another one isn't called and when another one that's called doesn't exist.

EDIT2:

By havning
PHP:
		case 'test2':
			include('moduler/test2.php');
			break;	
		    default: include('moduler/default.php');
	}
}
?>
instead it successfully load the default page WHEN i try to load a page that doesn't exist. So my final question is, how to configure the code so it loads default by default aswell when i visit my front page
 
If you don;t set it in the URL, then it isnotset()! So, by using the isset() function, it will never run the code inside the if, whenever no value at all is passed. Remove the if :)

Also, for convenience I always use brackets after each case, makes life more structured.
 
It's not difficult...just do what I told you:

PHP:
<?php

    switch($_GET['page'])
    {
        case 'test1':
        {
            include('moduler/test1.php');
            break;
        }
        case 'test2':
        {
            include('moduler/test2.php');
            break;
        }
        default:
        {
            include('default.php');
            break;
        }
    }
?>
 
Aha, sorry and thanks. Just one thing. Now when i've worked on the page abit the case list is getting REALLY big, since i have many subpages lol and you said something about brackets? Any ideas, tips to make the "list" shorter as in less code?
 
Make sure you put code that keeps coming back in a function, or things that apply to every case can go before or after the switch.
 
Aha, sorry and thanks. Just one thing. Now when i've worked on the page abit the case list is getting REALLY big, since i have many subpages lol and you said something about brackets? Any ideas, tips to make the "list" shorter as in less code?
Dynamic routing. Instead of explicitly declaring each possible scenarios, your "front controller" will lookup the module file, and if it exists loads it:
PHP:
<?php
function route($controller = 'home')
{
    // Assert that controller contains no illegal symbols
    if(!preg_match('/\.\.\//', $controller)) {
        $filePath = 'pages/' . $controller . '.php';
        if(file_exists($filePath)) {
            // File found, require it and return true
            require $filePath;
            return true;
        }
    }
    
    // Something went wrong, display 404
    require 'pages/404.php';
    return false;
}

route($_GET['page']);
That's it at minimum! Dropping files into pages/ folder will make them load when they are requested.
 
Why not create an array of pages?
PHP:
//Using keys and values in case you wanted a different name for the GET page name and file name.
$pages = array(
   'test1' => 'test1',
   'test2' => 'test2',
);

//If the get is set (This will remove any errors) and it is in the keys of pages array.
if(isset($_GET['page']) && array_key_exists($_GET['page'], $pages))
{
   include('moduler/'.$_GET['page'].'.php');
}
else
{
   include('default.php');
}
 
Or why not just do
PHP:
if($_GET['page'])
{
	include 'moduler/'.$_GET['page'].'.php';
} else {
	include 'default.php';
}
Bout as simple as it gets?

Edit: Didnt see greenowls post. >.> But still same concept.
 
Or why not just do
PHP:
if($_GET['page'])
{
	include 'moduler/'.$_GET['page'].'.php';
} else {
	include 'default.php';
}
Bout as simple as it gets?

Edit: Didnt see greenowls post. >.> But still same concept.

Because
a) your code poses a security flaw. (?page=../)
=> preg_match() will take care of validating the URL.
b) wrapping routing logic into function allows us to reuse it, resulting in nested pages!
PHP:
<?php
// This file is a module.
?>
<h1>Example module.</h1>
<?php
route('news');

Greenowl, why do you need an array? What's the advantage over dynamically loading? Filling in an array with keys and values still takes a lot more work than loading the module automatically.
 
If you don;t set it in the URL, then it isnotset()! So, by using the isset() function, it will never run the code inside the if, whenever no value at all is passed. Remove the if :)

Also, for convenience I always use brackets after each case, makes life more structured.

It won't load default.php if the ?page is blank, as you have coded a check so see if isset().
Okey Daney? :o
 
PHP:
$page = $_GET['page'];
if (file_exists("moduler/" . $page . ".php") == true)
{
	include("moduler/" . $page . ".php");
}
else
{
	echo 'The requested page is unavailable.';
}
?>
 
I know it uses up more CPU cycling through the keys and more memory storing they keys, but it will probably only be called once (and we're also talking about bytes, at most kilobytes in difference). As for the advantage, he may want the &page=home to load say home-42.php in which yours wouldn't without extra coding or changing the file name. I have not done performance testing so I couldn't necessarily say that either is better.

As for making it a function, it is preferable.
 
I know it uses up more CPU cycling through the keys and more memory storing they keys, but it will probably only be called once (and we're also talking about bytes, at most kilobytes in difference). As for the advantage, he may want the &page=home to load say home-42.php in which yours wouldn't without extra coding or changing the file name. I have not done performance testing so I couldn't necessarily say that either is better.

As for making it a function, it is preferable.
I understand your point. In theory it would be best to merge these two, such as checking the "override" array first, and then fall back to my dynamic loading if a key was not found. In fact, file_exists() is an I/O operation and thus more resource intensive than array management.
 
PHP:
$page = $_GET['page'];
if (file_exists("moduler/" . $page . ".php") == true)
{
    include("moduler/" . $page . ".php");
}
else
{
    echo 'The requested page is unavailable.';
}
?>

I second this..

Because the query in his link should be the same as the filename he wants to link to. It only makes sense that they're the same. If it's not the same, change the link to match, not the code. Also, use .htaccess to create an error page for pages that don't exist.. it's a common procedure.

PHP:
$page = 'moduler/'.$_GET['page'].'.php';
if (file_exists($page) == true)
{
    include($page);
} else if(file_exists('404.html')
{
    include('404.html');
} else
{
     echo 'File does not exist.';
}
?>
 
Last edited:
Back