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 10 of 10
  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Variable from URL

    Ok lets say I have a script under post/index.php. And I need a variable from the URL but I really would like to find away to not use ?id=xxx. What I'd like to do is something like www.mysite.com/posts/id.

    Is there anyway to retrieve the var from this?

    Thanks a lot!

    FuZion

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by FuZion View Post
    Ok lets say I have a script under post/index.php. And I need a variable from the URL but I really would like to find away to not use ?id=xxx. What I'd like to do is something like www.mysite.com/posts/id.

    Is there anyway to retrieve the var from this?

    Thanks a lot!

    FuZion

    Well modrewrite is how it gets done.
    ---
    Here's an easy way to do it for beginners and learn the ropes of mod rewrite.

    in your .htaccess file put this:
    Code:
    RewriteEngine On
    RewriteRule ^(PHPFILE1)(/.*)?$ $1.php
    where PHPFILE1 is the name of the php file that you'd like to do this with (you can add as many as you like.
    example:
    Code:
    RewriteEngine On
    RewriteRule ^(PHPFILE1|PHPFILE2|PHPFILE3|PHPFILE4)(/.*)?$ $1.php
    -------------

    once that's done..
    in the PHPFILE1.php place this bit of code into it:
    PHP Code:
    <?php
    $urlArray 
    explode("/",$_SERVER["REQUEST_URI"]);

    $page_url $urlArray[count($urlArray)-1];
    $page_id $urlArray[count($urlArray)-2];

    echo <<<end
    Page URL: $page_url
    <br />
    Page id: $page_id
    end;
    ?>
    Finally, visit this url in your browser:

    Code:
    http://www.YOURSITE.com/FILE1PHP/1234/fake-file-name.html
    and the script will output:
    Code:
    Page URL: fake-file-name.html
    Page id: 1234
    Again, this isn't the best way to do it; but it's the easiest for a beginner with mod-rewrite to understand and use.

    Best,
    Last edited by Coastal Web; 06-09-2007 at 07:03 AM.

  • #4
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    If you don't mind me asking, why are you hiding the id? not like it's a password. And if your logged in, (just stereotypically) why don't you have a field that has to match current cookie setting. Just to save the effort.

  • #5
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GSimpson View Post
    If you don't mind me asking, why are you hiding the id? not like it's a password. And if your logged in, (just stereotypically) why don't you have a field that has to match current cookie setting. Just to save the effort.
    Well, I have a site that will receive a lot of "posts" if you will, when the user posts something they will be given a unique id that they will have to remember. And in order for them to visit it I'd like for them to be able to go to www.mysite.com/posts/ID or something like that.

    Thanks a bunch for your help guys!

  • #6
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    why not just do it like this forum, I mean it receives at least 100 posts a day and has been built to go forever (speaking theoretically). and remembering your id is ok, but if it just gonna be entered in the url, why not just do it after ?id=

  • #7
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GSimpson View Post
    why not just do it like this forum, I mean it receives at least 100 posts a day and has been built to go forever (speaking theoretically). and remembering your id is ok, but if it just gonna be entered in the url, why not just do it after ?id=
    Not everyones a coder.

    example.com?id=x looks very foreign compared to example.com/id/

    example.com/id/ is considered a search engine friendly (SEF) url - though it would be more SEF if you placed the 'title' instead of the 'id' in the URL.

    This becomes even more apparent the more url params you have:

    example.com/php/what-is-php/

    compared to:

    example.com?f=1&p=2

    who can remember that? who wants to add that to their blog when it doesn't have any keywords reflecting their blog post.. etc etc...

    automated services can easily classify/tag/aggregate/index/bla..bla your content into their own system if you give them hints..

    the list goes on...
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    FYI, there is another option and that is to use the $_SERVER['PATH_INFO']

    e.g. with the url domain.com/index.php/article/nice-day
    echo $_SERVER['PATH_INFO']; outputs '/article/nice_day'

    you can then use explode('/',$_SERVER['PATH_INFO']) etc to map those into variables.

    I use this method myself though it is not without gotchya's the main one being that relative paths will now be wrong , so to include a file in the same directory as index.php you will need to prepend ../../ to get at that file.
    Thats not an issue for me as all my pages use absolute paths but it is worth noting.

    The other issue is the predetermined variable order but thats the same with mod-rewrite as well.

    If your server supports force-type you can also lose the '.php' part of the usl
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ok so not every ones a coder, why not just have a page ie somedomain.com/index.php and have a form ENTER YOUR ID: than have it goto http://somedomain.com/posts/?id=$_POST['input'], for example.

  • #10
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GSimpson View Post
    Ok so not every ones a coder, why not just have a page ie somedomain.com/index.php and have a form ENTER YOUR ID: than have it goto http://somedomain.com/posts/?id=$_POST['input'], for example.
    thats called google. Works best with names instead of ids...
    getting off topic here...
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog


  •  

    Posting Permissions

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