Trizen Posted November 20, 2009 Share Posted November 20, 2009 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 More sharing options...
Cardale Posted November 20, 2009 Share Posted November 20, 2009 It is better practice to use a switch. A lot of problems with this type of system. Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961695 Share on other sites More sharing options...
Trizen Posted November 20, 2009 Author Share Posted November 20, 2009 im still new to php so can you help with a link or an example of a switch. Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961699 Share on other sites More sharing options...
emopoops Posted November 20, 2009 Share Posted November 20, 2009 wth is a switch ? is that sland term for you newer people to php? Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961710 Share on other sites More sharing options...
Trizen Posted November 20, 2009 Author Share Posted November 20, 2009 ok im not up on my terminalogy but i think what im using now is a switch. but i want to try something like i was explaining. if it can be done can someone offer some help or advice. Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961711 Share on other sites More sharing options...
emopoops Posted November 20, 2009 Share Posted November 20, 2009 oh ur looking for a php FUNCTION that will display a list of urls in the same directory on your site as links? Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961714 Share on other sites More sharing options...
Trizen Posted November 20, 2009 Author Share Posted November 20, 2009 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 More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 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 More sharing options...
Trizen Posted November 20, 2009 Author Share Posted November 20, 2009 no look at my first post. thats what i want to do. Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961729 Share on other sites More sharing options...
emopoops Posted November 20, 2009 Share Posted November 20, 2009 why on ever eearth would u want to put pages on a page liike that Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961730 Share on other sites More sharing options...
Trizen Posted November 20, 2009 Author Share Posted November 20, 2009 because i would not have to update my script every time i add a link to the site it would do it for me. Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961731 Share on other sites More sharing options...
emopoops Posted November 20, 2009 Share Posted November 20, 2009 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 More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 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 More sharing options...
Trizen Posted November 20, 2009 Author Share Posted November 20, 2009 thats what i been asking for. how to i make a script to find all the filenames then and then populate my definedpages array with those names. Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961741 Share on other sites More sharing options...
emopoops Posted November 20, 2009 Share Posted November 20, 2009 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 More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 why you want to find all files and put them in the definepages you can just use file_exists to check if it exists as illustrated and include it Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961743 Share on other sites More sharing options...
Trizen Posted November 20, 2009 Author Share Posted November 20, 2009 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. Thanks a Lot Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961744 Share on other sites More sharing options...
emopoops Posted November 20, 2009 Share Posted November 20, 2009 http://www.trap17.com/index.php/Sort-Files-Directory-Php_t36366.html this one makes sense Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961745 Share on other sites More sharing options...
Cardale Posted November 20, 2009 Share Posted November 20, 2009 Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961754 Share on other sites More sharing options...
Koobi Posted November 20, 2009 Share Posted November 20, 2009 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 More sharing options...
salathe Posted November 20, 2009 Share Posted November 20, 2009 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 More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 thanks salathe I was thinking of telling him about it but did not how to word it Link to comment https://forums.phpfreaks.com/topic/182250-php-switch2/#findComment-961815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.