Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    46
    Thanks
    7
    Thanked 0 Times in 0 Posts

    index.php?action=XXXXX ..

    i was wondering how to make a lot of my content to run under index.php instead of a bunch of files

    ex. - SITE.tld/index.php?action=contact
    vs. - SITE.tld/contact.php

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    although it might be tempting to use only index.php to show all content, it is wise to use other .php files for pages. especialy files that have special functions - eg. contact.php

    but, if you want it all to be index.php you can use $_GET[] predefined variable.
    what does get do?

    if your requested url was something like index.php?id=xxx then this PHP code in index.php file

    echo $_GET['id'];

    will echo 'xxx' .

    you can then use $_GET to include files on your page like this

    include ($_GET['id'].html);

    in your case it would include xxx.html file .

    be careful when using get when page first loads, beacouse then GET isn't set. you might use if control structure with isset to load get.

    it would be something like

    PHP Code:
    if (!isset($_GET['id'])) {
    //code to load default page (page when get is not set)
    }
    else {
    //code to use $_GET eg.
    include ($_GET['id']."html");

    remember what i wrote first, it's not smart to use only index. but it is all up to you and what you think is better. i used only index.php for all my site, and when loading forms that have special functions (eg e-mail), i had to write special pieces of code to load those functions.
    you can avoid that if you put all your functions in one file and include it on top of your index.php but then it is just unnecessary server load

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    although it might be tempting to use only index.php to show all content, it is wise to use other .php files for pages.
    I'm curious what your reasons are.

    I myself can't find any obvious reasons.

    It's actually a lot more flexible, and I would say that in most cases you should use a solution like that. You also end up repeating a lot less code (you generally shouldn't ever repeat blocks of code).

    i used only index.php for all my site, and when loading forms that have special functions (eg e-mail), i had to write special pieces of code to load those functions.
    you can avoid that if you put all your functions in one file and include it on top of your index.php but then it is just unnecessary server load
    You're not forced to use 1 include, you can use as many as you want.

  • #4
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    sure but if all functions are included in index file, and aren't used always, then why including them?

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The functions can be included in the individual pages. No one forces you to include them in index.php

  • #6
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by aedrin View Post
    I'm curious what your reasons are.

    I myself can't find any obvious reasons.

    It's actually a lot more flexible, and I would say that in most cases you should use a solution like that. You also end up repeating a lot less code (you generally shouldn't ever repeat blocks of code).
    Independent files (preferably with no extensions) are much more user friendly. I'd only use it where it can be beneficial to the user and not harmful, e.g. form information; on my contact page, I use a $_GET variable to see which page the user came from, and based on that set the selected OPTION in the SELECT element (drop-down list).

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Independent files (preferably with no extensions) are much more user friendly.
    Why? A user clicks on links, rarely does a user enter a link manually. And if they do, it's generally only your main URL/homepage.

    And if worried about the looks of the link, you can just set up Mod_Rewrite.

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    I have always found that it is easier to manage separate files (contact.php, index.php, forumlist.php, thread.php, etc.) than to destroy your PHP code with comments everywhere just to make sure that you understands the code. Not only that, but it defeats the purpose of having an include statement and causes longer page load times! Modularity is a big thing that has many advantages, though the biggest advantage is probably time.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #9
    New Coder
    Join Date
    Aug 2007
    Posts
    25
    Thanks
    1
    Thanked 4 Times in 4 Posts
    This is a good way to setup a site that's using one particular template. There's advantages and disadvantages, like every other method of formatting a site.

    One thing to keep in mind, though...

    This code:

    PHP Code:
    if (!isset($_GET['id'])) {
    //code to load default page (page when get is not set)
    }
    else {
    //code to use $_GET eg.
    include ($_GET['id']."html");

    Is just begging to get hacked.

    You should have some sort of check to ensure that you're only including valid files. For example, an array of acceptable file names, and if the requested file is not in that array, show a 404 page. When you're including whatever file was requested without checking, you've left a big XSS hole in your site.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •