SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 72

Hybrid View

  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 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?

  10. #10
    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.

  11. #11
    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

  12. #12
    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$

  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)
    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?

  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
    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.

  15. #15
    "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.

  16. #16
    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.

  17. #17
    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?

  18. #18
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would go with the method that HarryF suggested.

    -Helge

  19. #19
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can extend this so that you don't have to specify the path to the file in the url.
    PHP Code:
    $pages = array('AddClan'         => 'Clans/AddClan.php',
                   
    'EditClan'        => 'Clans/EditClan.php',
                   
    'DeleteClan'      => 'Clans/DeleteClan.php',
                   
    'AddLanCenter'    => 'LanCenters/AddLanCenter.php',
                   
    'EditLanCenter'   => 'LanCenters/EditLanCenter.php',
                   
    'DeleteLanCenter' => 'LanCenters/DeleteLanCenter.php'
                  
    );

    if(
    array_key_exists($_GET['page'], $pages)) {
        include 
    $pages[$_GET['page']];
    } else {
        include 
    'default.php';

    -Helge

  20. #20
    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 happy with this code:
    Code:
    <?php 
    $file = isset($_GET['page']) ? $_GET['page'] : 'default'; 
    include($file . '.php'); 
    ?> 
    It just makes more sense for me to use it, and plus, it's the smallest. I don't have too many server security issues. I don't really care about it.

    How do I get links to work through that code? You know, as in index.php?page=whatever

    Can't seem to get that to work...
    Content Writing Service - Get custom SEO articles for 10$

  21. #21
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using this:
    Code:
    <?php 
    // index.php 
    
    // Some function / class that gets scalar variables 
    $scalarVars=getScalars(); 
    
    switch ( $scalarVars[0] ) { 
    	case 'news': 
    		include ('news.php'); 
    	break; 
    	case 'downloads': 
    		include ('downloads.php'); 
    	break; 
    	default: 
    		include ('default.php'); 
    	break; 
    } 
    ?>


    I get this error message:
    Fatal error: Call to undefined function: getscalars() in c:\apache\www\test\index.php on line 38

    What is causing it this time?
    Content Writing Service - Get custom SEO articles for 10$

  22. #22
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getScalars() is not a PHP function.
    Try
    PHP Code:
    switch($_GET['page']) { 
    -Helge

  23. #23
    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 Helge
    getScalars() is not a PHP function.
    Try
    PHP Code:
    switch($_GET['page']) { 
    -Helge
    Thanks for that, but I can't get the page to execute the switch! Here is my code:
    Code:
    <?php 
    switch ($_GET['page']) {  
     case 'add': 
      include ('add.php'); 
     break; 
     case 'downloads': 
      include ('downloads.php'); 
     break; 
     default: 
      include ('default.php'); 
     break; 
    } 
    ?>
    And one of my links is:
    <a href="index.php?page=add">Add</a>

    When you click on it, all it does is add ?page=add into my url box! Yet it does not get add.php. What am I doing wrong?
    Content Writing Service - Get custom SEO articles for 10$

  24. #24
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the behavior in the url is correct.

    Remeber that this will not redirect you to a new page (and changing the url). It will simply include the page (add.php) into index.php.

    Do you have a file called add.php which echo out some text?

    -Helge

  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)
    Quote Originally Posted by Helge
    I think the behavior in the url is correct.

    Remeber that this will not redirect you to a new page (and changing the url). It will simply include the page (add.php) into index.php.

    Do you have a file called add.php which echo out some text?

    -Helge
    Wait, it has to echo out some text? I just have it with the html coding, just like default.php.
    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
  •