Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

fully dynamic website with php

Options
  • 18-09-2006 10:13am
    #1
    Closed Accounts Posts: 2,175 ✭✭✭


    Guys, I think I've just had a brainwave (or an aneurysm....).

    I'm working on a comercial site for a client. It's is a huge undertaking, I'm doing it all myself, and when I started I had no idea what the hell I was doing. To a large degree I still don't.
    But what I want to ask is, is it possible to have a one page website, as in one index page where the entire site is generated on that page dynamically by php??

    We're talking a large database driven site, with lots of user driven content. So there would be search queries/results, user interface for adding content, and members only sections.

    Thanks


Comments

  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Why would you want all pages coming from index.php?
    I mean, it's possible but OMFGS you're talking about running a switch on some get variable (like $act)... just think of the content of index.php

    case "doSomething":
    code
    break;
    case "doSomething":
    code
    break;
    case "doSomething":
    code
    break;
    case "doSomething":
    code
    break;
    case "doSomething":
    code
    break;


    six million and four times.


    No.


    Seperate out into relevant page names what things are supposed to be doing.
    login.php is a logical place for logging in
    account.php (or user.php) for all your user related stuff
    logout.php is a logical place for logging out.


    One control file for the entire site is not a good idea


  • Registered Users Posts: 4,188 ✭✭✭pH


    Interesting question, it's definitely possible using something like

    site.com/index.htm as your main page and then

    all links then got back to index.htm with parameters for example:

    site.com/index.htm?action=listusers
    site.com/index.htm?action=logout
    site.com/index.htm?action=postnew
    site.com/index.htm?action=editpost&postid=9384

    However as mentioned above, why?


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    Yeah ok, but take the user login/ loggout for example. I want it done dynamically. It doesn't have it's own page with a login form, it just sits in a corner of every page and changes depending on whether they are logged in or not. Dynamic, like.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Again with the "yes, it's possible. But why?"

    If you use smarty for controlling your display, then you have no problem with dyamic pages and you can have
    [php]
    {if $user}{include file="logoutLink.tpl"}{else}{include file="login.tpl"}{/if}
    [/php]

    But the idea of one location for the entire site is an incredibly bad idea. It will mean that you're going to have URIs like
    index.php?act=login
    index.php?act=logout
    index.php?act=userdetails
    index.php?act=doSomthing

    And so your switch is going to have six million, four hundred and ninety five thousand and three, two hundred and seven and a half switch cases

    One page per discreet idea is best.

    Hell, if what you're talking about is a big project, then MVC implementing Data Transfer Objects, Data Access Objects, Managers and controllers is the way to go, certainly not just an index.php with above (hyperbolic) number of switch cases


  • Registered Users Posts: 4,188 ✭✭✭pH


    Also if what you're doing even remotely behaves like a content management system then you'd be far better starting with an existing package and customising it to your needs.

    http://en.wikipedia.org/wiki/List_of_content_management_systems

    Even if you're not going to use one, there's a lot of free examples there, which would allow you to see how problems have been solved.

    If you do decide to use one as a base, if they have a GPL type license you'd need to publish your source code, but some may have BSD style licenses so you can just take the code and do with it what you want.


  • Advertisement
  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    Yeah the one page is a bad idea.
    But that bit of code there looks interesting. I don't know what smarty is.
    I was think about having the login as part of the header originally. Obviously this involves sessions, which I'm quite iffy about still. My Html background has not prepared me for this programming lark.

    EDIT: as regards what I'm doing, think "carzone" or "myhome"....


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Smarty is a PHP templating engine designed to seperate content from code.
    And if you want to do something on the scale of myHome.ie, I can't stress strongly enough how myc I recommend a pattern like MVC


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    Thanks for the info about smarty. Looks dead handy. But I have to admit I've never heard of MVC.


  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    Using functions would give you some of the same benefits of a one-page site (such as having all the important code in one place).

    Basically you create a function :
    [php]
    function page_wrap ($content) {
    print "<div>" . $content . "</div>";
    }
    [/php]
    and then call it wherever needed :
    [php]
    page_wrap("whatever is here will become \$content");
    [/php]

    Your actual pages could be just a couple of function calls, with all your code resting in a single include file.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    chamlis wrote:
    But what I want to ask is, is it possible to have a one page website, as in one index page where the entire site is generated on that page dynamically by php??

    We're talking a large database driven site, with lots of user driven content. So there would be search queries/results, user interface for adding content, and members only sections.

    Yes, of course it is! I can see where you're going, it's a very basic and simple system to implement, and vBulletin uses it to a large extent. It can be something along the lines of index.php?p=news in which case it prints standard headers and footers and selects a column from a database which has something along the line of "pagename=news" and that loads a set of settings and text to populate the page.

    Simple, effective, and incredibly bad practice! I don't recommend it all.

    Read up on the "import" statement and import standard classes of DB settings and pages containing error messages and seperate headers and footers, and call them from your code as you need them. It make things a lot more secure, easier to maintain and simple, while removing the need for a full object-orientated layout (which I would recommend you use if you're producing a properly large website)


  • Advertisement
  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    You hit the nail on the head there civilian_target. I realise it's very bad practice now though :D

    Since ye kind of have an idea of what I'm doing (see previous posts. I really can't say exactly what) are there any templates or programs out there to help? I'm trying 'phpwebsite' at the moment, but there are issues with PEAR (I only discovered PEAR today...) that I just have no idea what to do with.

    I would say I have working knowledge of MySQL, a novice amount of PHP knowledge (so I could do, say, a basic php/MySQL commerce/catalog site at this stage), but the requirements for what I'm doing are far more advanced I think.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    It is possible to write an entirely dynamic site in PHP or any other scripting language, but rarely advisable. There are a number of reasons for this, many of which are related, off the top of my head:
    1. You would end up with a very big and unmanageable file. Try and imagine putting all the business logic of an entire site into a single script - All the content, all the form handling, all the functions. Very soon you’d end up with a monster that would become very complex and confusing to maintain. It also would make modularisation and code reuse much more difficult.
    2. Scripts are typically interpreted (including PHP unless things have radically changed, and yes I know that you can in theory compile it, but few do) and not running as compiled native code (such as servlets). This means that your very big single script would end up being interpreted every time it’s called.
    3. Some pages don’t need to be dynamic and making them so will simply add unnecessary overhead to your application server. Things such as ‘about us’ or ‘contact’ pages tend to be updated only once every few years, and some content (such as in many blogs and wikis) are written once and stored as static files to save of processor power.
    4. Search engines like nice descriptive URLs and multi page sites.
    Of course, putting everything in one script can work in theory and every now and then in practice. I remember coming across a PHP script a few years back that essentially combined all the pages of a Webmail client in one script, which worked well and was cleverly done.

    However, normally doing something just because it appears clever is not a good idea.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Why not use index.php for controlling all the actions and have seperate files to deal with those actions.

    Examples: index.php?action=showproducts , index.php?action=contactus

    then index.php something like this:
    [php]
    $action = $_GET;

    switch ($action)
    {
    case 'showproducts':
    include('modules/showproducts.php');
    break;
    case 'contactus':
    include('modules/contactus.php');
    break;
    default:
    include('modules/home.php');
    break;
    }
    [/php]


  • Registered Users Posts: 1,656 ✭✭✭rogue-entity


    I would never recommend putting all your eggs in one basket.. but, you can have index.php?page=news type URIs without a load of switch statements and one massive index.php.

    Here is how I would do it.
    [COLOR="Red"]<?php[/COLOR]
    [COLOR="Blue"]$act = $_GET[[COLOR="Red"]'act'[/COLOR]];[/COLOR]
    
    [COLOR="SeaGreen"]switch([/COLOR]$act[COLOR="SeaGreen"]) {[/COLOR] // Do Login, Logout, Show News Stuff, Show Main Stuff
    [COLOR="SeaGreen"]case "logout":
           // Logout Code Goes Here
           break;
    case "login":
           //Login Code goes Here
           break;
    case "news":
           $path = "./contents/news/";
           break;
    default:
           $path = "./contents/main/";
    } ?>[/COLOR]
    
    
    [COLOR="Red"]<?php[/COLOR]
    $file = $_GET['id'];
    [COLOR="SeaGreen"]   if([COLOR="Blue"]file_exists([/COLOR][COLOR="Red"]$path.$file.".php"[/COLOR][COLOR="Blue"])) {[/COLOR] [COLOR="Red"]include $path.$file.".php";[/COLOR] }
       elseif[COLOR="#0000ff"](!$file) {[/COLOR] [COLOR="#ff0000"]include $path."main.php";[/COLOR]
       else [COLOR="#0000ff"]{ [/COLOR][COLOR="#ff0000"]include "./contents/main/err.php";[/COLOR] [COLOR="#0000ff"]}[/COLOR][/COLOR]
    [COLOR="#ff0000"]?>[/COLOR]
    

    The first part decides if the page should process logins, logouts or just show the news or main content sections. You can also keep your login and logout code in a seperate file, e.g. "login.inc" and include it with a PHP include statement at the start of the index.php file. As you can guess from my above code, all my page files are stored in a folder called Contents, and two subfolders called main and news store the main and news content pages.

    This way, the site is modular as all I need to do is put some HTML formatted text into a file called "file.php" in the ./contents/main dir and by calling ?act=main&id=file, the contents of that file are included in the output.


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    Thanks alot guys. Definately some interesting suggestions there.

    I'm looking into this MVC lark with interest. I've found Code Igniter. Is that a good one?


  • Registered Users Posts: 15,989 ✭✭✭✭blorg


    Using a single file as a "switch" is not necessarily such a bad idea. You still have multiple files, just the switch includes them as required, depending on the "action". This gives you a sort of "map" of your application in the switch file (generally, you would use several switches for different logical sections of the site, with a "master switch" at the root.) As all requests have to go through the switch file it is an easy task to look in there to find the code necessary to do anything.

    Otherwise, you just have links to random pages embedded all over the place and you end up with code spaghetti. Carzone, which was mentioned, uses a popular development methodology called "Fusebox" which originated with ColdFusion but which is also applied to PHP. As a methodology it is pretty similar to what you have been talking about.

    Note you still have the multiple files, all requests are just routed through the "switch". Putting all the actual code into a single file would indeed be an unholy mess.

    Reading up on MVC is also a good idea.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    blorg wrote:
    Using a single file as a "switch" is not necessarily such a bad idea.
    No, but typically it is. Of the points I raised earlier, what you’re suggesting addresses the second point, kind of addresses the first and third points and does not address the last. And these were simply off the top of my head.
    You still have multiple files, just the switch includes them as required, depending on the "action".
    Depends on the language. With PHP file includes will only occur if called, however with other languages/technologies, that use more traditional SSI methods (such as ASP) all file includes are called automatically before the script is even run. In effect they all become one big script.
    This gives you a sort of "map" of your application in the switch file (generally, you would use several switches for different logical sections of the site, with a "master switch" at the root.) As all requests have to go through the switch file it is an easy task to look in there to find the code necessary to do anything.
    TBH, it doesn’t matter how you lay out a Web site if you want your code to be laid out cleanly and logically.
    Otherwise, you just have links to random pages embedded all over the place and you end up with code spaghetti.
    The nature of Web navigation is that even if you do have only one file doing all the work, from the perspective of the user, they’re still going to have links on ‘different pages’ going all over the place.
    Reading up on MVC is also a good idea.
    MVC is how 99% of applications should be written, IMO. Of course, you don’t need the ‘switch’ file approach to do this.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    I actually don't use a third party platform.
    I merely use a design pattern.
    If you like, I suppose I could do up a tutorial and post it to the net somewhere.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    If you're using MySQL and that's it, you shouldn't really need PEAR, because it's main function is as a DB API.

    Similarly, if you have a good knowledge of xHTML already you shouldn't really need any external apps - it's easy enough and much more controllable to simply write it yourself.

    Create a couple of includeable pages, like errors.php, db.php and components.php for example, and let them contain relevant methods that you can then check for at runtime.

    eg. db.php contains methods like connectToDB() that connects to your regular database, errors.php contains methods like DBNotFound() which is called if connectToDB() does not give you a DB connection when it's called. And then components.php has methods like printHead(title) which prints a standard page header for all your pages but with a customisable page title.
    Then you can have similar methods like printNavBar() etc.

    Corinthian et al. - In all fairness, I know modern (read, Java/C#) design standards say all this is a big no-no, but I program Fortran, C and COBOL and guess how those languages work... Fact is, a well managed procedural program can work well and can be much easier for a learner to implement, particularly if scalability is not a requirement (fact is, when someone becomes good at a programming language, they usually rewrite their old code from scratch rather than try to update their previous hacks)


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    yeah I have afew include files. THere is another thread on here I started about search queries. It has a mock example of the type of code I've written.

    @akiri: Guys, any kind of tutorials or examples would be awesome. I'm running out of time to do this and I don't have the knowldege or experience to get it done....


  • Advertisement
  • Registered Users Posts: 4,188 ✭✭✭pH


    Just to add to your last comment, if this site is going live in any meaningful way then you have to be able to handle security issues.

    This in itself is a massive problem, even experienced php developers often leave XSS,SQL insertion/Error reporting security holes. You really need to understand these issues very well from day 1, as redoing all your code later will be a major pain in the ass.

    Have a read here for a simple overview:
    http://www.sitepoint.com/print/php-security-blunders

    If this is a real commercial site rather than a student project, I can't emphasise enough how important it is to understand these attacks and prevent them.


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    It is a real site, and thanks for the advice.


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    If you have lots of time then great, but if the customer needs a system soon and you're facing a learning curve that is extending the development time, it might be wise to split the job with someone who already has the experience. I've no time but others on boards might...


  • Closed Accounts Posts: 72 ✭✭TheFredJ


    fusebox is the correct answer to this. the current PHP version is quite solid.


  • Registered Users Posts: 7,314 ✭✭✭Nietzschean


    i've done a few sites using similar idea of this, works best i find with a mod-rewrite to bascially grab the other pages and mangle them into args onto your php page. Sites i've done basically use the args to build a path of other php files to include for menu's and content, works quite nicely. Alot of the content pages are just static html that gets included, means we've been able to do some nice privlidge seperation(i.e. people who update site only have access to the relevent content pages, and its all flat html, works quite well for them updating stuff, while not being able to break the entire site)


  • Closed Accounts Posts: 2,175 ✭✭✭chamlis


    I'm tipping away at this the whole time lads.
    All the advice and opinions are greatly appreciated.

    It is a bit of a noodle, isn't it? :D


Advertisement