FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to create a dynamic PHP website.





bdoneck
For those of you that want to have your website so each page is displayed in the browser as "index.php?page=home" and so forth but don't know how, look no further, the answer is right here!

First you need to create an index.php file and in it write the following code:

Code:
<?php

$page = $_GET['page'];
$page = str_replace('..','',$page_name);
$page .= ".php";

if (file_exists($page AND $page != "index.php")) {
  include($page);
} else {
  include("home.php");
}

?>


Now to break the code down.

To retrieve the variable 'page' from the address (index.php?page=page)
Code:
$page = $_GET['page'];

To prevent any misuse (I.E. Changing directories)
Code:
$page = str_replace('..','',$page_name);

To add ".php" to the end of the page name
Code:
$page .= ".php";

This checks if the file $page exists and to prevent a continuous loop, that it is not this page. Then it includes the file if it does exist, and if not, includes the home page.
Code:
if (file_exists($page AND $page != "index.php")) {
  include($page);
} else {
  include("home.php");
}
izcool
I've seen several sites that use this same technique, but I feel that it's a bit pointless in a few ways.

For instance, if you have the following URL : http://www.yoursite.com/index.php?page=home

They can just go into the browse and type in : http://www.yoursite.com/home.php

I don't really see any need on why that should be necessary, I use the same idea, but in a different form.

Let's say this is a contact page :
Code:

<?php
if($_GET[page] == "email"){
echo"eMail addresses here";
}
elseif($_GET[page] == "phone"){
echo"Phone Numbers here";
}
else{
echo"Choose a method :<br><br>
- <a href='$_SERVER[PHP_SELF]?page=email'>eMail</a><br>
- <a href='$_SERVER[PHP_SELF]?page=phone'>Phone</a>";
}
?>


I use this same method for nearly all my pages.

Just my thought.

- Mike.
Scott
I was actually going to make a post asking how to do this.

To the person above me, I think the good part about this is that you can wrap that PHP code in a header and a footer, like the shell of your page. Whenever you want to change your design a bit, or add links to your sidebar, all you have to do is change your index.php file.

At least I think that's how it works.


I might use this, but I am going tos ee if there are any other options (Maybe another method is faster.) One thing I thought about was including my header and footer on each of my pages. So around x.php, y.php and z.php the shell of my page would be loaded, and there wouldn't need to be any if statements.

I think it would be like this, but I don't actually know PHP:

Code:

<?php
include("top.php");
?>

(Hard-coded content)

<?php
include("bottom.php");
?>


I probably messed up the syntax there, but you get what I'm saying.
izcool
Your syntax is fine, I use PHP includes for only one file (my layout file) and use functions to call the header and the footer sections.

- Mike.
bdoneck
izcool, while your method does work, it creates very large files that take time to load and are very hard to edit.

I used to do that but it left me with like 3 files that were all at least 2,000 lines each and about 80 kb.
Grimboy
But the user only has to download the parsed file so it's okay.

I don't see an amazing advantage in doing stuff this way. In my opinion it makes it more likely for search engines to cache two versions of the pagem one of whitch won't have a menu AND puts and extra load on the servers. Remember Occam's razor, well that applies to pretty much everything.

Does anybody know an advantage of doing stuff this way?
Related topics
How to get your dynamic PHP website crawled better by se ?
How To : Secure Your PHP Website
Dynamic User Customizable Sites. CSS + PHP = Awesome!
PHP version 5.0
full PHP website? help need please
Creating a page with php
How to create a php files that contain a image ?
How to create a simple database based website?
Php Begginer
vBulletin installation: Files upload, Database setup
PHP Forum???
Create a Flash Contact form, sends to your email account
create photo website
Am I able to create a temp website for tests and delete it?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.