SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 72
  1. #1
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Url's with php. HELP!

    How would I get php to recognize a link such as this:

    http://localhost/index.php?page=section

    I have been wondering, and its getting annoying to always retype urls.
    Then again, I might be wrong.

    Which way is it better to make links?
    /section.html
    or
    index.php?page=section

    Kind of confused. Could somebody clear this up for me please?
    Content Writing Service - Get custom SEO articles for 10$

  2. #2
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Giving each page it's own name / link like section.html or section.php makes more sense to me than calling one file for the whole site with variables.
    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content

  3. #3
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The advantage of using all under index.php is it's easier to define global variables, if you know what I mean.

    Such as a connection at the top of the file, rather than at the top of each file.

    Just a tad bit easier.

  4. #4
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    The advantage of using all under index.php is it's easier to define global variables, if you know what I mean.

    Such as a connection at the top of the file, rather than at the top of each file.

    Just a tad bit easier.
    How would I go about doing that? how would I call all files from one file?
    Content Writing Service - Get custom SEO articles for 10$

  5. #5
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    <?php
    $file 
    = ( isset($_GET['page']) ? $_GET['page'] : 'index' );
    include(
    $file '.php');
    ?>
    Now if you went to http://www.yoursite.com/file.php?page=test that would include http://www.yoursite.com/test.php.

  6. #6
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DJ P@CkMaN
    PHP Code:
    <?php
    $file 
    = ( isset($_GET['page']) ? $_GET['page'] : 'index' );
    include(
    $file '.php');
    ?>
    Now if you went to http://www.yoursite.com/file.php?page=test that would include http://www.yoursite.com/test.php.
    Could you explain that a bit more thoroughly. That got me confused. I'm going to use index.php as the main file, so could you clear that up abit more please.
    Content Writing Service - Get custom SEO articles for 10$

  7. #7
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try thinking of it like this:
    PHP Code:
    if ($_GET['page']=='action1')
    {
       
    load_action1_page();
    }
    if (
    $_GET['page']=='action2')
    {
       
    load_action2_page();
    }
    // and so on... 
    (a switch statement would probably be a little better looking in this case, but basically does the same thing)

    After you master altering the page with "get" requests, you can go back and make your urls more friendly with mod-rewrite.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  8. #8
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    Try thinking of it like this:
    PHP Code:
    if ($_GET['page']=='action1')
    {
    load_action1_page();
    }
    if (
    $_GET['page']=='action2')
    {
    load_action2_page();
    }
    // and so on... 
    (a switch statement would probably be a little better looking in this case, but basically does the same thing)

    After you master altering the page with "get" requests, you can go back and make your urls more friendly with mod-rewrite.

    HEY wait! I'm still confused! Do you know of any tutorials or anything about this? I have no idea how to do this? Where do I put the code and how do I work it. I am serious! I'm a newbie to this stuff. Why else would I be asking these kind of question?
    Content Writing Service - Get custom SEO articles for 10$

  9. #9
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    Try thinking of it like this:
    PHP Code:
    if ($_GET['page']=='action1')
    {
    load_action1_page();
    }
    if (
    $_GET['page']=='action2')
    {
    load_action2_page();
    }
    // and so on... 
    (a switch statement would probably be a little better looking in this case, but basically does the same thing)

    I thought about doing something like this, but won't all of your pages have to be gotten to by the GET method? What if you have a form that POSTs to another page or itself? Won't that page, um, not work? And I know that you can just change the action to GET, but what about a login form, you don't want to send the username/password in the URL, right?

    I've seen lots of sites that use this method, of one page that includes a page in the URL, but how do they handle forms being submitted via POST.

  10. #10
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <form action="index.php?page=test" method="post">
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  11. #11
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    <form action="index.php?page=test" method="post">
    That's it? You shouldn't run into any other issues? Is that method (using GET and POST at the same time) a standard? Will it work in every browser? Are there any browsers that won't send the url data?


    What about subfolders? For instance, on one of the sites I do, there are clans and lan centers. I have everything seperated so everything to do with clans (add, edit, delete, view, etc.) is in a subfolder called /clans/ and the same for lancenters (/lancenters/). So would I use the index.php file off of the root with ?page=/clans/editclan.php in the url? Or should I use an index.php file INSIDE the clans folder with ?page=editclan.php in the url? And if I have multiple directories for different subjects (i have 6 or 7), would I need an index.php for each directory that only handles that directory? And if so, won't all of the index.php files be (almost, if not) identical?

    Thanks for any light you peeps can shed on this as it has confused me for awhile now.

  12. #12
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sojomy
    That's it? You shouldn't run into any other issues? Is that method (using GET and POST at the same time) a standard? Will it work in every browser? Are there any browsers that won't send the url data?
    Yes, yes, yes and no.
    Well, I think. Now that you mention it, I don't think I've ever seen a browser breakdown for that behavior, which may mean they all work just fine or that may mean I just have not seen it. Hmmm. I really think it works on all browsers, but I am not positive.

    If you were using mod_rewrite to generate "get" requests from a more friendly url, then it would definately work, every single time, absolutely!

    Quote Originally Posted by sojomy
    What about subfolders? ...
    So would I use the index.php file off of the root with ?page=/clans/editclan.php in the url?
    You could do that, should work fine. Sounds like the more simple of the choices you've presented.

    However, if you are channelling the user through a single main.php page, why not hide the fact that the files are in different directories from them?

    I'm a little crossed up (thinking of a few examples at once) so I'll leave it at that. Anything still need clarification?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  13. #13
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dragonfly_7456
    HEY wait! I'm still confused! Do you know of any tutorials or anything about this? I have no idea how to do this? Where do I put the code and how do I work it. I am serious! I'm a newbie to this stuff. Why else would I be asking these kind of question?
    Alright, lets see. Does this help?:
    PHP Code:
    <?php 
    if ($_GET['page']=='razzleberries')
    {
       echo(
    'This is the razzleberries page!');
       
    // put any code you want for the razzleberries page here
    }
    if (
    $_GET['page']=='banannas')
    {
       echo(
    'This is the banannas page!');
       
    // put any code you want for the bananna page here
    }
    // and so on...
    ?>
    That way when you enter the url:
    http://site.com/your_page.php?page=razzleberries
    You will execute the first if section and not the second.

    http://site.com/your_page.php?page=banannas
    You will execute the second if section and not the first.

    http://site.com/your_page.php
    Will execute neither the banannas or razzleberries section.

    Does that help?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  14. #14
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    However, if you are channelling the user through a single main.php page, why not hide the fact that the files are in different directories from them?
    So, using your example, I could do something like this for index.php

    PHP Code:
    <?
    <HTML><BODY>
    Blah Blah at the top
     
    // include the main content below
    switch ($_GET['Page']) {
      case 
    'AddClan' : include('Clans/AddClan.php'); break;
      case 
    'EditClan' : include('Clans/EditClan.php'); break;
      case 
    'DeleteClan' : include('Clans/DeleteClan.php'); break;
      case 
    'AddLanCenter' : include('LanCenters/AddLanCenter.php'); break;
      case 
    'EditLanCenter' : include('LanCenters/EditLanCenter.php'); break;
      case 
    'DeleteLanCenter' : include('LanCenters/DeleteLanCenter.php'); break;
    }
     
    Blah Blah at the bottom
    </BODY></HTML>


    ?>
    Like that? Then every page would have the "blah blah" at the top and the bottom?

    Would you run into any problems with the Referrer variable? Would the referrer be something like
    http://www.domain.com/index.php?Page=EditClan

    I can't think of anything else, I just feel like there's something I'm forgetting about that might cause a problem. I guess not though.

  15. #15
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    Alright, lets see. Does this help?:
    PHP Code:
    <?php 
    if ($_GET['page']=='razzleberries')
    {
    echo(
    'This is the razzleberries page!');
    // put any code you want for the razzleberries page here
    }
    if (
    $_GET['page']=='banannas')
    {
    echo(
    'This is the banannas page!');
    // put any code you want for the bananna page here
    }
    // and so on...
    ?>
    thant kind of helps, thanks. So, does that mean that for every link you would have to add another if conditional? How would you do it so that the browser (or is it apache) would recognize that when a link says index.php?page=berry that the real page is berry.php in the main directory all in one piece of code, or is it called a "rule"?

    And then, how would you link to pages in subdirectories?

    You know, i'm finding this method a pretty bad one. What are the pluses of this kind of thing? I mean, by the looks of it, you have to add alot more code, then just to let apache work things its way.
    At the moment, here is the type of thing apache is doing on my server:
    /page will get me /page.php or /page.htm
    and it just seems that this method is a lot better, because you have less coding to do, less coding for the server and browser to process, it is quicker and best of all, it doesn't reveal the technology that you are using for the site. Correct me if I am wrong...

    So as i understood this, I would have one basic template for the entire site, and in the part where the content changes, I would include that php code. Correct? So that would mean, all your other pages would then take up less space on the server and would be quicker loading. Is that the advantage? Does it decrease loading time of each page? But then again, couldn't you just get the same result by using a ssi?
    Last edited by dragonfly_7456; Mar 26, 2003 at 11:45.
    Content Writing Service - Get custom SEO articles for 10$

  16. #16
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't have a reason to canopy everything under one php file, it is probably not a good idea to do so.

    It does carry some potential advantages though. It naturally centralizes the application which can lead to fewer files and (more importantly) less redundancy.

    The examples given are simple so they do not provide much advantage over creating multiple files. But imagine you have 100 php files. Is there overlap between them? If so you could have saved space and reduced the risk of editting one and forgetting another by centralizing them.

    Overall, it is mostly a choice to use one file as an entry point into their application rather than (say) a dozen.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  17. #17
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    If you don't have a reason to canopy everything under one php file, it is probably not a good idea to do so.

    It does carry some potential advantages though. It naturally centralizes the application which can lead to fewer files and (more importantly) less redundancy.

    Overall, it is mostly a choice to use one file as an entry point into their application rather than (say) a dozen.
    So is that the only advantage? You only have one file to edit, not alot? Isn't that the only advantage of frames?
    Are there any other benefits to using one page (that includes the rest when called)? Are there any other drawbacks or reasons why you shouldn't use it?

  18. #18
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sojomy
    So is that the only advantage? You only have one file to edit, not alot? Isn't that the only advantage of frames?
    Heh, that's a big advantage if your application gets large.

    When you think about grouping into one file, think about grouping classes and/or functions and reacting to user input.

    With one entry point you can easily create a security check right up front and be certain it will be effective across the entire site. With multiple points of entry, you have to make sure each of a dozen security checks is up to date.

    You know, this isn't something I discuss much... perhaps someone with more experience on this topic will drop by to shed some more light on the topic.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  19. #19
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    Heh, that's a big advantage if your application gets large.

    With one entry point you can easily create a security check right up front and be certain it will be effective across the entire site. With multiple points of entry, you have to make sure each of a dozen security checks is up to date.
    So if I was going to use sessions on my site, I would have to put the session_start() at the top of the index.php file, right? Because out put would already have been sent? Is there a problem with using sessions on every page? Including pages that don't use the session? Is there a performance drawback?

  20. #20
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, there could be a performance drawback to using sessions on pages that don't need them. I say "could" because it is entirely possible that you could get sessions handled so rapidly that your users would never experience delay. However, you don't have to load sessions on every page just to use them on some. Your main limitations would come from your choices in how the application is designed.

    Here's something like what you might do:
    PHP Code:
    <?php
    // before any header data is sent
    require_once('classes.php');

    $page = new Page();
    // event data exmined, correct page discovered

    $page->displayPage();
    // this throws everything into motion
    // if a user needs to be authenticated, 
    // it is triggered (directly of indirectly)
    // through this object

    ?>
    That's just one way a page might be composed. Really simple, huh? That's the idea. Many simple, clean steps leading to the finished work.

    I wish I had more experience, I could probably sell this a little better.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  21. #21
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're selling it quite well...or I wouldn't keep asking about it
    Now in the example below, how would I utilize sessions only on some pages?


    PHP Code:
    <?
    <HTML><BODY>
    Blah Blah at the top
     
    // include the main content below
    switch ($_GET['Page']) {
    case 
    'AddClan' : include('Clans/AddClan.php'); break;
    case 
    'EditClan' : include('Clans/EditClan.php'); break;
    case 
    'DeleteClan' : include('Clans/DeleteClan.php'); break;
    case 
    'AddLanCenter' : include('LanCenters/AddLanCenter.php'); break;
    case 
    'EditLanCenter' : include('LanCenters/EditLanCenter.php'); break;
    case 
    'DeleteLanCenter' : include('LanCenters/DeleteLanCenter.php'); break;
    }
     
    Blah Blah at the bottom
    </BODY></HTML>
     
     
    ?>
    I can't start the session in the individual pages, right? Because the '<HTML><BODY>' has already been sent, right? Is there another way without using classes? I've never used classes and don't anticipate it's a "5 minute quick read" to properly learn them

  22. #22
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, if you are thinking that you can't use sessions with cookies after sending html, you are correct.
    So that would be a change in the code you've posted right there.

    The general model is:
    PHP Code:
    // code
    $data do_stuff();
    $html_text do_more_stuff($data$_GET['page']);
    // presentation
    echo($html_text); 
    It can be a lot more elaborate than that, but you can get the idea. Data is gathered, then displayed. Any number of considerations (like cookies) may be figured out along the way, as long as "along the way" is before presentation begins.

    Will existing php files work well simply included? Depends upon the script, and how they handle presentation, but probably not.

    If you were to centralize your scripts, you would probably want to gather components of your scripts and group them. Do add, edit and delete clan really need separate files? They probably have quite a bit in common and can share parts of each other. Anyway, you can probably see where I'm heading: if you wanted to gain any benefit from creating a single access point, you'd probably need to rework the scripts to conform to such an approach.

    Getting to your situation, if AddClan.php and such are currently standalone, wouldn't they have <html> and </html> tags of their own? Seems to me like you would either need to get rid of those tags at the top and bottom or edit the scripts to work with a new point of entry.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  23. #23
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So it's like a home made output buffering of sorts? You put all of the output in variables, and once you're sure you don't have to send any more headers, you send the output?

    And yes, I would have to edit all of the scripts (get rid of the HTML and BODY tags and such). I really don't know if the benefits outweigh the problems (or ways around the problems)

    What about taking everything above and below the big switch statement (the one that figures what page to include), and putting that in each file, and just keeping the files seperated? I know that you would have alot of redundant code (the layout part of the code), but is that bad? Do the benefits of using a central file outweigh the ease of individual files?

    Again, thanks for your opinions. You're knowledge is a little more advanced than mine, so I'll accept any input you give. I might not take it, but I'll consider it heavily

  24. #24
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sojomy
    Again, thanks for your opinions. You're knowledge is a little more advanced than mine, so I'll accept any input you give. I might not take it, but I'll consider it heavily
    Funny you should say that because I'm kind of quoting knowlege well more advanced than mine.

    Look for posts by voostind and company in the advanced PHP forum if you want the straight dope. I know I've seen voostind discuss his method of application organization before but I can't find the thread.

    Quote Originally Posted by sojomy
    What about taking everything above and below the big switch statement (the one that figures what page to include), and putting that in each file, and just keeping the files seperated? I know that you would have alot of redundant code (the layout part of the code), but is that bad? Do the benefits of using a central file outweigh the ease of individual files?
    I would say that if you are using the files like separate files, then you probably do better to keep them as separate files, although it is certainly possible to use a php file as a switchboard for those files.
    Last edited by samsm; Mar 26, 2003 at 23:25. Reason: enhanced reality
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  25. #25
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I'm kind of catching on now!

    Let me get this strait. By using the method that we have been discussing (using one main file) we would end up with this for our web:

    Index.php file:
    Code:
    <?
    <html>
    <head></head>
    <body>
     
    BLABLALBLALBALBLALBLALBLABLA// include the main content below
    switch ($_GET['Page']) {
    case 'AddClan' : include('Clans/AddClan.php'); break;
    case 'EditClan' : include('Clans/EditClan.php'); break;
    case 'DeleteClan' : include('Clans/DeleteClan.php'); break;
    case 'AddLanCenter' : include('LanCenters/AddLanCenter.php'); break;
    case 'EditLanCenter' : include('LanCenters/EditLanCenter.php'); break;
    case 'DeleteLanCenter' : include('LanCenters/DeleteLanCenter.php'); break;
    }
     
    Blah Blah at the bottom
    </body>
    </html>
    ?> 


    then, say the AddClan.php, would only contain the text that should be in place of the big code, correct? So, that would mean, the next page that you load would then just have to load the switched text, and not the whole layout. Right? How does this affect the outcoming loading time? As I understood, it should decrease loading time by a long shot.

    First, the loading pages all are smaller in size by a far shot.
    Second, you don't have to reload the layout each time.

    Is the second point correct? Does the browser store the loaded layout, and then only have to load in the switch, or does it reload everything?

    Third, you get a larger index file that loads longer.

    This could be a big concern couldn't it? I mean, you are saying that this method is great for sites with over 100 pages. But then, all 100 pages would have to be included in the main index file. If so, then the php loading, plus the size of the code could increase the loading time to such a point, where no one will wait. Correct? Of course, this could be a good method for popular sites, where the users know what they can expect, but wouldn't this method lose you customers?

    Please do correct me if I am wrong.

    One more thing, in the part where the switch is, how would you get a default page to load? You know, as in, where the switch will take place, is also where your index page content is. How do you get it to show up before the switch?
    Last edited by dragonfly_7456; Mar 27, 2003 at 02:43.
    Content Writing Service - Get custom SEO articles for 10$


Bookmarks

Posting Permissions

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