Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
[PHP] How to load pages through "index.php".
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="RastaLulz" data-source="post: 69164" data-attributes="member: 1"><p>Hello,</p><p> </p><p>Today I will be showing you a very basic way of loading all your pages through "index.php", rather than having to make a separate file for each page with the same template, includes, etc.</p><p> </p><p>[PHP]<?php</p><p> </p><p>// here you could include the header of your site</p><p>// require_once 'skin/header.php';</p><p> </p><p>/* CONFIG */</p><p> </p><p>$path = 'path/to/pages/'; // path to the folder where you store pages</p><p> </p><p>$default_page = 'home'; // if no page is set, this will be included</p><p> </p><p>$default_404_page = '404'; // if the page does not exist, this will be included</p><p> </p><p>/* SLAVE */</p><p> </p><p>if(empty($_GET['page'])) { // check if you have set the page in the URL</p><p> </p><p> $page = $default_page; //set the default page set in the config since no page was set in the URL</p><p> </p><p>}else{</p><p> </p><p> // the code below checks if the page contains only letters, numbers, underscores and/or dashes</p><p> // it also checks if the file exists</p><p> </p><p> $page = (preg_match('/[^a-zA-Z0-9\ _-]/', $_GET['page']) || !file_exists($path . $_GET['page'] . '.php') ? $default_404_page : $_GET['page']);</p><p> </p><p>}</p><p> </p><p>// includes the page correct page</p><p> </p><p>require_once $path . $page . '.php';</p><p> </p><p>// here you could include the footer of your site</p><p>// require_once 'skin/footer.php';[/PHP]</p></blockquote><p></p>
[QUOTE="RastaLulz, post: 69164, member: 1"] Hello, Today I will be showing you a very basic way of loading all your pages through "index.php", rather than having to make a separate file for each page with the same template, includes, etc. [PHP]<?php // here you could include the header of your site // require_once 'skin/header.php'; /* CONFIG */ $path = 'path/to/pages/'; // path to the folder where you store pages $default_page = 'home'; // if no page is set, this will be included $default_404_page = '404'; // if the page does not exist, this will be included /* SLAVE */ if(empty($_GET['page'])) { // check if you have set the page in the URL $page = $default_page; //set the default page set in the config since no page was set in the URL }else{ // the code below checks if the page contains only letters, numbers, underscores and/or dashes // it also checks if the file exists $page = (preg_match('/[^a-zA-Z0-9\ _-]/', $_GET['page']) || !file_exists($path . $_GET['page'] . '.php') ? $default_404_page : $_GET['page']); } // includes the page correct page require_once $path . $page . '.php'; // here you could include the footer of your site // require_once 'skin/footer.php';[/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
[PHP] How to load pages through "index.php".
Top