Jump to content

php switch2


Trizen
 Share

Recommended Posts

ok so i was told to make my own post on this.

 

I have a question. The following script was provided to dynamically call up pages on a website. i have done this with the get and include functions on my website but the new script i think i like cause it has potential.

 

<?php
$definedPages = array('home', 'Join', 'progression'); 
$myPath = 'path/to/dir/';

if (in_array($_GET['page'], $definedPages)) {
    require_once $path . $_GET['page'] . '.php';
} else {
    require_once $path . 'home.php';
}
?>

 

ok so i want the $definepages array to be populated automatically from the links in the html. i was told that parsing html = bad so, can i populate the links to the html dynamically and then from that populate my array so that i dont parse the html and can call my pages dynamically?

 

 

Link to comment
Share on other sites

this is what im using now

 

 

<?

$requested = $_GET['p'];

if($requested == "policies"){

include ("p.html");

}


elseif($requested == "join"){

include ("join.php");

}

elseif($requested == "projects"){

include ("projects.php");

}

elseif($requested == "progression"){

include ("progression.php");

}
elseif($requested == "suggest"){

include ("suggest.php");

}
elseif($requested == "Forum"){

include ("Forum.php");

}

elseif($requested == "contact"){

include ("suggest.php");

}

elseif($requested == "about"){

include ("suggest.php");

}

else{ //default page if no $requested

include ("home.html");

}



?>			

 

so is there a way to do it the other way.

Link to comment
Share on other sites

something like this :)

 

<?php

$requested = $_GET['p'];


switch ($requested) {
    case 'policies':
        include('p.html');
    break;
    case 'join':
        include('join.php');
    break;
    case 'projects':
        include('projects.php');
    break;
    case 'progression':
        include('progression.php');
    break;
    case 'suggest':
    case 'contact':
    case 'about':
        include('suggest.php');
    break;
    case 'Forum':
        include('Forum.php');
    break;
    default:
        include('home.html');
    break;
}


?>         

Link to comment
Share on other sites

hon i dont know if u know this or not, but none of the codes in this thread are anyhwhere near actually adding the webpages for you, you have to manually add each site url piece yourself. i dont know where your getting this. if u want the script to do it for u u would have to use some PHP FUNCTION that has somehting to do with getting all the filenames in a directory.

Link to comment
Share on other sites

this should work.

 

<?php
$page_maps = array('policies' => 'p.html', 'contact' => 'suggest.php', 'about' => 'suggest.php');

$requested = $_GET['p'];

if (!empty($requested) && file_exists($requested.'.php')) {
    include($requested.'.php');
}
else if (!empty($requested) && isset($page_maps[$requested])) {
    include($page_maps[$requested]);
}
else {
    include ("home.html");
}

?>         

Link to comment
Share on other sites

It is better practice to use a switch.  A lot of problems with this type of system.

[/quote

just curious :) but what are the problems with what Trizen posted originally? apart from the fact that a switch is faster than an if condition by a few microseconds?

Link to comment
Share on other sites

Thanks rajivgonsalves you the man i tried your script and it works like a charm i love it im going to use it from now on.

 

Please be mindful when using rajivgonsalves's code, it may allow "pages" that you really don't want it to allow.

 

For example, if the presented code was in index.php and the user requested index.php?p=index then the page would try to include itself again and again and again and again and again and again until the max. function nesting level is reached (~100). It also allows the accessing of any readable file on your server like /etc/passwd and if allow_url_include is On then someone could execute their PHP code within your script!

 

If you are going to go with this approach, make sure to only allow accessing files in a specific directory (and sub-directories if you really need them) and be absolutely sure that the only files people can access are those that you want them to be able to.

 

 

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.