darth_tater Posted April 2, 2007 Share Posted April 2, 2007 Ive come here because this community is by far the largest when it comes to PHP development and troubleshooting. as i continue to build upon my websites, i have a feeling a dedicated community membership will become *Very* helpful. with that out of the way, lets proceeded my question. here is the contense of a particular PHP file that i happen to be working on, and it partially works correctly. <? $urlarray = explode('/',$HTTP_SERVER_VARS['PATH_INFO']); //get the URL and cut it up so that everything after the phpfile.php is passed on. // EG index.php/1/2/ would return 1/2/ // array_shift($array); $modules = array( // Define our array of valid moduels. theese are individual PHP files that do certan Functions. aka /index/ or /edit/. Add more below to define more functions in the URL. 'index', 'edit', ); $module = $urlarray[0]; // The first key contains what we will use as our module $page = $array[1]; // The second key contains what we will use as the page number... note: i dont need this yet! but will soon; planning ahead never hurt anybody if(empty($module)) // I need a default module { $module = "index"; //should i add somethign here about includeing the welcome page or something?... maby in INC folder w/ in the main root? include "./modules/index/index.php"; echo("the module is $module"); } if(in_array($module, $modules)) // Does the requested module exist?... removed { include "./modules/{$module}/index.php"; // If yes: Include it } else { die("Failed to load module '{$module}'"); // If no: Error message } ?> basically, all it does is check to see if there is an extension to INDEX.PHP, and if so, display it. as defined in an array, there can only be two valid 'extensions' either www.site.com/index/ or www.site.com/edit/ however, if i go to www.site.com w/o any 'extensions' it correctly displays the ./modules/index/index.php file, then echo("the module is $module"); but then, the script hits the next IF statement, and the page is re displayed!!! how can i change this? THANKS FOR ALL THE HELP!... this newb appreciates it and hopes to learn from your input! Link to comment https://forums.phpfreaks.com/topic/45341-if-function-giving-me-problems-please-help-this-newb/ Share on other sites More sharing options...
Barand Posted April 2, 2007 Share Posted April 2, 2007 I think you need <?php if(empty($module)) // I need a default module { $module = "index"; //should i add somethign here about includeing the welcome page or something?... maby in INC folder w/ in the main root? include "./modules/index/index.php"; echo("the module is $module"); } else { if(in_array($module, $modules)) // Does the requested module exist?... removed { include "./modules/{$module}/index.php"; // If yes: Include it } else { die("Failed to load module '{$module}'"); // If no: Error message } } ?> Link to comment https://forums.phpfreaks.com/topic/45341-if-function-giving-me-problems-please-help-this-newb/#findComment-220145 Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 It generally isn't a good idea to put comments inside of definitions first off and usually you do not want the trailing , in the array definition if there is nothing else left. I would change the modules arr definition to this: // Define our array of valid moduels. theese are individual PHP files that do certan Functions. aka /index/ or /edit/. Add more below to //define more functions in the URL. $modules = array( 'index', 'edit' ); Next the $page = you are defining it to $array when you state the "second key" shouldn't that be the $urlarray? $page = $urlarray[1]; // The second key contains what we will use as the page number... note: i dont need this yet! but will soon; planning ahead never hurt anybody Next use elseif, you are just using if which means that potentionally the second if statement will get executed although you defaulted the value: if(empty($module)) // I need a default module { $module = "index"; //should i add somethign here about includeing the welcome page or something?... maby in INC folder w/ in the main root? include "./modules/index/index.php"; echo("the module is $module"); }elseif(in_array($module, $modules)) // Does the requested module exist?... removed { include "./modules/{$module}/index.php"; // If yes: Include it } else { die("Failed to load module '{$module}'"); // If no: Error message } ?> See how that works out. Link to comment https://forums.phpfreaks.com/topic/45341-if-function-giving-me-problems-please-help-this-newb/#findComment-220148 Share on other sites More sharing options...
darth_tater Posted April 2, 2007 Author Share Posted April 2, 2007 It generally isn't a good idea to put comments inside of definitions first off and usually you do not want the trailing , in the array definition if there is nothing else left. I would change the modules arr definition to this: your right, that was me just being sloppy and typing this quickly. Next the $page = you are defining it to $array when you state the "second key" shouldn't that be the $urlarray? again, i was being sloppy and going too fast. i didn't notice it BC i haven't yet used $page Next use elseif, you are just using if which means that potentionally the second if statement will get executed although you defaulted the value: See how that works out. Link to comment https://forums.phpfreaks.com/topic/45341-if-function-giving-me-problems-please-help-this-newb/#findComment-220191 Share on other sites More sharing options...
darth_tater Posted April 3, 2007 Author Share Posted April 3, 2007 the fixed IF statement keeps the page from re-printing its self, but i still can't get it to work right. say i go to http://<serverIP>/ the php script will run and show me the file located in /modules/index/, just like it should. however, when i try to go to http://<serverip>/edit/ i get a 404 error. is this because apache is immediately looking for the directory /edit/ instead of passing it on to the index.php file like it should? how can i fix this? also, lets say that i go to http://<serverip>/index.php/edit/ well, youd think that /edit/ would get passed on to the index.php file and it would show me the corresponding /modules/edit/index.php file... but nooo. i still get the main page, as if i had passed no arguments on to index.php Link to comment https://forums.phpfreaks.com/topic/45341-if-function-giving-me-problems-please-help-this-newb/#findComment-220214 Share on other sites More sharing options...
darth_tater Posted April 6, 2007 Author Share Posted April 6, 2007 Bump? i still need a bit of help... the php code looks as if it should work, but its not working as expected.... i think it has something to do with the way apache is set up, but im not entirely sure. i looked at the access.log and the error.log files and i think i know what the problem is. i used this for my main index.php so that i could take advantage of clean URL's instead of having stuff like www.site.com/index.php?pg=234908?var2=234?var3=dfskshfdks etc... but because there are no traditional variables (only separated by /) when i request www.site.com/edit/ apache actually looks for a directory called /edit/ withink my webserver httdocs root, instead of passing /edit/ onto index.php so my question is this: how can i create a rewrite rule so that ALL traffic destined to www.site.com is forced to go to the index.php instead of the more traditional "look to see if that directory exsists, then go there for index.php" PS: yes i know this is no longer about the if function, and it is now about apache not understand my clean URL's... but i can only edit my last post... will a mod please fix that? Link to comment https://forums.phpfreaks.com/topic/45341-if-function-giving-me-problems-please-help-this-newb/#findComment-222951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.