Current location: Hot Scripts Forums » Programming Languages » PHP » Entire site on one php file...


Entire site on one php file...

Reply
  #1 (permalink)  
Old 09-14-04, 06:28 PM
sixflagsga sixflagsga is offline
Newbie Coder
 
Join Date: Sep 2004
Location: Atlanta, Georgia
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Entire site on one php file...

I noticed that on some websites, they use single .php files (such as index.php) for the entire website. I am not aware of how this is done, but I'll give a few examples. The main reason is, I am redoing my website fully with php instead of asp and need it all on one easy-to-update .php file.

1.) For example, one site I came across uses the following:

Homepage: http://www.sitename.com/index.php
Downloads: http://www.sitename.com/index.php?cat=dwnlds

2.) Another site used:

Homepage: http://www.sitename.com/index.php
Downloads: http://www.sitename.com/index.php?act=dwnlds

And so on. If someone could provide me with this kind of script, without copyright notices, I'd appreciate it.
Reply With Quote
  #2 (permalink)  
Old 09-14-04, 06:38 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
the query string ?cat=dwnlds probably includes a file into index.php called dwnlds.php or something similiar.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Reply With Quote
  #3 (permalink)  
Old 09-14-04, 06:45 PM
ralphg3 ralphg3 is offline
New Member
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
there a few php frameworks that use single entry point controller (index.php) model. take a look these two:

phpmvc
--------
http://www.phppatterns.com/index.php...rticleview/11/
http://www.phpmvc.net/

fusebox
---------
http://bombusbee.com/articles.view/id/6.htm
http://bombusbee.com/
Reply With Quote
  #4 (permalink)  
Old 09-14-04, 06:56 PM
hardcoded's Avatar
hardcoded hardcoded is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
I used a single php file for my whole site a couple of months ago. However, XSLT is a much better solution for this kind of stuff. Don't forget that PHP uses much more CPU than serving a simple html file. Besides, the resulting central php script will become quite ugly if it is used for every of your pages, trust me.

I got details about XSLT on my website.
__________________
http://www.hardcoded.net
Reply With Quote
  #5 (permalink)  
Old 09-14-04, 10:19 PM
rjwebgraphix rjwebgraphix is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by hardcoded
Besides, the resulting central php script will become quite ugly if it is used for every of your pages, trust me.
In my opinion, This really depends on the purpose for the site and how many pages it's serving. A relatively small site like mine it's perfect. I only have 7 pages of information to be displayed to the users and a limited amount of bandwidth to do it in, so I'm always looking for ways to cut down bandwidth. This way I'm loading one file with only the content changing. Granted, if you had a massive site with thousands of pages of information, I probably wouldn't do it like this either as it would get quite boring to look at after a while.
Reply With Quote
  #6 (permalink)  
Old 09-14-04, 10:19 PM
bugalyzer bugalyzer is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
1 file

I use this method on all my sites, check out http://dynadocs.org - it's an entire repository application and all you have to do is include it in any file (a template file) and it acts the same way.

Basically - heres what you want to do:

index.php:

<html>
<body>
template html code here

Menu:
<a href=<? echo $PHP_SELF?>?page=file1>File 1</a>
<a href=<? echo $PHP_SELF?>?page=file2>File 2</a>

include "$page.php";

</body>
</html>

-- Create 2 files, file1.php and file2.php and call the above php page on your webserver. You'll see that the url always calls index.php and index.php displays file1.php or file2.php based on the link you click. Build on this method and you'll have some extremely powerful applications that can be integrated anywhere.

http://buildacom.com
Reply With Quote
  #7 (permalink)  
Old 09-14-04, 11:04 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
I wrote something exactly like this on another topic. It's the best way to do it.

http://www.programmingtalk.com/showpost.php?p=34683
Reply With Quote
  #8 (permalink)  
Old 09-14-04, 11:34 PM
ThePatronus HP ThePatronus HP is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
I use this method on my site, so that certain information can be categorized by certain pages, such as books.php, then i just use the books.php page to call up certain books. This way, my links look similar to this...

books.php?book=bookone

Now, what you need to do in order to make your URL like this is create each individual file that you will be calling. I find it easiest to do each file using an HTML page with the layout being done in HTML itself, because it's so much easier than constantly writing out print ""; statements, and then everything requiring PHP or MySQL can be done in the PHP file itself. So, let's create examples!

First, you should probably create a header and footer file, as most sites do, and save them to be called up in the PHP file at the beginning and end, this way you dont have to put the same information into every single file you create, which can be extremely time-consuming in the long run. So, do that first! I save mine as .php files to sort them from the rest.

You said you wanted everything to run off of one page, which will probably be index.php, so we'll work with that. Let's say your first page (aside from the index itself, we'll work on that later) is for an 'about us' page. Start a new PHP document with whatever editor file you use (DreamWeaver, Cute FTP) and make it index.php, but leave it blank for now. Then, start a new HTML document that will be used as your 'about us' page. Put in all of the information you want on the 'about us' page using HTML. Save the document as whatever you want to call it (I usually save my documents as *.tpl, and I name it something like aboutus_body, but you can save it as .htm or .html, whatever fits your needs best!), and place it into a convenient folder on your site, such as /site_pages (Reason being because then all of these included files are out of the way when you upload other things onto your server, and makes it very organized!). Now, go back to your index.php page, and this is where we begin the conditional statement code. Let's say you want your link to look something like index.php?page=aboutus. The 'switch' that will be used in the PHP code is going to be named 'page', but if you want it to be something else that fits your site better, you can call it whatever you want. You're going to need to enter the following information...

PHP Code:

<?php


include('/site_pages/header.php');  //(You can call your header file whatever you want, just be sure you place the information that will be on EVERY page on the top and/or left into this file, because it makes it really easy later on!)

if(isset($_GET['page']))  //(Remember, you can switch 'page' to whatever you want!)
{
   switch(
$_GET['page'])  //(Again, whatever you call from the $_GET will need to be placed here as well!)
   
{
      case 
"aboutus":  //(You can call the page whatever you want, but I'm calling it aboutus because that is what we're working with!)
      
include('/site_pages/aboutus_body.tpl'); //(As I stated above, this is how I save my files, but you can save them as whatever you want!)
      
break;
   }
OK! We're not finished yet. That's only for one conditional, but you stated you want many. I'm hoping by now you've got the idea, though. All you need to do is copy that same case 'filename': information for each one, and you've got that part! Now, you need to create your main index page, and we're going to place that into the else statement that comes at the end of the file. Call your index page whatever you want, just as the other files, but for here I'll call it index_body.tpl. So, the end of the code will look like this...

PHP Code:

}

else
{
   include(
'/site_pages/index_body.tpl'); //(Wherever you place all of your pages, call that file and the index file in this part, that way when people type in your URL this is the first page it will go to!)
}
include(
'/site_pages/footer.php'); //(Same as header file, just call the footer whatever you want, but place everything that will be on EVERY page on the bottom and/or right in this file!)

?> 
So, that's the finished code. I'll do it all together below (without my helpful suggestions!) with a second conditional statement to show you how it works all together.

PHP Code:

<?php


include('/site_pages/header.php');

if(isset(
$_GET['page']))
{
   switch(
$_GET['page'])
   {
      case 
"aboutus":
      include(
'/site_pages/aboutus_body.tpl');
      break;

      case 
"games":
      include(
'/site_pages/games_body.tpl');
      break;
   }
}
else
{
   include(
'/site_pages/index_body.tpl');
}

include(
'/site_pages/footer.php');

?>
That's it! If you need anymore help with this, because I may have placed too much information in one deal, then either send me a PM, or you can email me at thepatronushp at thepatronus.com. If you're interested, I'll even setup the index.php file for you, and you just let me know what you want all of the files to be called. I'll do it for free because it's really, really simple and won't take that much time, otherwise, if you want the practice, you can go at it and just let me know what's going on if there are any other problems.

And if you're looking for links that look like this...

/index.php?page=aboutus&info=sixflagsga

...then you need to use more conditional statements INSIDE of the conditional statements, which I can help with if you're interested as well, but is also really, really simple. So, get back to me!
Reply With Quote
  #9 (permalink)  
Old 09-15-04, 06:14 AM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
Just as a security thing make sure that the acual $_GET isn't the file that it's looking for ie:
PHP Code:

$page $_GET["page"];

$full_page $page '.php';
if(
file_exists($full_page){
include
'$full_page';
}
else {
include
'404.shtml';

That can be hacked exteamly easaliyto show all your passwords since the ".php" can be escapsed but a NULL charecter (which i will not say) then the url can be something like yoursite.com/index.php?page=.../etc/passwd {followed by null charecter} and BAM! all your passwords are shown. So make sure to use the switch and case like ThePatronus HP said and not the way shown above.

Last edited by Eclipse; 09-15-04 at 06:15 AM. Reason: fixed code ~ added if statment
Reply With Quote
  #10 (permalink)  
Old 09-15-04, 06:38 AM
hardcoded's Avatar
hardcoded hardcoded is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
That is what I mean when I say that this kind of PHP stuff is ugly. Outputting a couple of html tags from a couple of ".tpl" files. Urg...

you could have XML content pages that look like:

Code:
<mypage>
  <title>Doc title</title>
  <menu name="mainmenu"/>
  <content>
    <title>My Title</title>
    <text>
      My Content
    </text>
  </content>
</mypage>
And render it with a XSLT like

Code:
<xsl:template match="mypage">
<html>
<head>
  <title><xsl:value-of select="title"/><title>
</head>
<body>
  <xsl:apply-templates select="menu"/>
  <!-- write your headers here, and where you wrap your content -->
  <xsl:apply-templates select="content"/>
</body>
</html>
</xsl:template>
Much cuter isn't it?

Embrace the W3C... They're the answer.
__________________
http://www.hardcoded.net
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code to edit a text file mdhall Script Requests 12 12-23-10 04:03 AM
Using Flash, PHP, & a Text file bxcheats Script Requests 0 06-09-04 05:53 PM
PHP code on a remote file tonniar .rm PHP 2 05-24-04 02:32 AM
compare the contents of 2 file in php rani PHP 5 04-14-04 11:47 PM
Use PHP to download .html file from another site (Like LWP::Simple in Perl) ? kevin PHP 4 07-03-03 01:57 AM


All times are GMT -5. The time now is 04:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.