Jump to content

php switch2


Trizen

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
https://forums.phpfreaks.com/topic/182250-php-switch2/
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
https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961715
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
https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961727
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
https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961735
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
https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961739
Share on other sites

well theres a php function for it i suppose.http://php.net/manual/en/function.readdir.php

<?php
$dir = ".";

$dh = opendir($dir);

while (($file = readdir($dh)) !== false) {
        echo "<<strong class="highlight">A</strong> HREF=\"$file\">$file</A><BR>\n";
}

closedir($dh);
?>

?

 

Link to comment
https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961742
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
https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961791
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
https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961812
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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