Xtreeme Posted November 20, 2007 Share Posted November 20, 2007 Hi, got a new question. My goal: Have contents section of my index page dynamic. That is to have just the content div change when links are clicked (kinda like frames in html). This much I have gotten to work, as long as the included files and the index is in root folder of site. Which brings us to the problem. I would like to keep clutter down. So it would help to have all other included pages in a folder. Only the home index page will be in root folder. As I understand it, since Im using variables for dynamic content aswell. I have to declare the root as something else in index so that when the code calls for "root" it really goes to say /mystuff/webpages for example. Any ideas? Im following this guide for includes and variables. http://phpro.org/tutorials/Introduction-to-PHP-templating.html His code is working fine. However its all set to have included pages in the same directory as the index (all in root folder of site). Example of links <li><a href="<?php echo $_SERVER['PHP_SELF'];?>">Home</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page1">Page One</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page2">Page Two</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page3">Page Three</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page4">Page Four</a></li> as I understad it the PHP_SELF is what does this. Now I read something that said you could define a diff "home" path so that PHP_SELF points to a diff directory then server root. But must be declared at the beging of page. This is where Im stuck.....how do I do this. Ive tried on solution but the code didnt work right. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Daukan Posted November 21, 2007 Share Posted November 21, 2007 You might be thinking of set_include_path() I never heard of being able to change php_self.. it always points to the script its running in. If it is in an included file it points to the file it was included in not itself. So no matter where you put your included files php_self should work. Quote Link to comment Share on other sites More sharing options...
Xtreeme Posted November 21, 2007 Author Share Posted November 21, 2007 not sure.... :-\ all I really want is to have the included file taken from somewhere else than root or where the index is. So if that code will do that, Im up for it! Any advice on how to do this is appreciated. Ill try that code now. Here is what I was trying to change home directory with. However Im just confusing myself, since I'm not really sure which is best way to achieve what I want. <? define("$home_path", "/mysite"); ?> But it was literally a stab in the dark. Now this code you gave me. It goes at top of page? Should it look like this set_include_path(/mysite) That is if mysite was a folder in root.... Thanks. Quote Link to comment Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 The best place to set your include path is in your .htaccess file. Add this line... php_value include_path ".:/path/to/includes" Now, any files you have within /path/to/includes can be simply included using.... <?php include "foo.php"; ?> As for this.... <li><a href="<?php echo $_SERVER['PHP_SELF'];?>">Home</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page1">Page One</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page2">Page Two</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page3">Page Three</a></li> <li><a href="<?php echo $_SERVER['PHP_SELF'];?>?page=page4">Page Four</a></li> The entire php part is not needed. Links (and form actions) default to the same page unless told otherwise. <li><a href="">Home</a></li> <li><a href="?page=page1">Page One</a></li> <li><a href="?page=page2">Page Two</a></li> <li><a href="?page=page3">Page Three</a></li> <li><a href="?page=page4">Page Four</a></li> Quote Link to comment Share on other sites More sharing options...
Xtreeme Posted November 21, 2007 Author Share Posted November 21, 2007 OK that works with a typical include. I added that to .htaccess and any include does default to the directory I set it to. But by trying to make it dynamic it breaks somehow. Im not good enough with code to see what Im doing wrong. This is what Im putting in the content div. The problem is with this code somewhere (Im sure its user error not the code itself) <?php // create an array of allowed pages $allowedPages = array('page1', 'page2', 'page3', 'page4'); // check if the page variable is set and check if it is the array of allowed pages if(isset($_GET['page']) && in_array($_GET['page'], $allowedPages)) { // first check that the file exists if(file_exists($_GET['page'].'.php')) { // If all is well, we include the file include_once($_GET['page'].'.php'); } else { // A little error message if the file does not exist echo 'No such file exists'; } } // if somebody typed in an incorrect url else { // if things are not as they should be, we included the default page if(file_exists('main.php')) { // include the default page include_once('main.php'); } else { // if the default page is missing, we have a problem and it needs to be fixed. echo 'main.php is missing. Please fix me.'; } } ?> Anything wrong with this code? Im getting the error 'No such file exists' so its not finding the include directory. But a simple include works......I have no idea why! My inlcude folder is two directories from root. mystuff/webpages for example. Like I said regular includes are working. However I want includes to change just the content div..........Im stuck. LOL. Thanks for all the help so far guys. Quote Link to comment Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 file_exists requires a full path, it will not search your include path. Quote Link to comment Share on other sites More sharing options...
Xtreeme Posted November 21, 2007 Author Share Posted November 21, 2007 OK, that clears that up. So what is the best solution. Can I give the full path in the file_exists command? It appears by this that it can be done, but not sure if Im reading it correctly. http://www.w3schools.com/php/func_filesystem_file_exists.asp Like this if(file_exists($_GET[mystuff/webpages'page'].'.php')) If not could I just remove the check for file code, without security issues that is? Quote Link to comment Share on other sites More sharing options...
trq Posted November 22, 2007 Share Posted November 22, 2007 Indeed you must (as I just said) give a path to file_exists.... if(file_exists('mystuff/webpages/' . $_GET['page'].'.php')) Quote Link to comment Share on other sites More sharing options...
Xtreeme Posted November 22, 2007 Author Share Posted November 22, 2007 Thank you! Ah it works. Made me so happy to actually see it do just what I wanted for the first time. I cant tell you how great it is to have help like this. You saved me days of frustration looking at random google searches. Best of all I understand why its not working, and why the solution fixed it. I like learning not just being spoon fed. Folks here do that, they explain it. So great site, great people and great help! So thanks again thorpe, for taking time and helping folks like me out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.