SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    URL Manipulation...

    PHP 5.2.5

    I want to create a function to manipulate the current URL.

    How is the best way to retrieve the separate parts of the URL to a variable.

    1 - "HTTP" or "HTTPS" etc
    2 - www.domain.com
    3 - directory/2nddirectory
    4 - index.php
    5 - id=10&mode=qv

    I want to be able to insert and/or remove 'variables' to facilitate passing more information thru _GET

    Thanks

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, I didn't make myself clear. How do I <b>retrieve</b> the seperate parts of the url, parse_url() is just a function, how do I get the URL itself into a variable so I can start manipulating it?

    Thanks

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at $_SERVER superglobal.

  5. #5
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    What a great function! I never knew that existed but that certainly beats essentially writing that functionality myself every other project.
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  6. #6
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've already been there, but there's multiples ways of retrieving some parts of the URL and I can't see a way of retrieving whether the URL is HTTP or HTTPS etc..

    At the moment I'm looking at using

    Code:
    $_SERVER["HTTP_HOST"]  www.domain.com
    $_SERVER['PHP_SELF'] /dir1/dir2/file.php
    and

    Code:
    foreach ( $_GET as $key => $value )
    to iterate through each _GET

    So A - How do I retrieve the HTTP or HTTPS?
    and B - Is the above, the best way of retrieving those parts?

    Thanks

  7. #7
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at the following keys in that superglobal:

    • HTTPS
    • HTTP_HOST
    • REQUEST_URI
    • QUERY_STRING


    Refer to the manual page I mentioned above for explanations of what they are.

  8. #8
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, thanks for everyone's input, it took a while to get exactly what i needed... If you read my original request:

    How is the best way to retrieve the separate parts of the URL to a variable.

    1 - "HTTP" or "HTTPS" etc
    2 - www.domain.com
    3 - directory/2nddirectory
    4 - index.php
    5 - id=10&mode=qv
    Everyone except the last guy dropped titbits, but didn't give me exactly what I needed, finally I finished up with the following:
    (credit for the substr() bit goes to a random tutorial I found thru google)

    REQUEST_URI includes the QUERY_STRING so there's no point using both of them... As I'll want to play with them later, I chose the below code to stick it all together:

    Code:
    $varQuery = "";
    if (strlen($_SERVER['QUERY_STRING']) != 0) {$varQuery = "?";}
    $varURL = 
    substr($_SERVER['SERVER_PROTOCOL'],0,strpos($_SERVER['SERVER_PROTOCOL'],'/')) . "://" .  
    $_SERVER["HTTP_HOST"] . 
    $_SERVER['SCRIPT_NAME'] . $varQuery .
    $_SERVER['QUERY_STRING'] ;

    Thanks for everyones help.

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you shouldn't be figuring out whether the URL is http:// or https:// what's the domain etc in your PHP code. That's like scratching your head with your leg.

    Your application should now what's going on without needing to use $_SERVER.

  10. #10
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A - I asked a very specific question, it wasn't answered...

    B - The section I need it for is for a non-specific part of each page that is exactly the same before any content is loaded. It is required...

    Thanks

  11. #11
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummmmm, that seems rather complicated for such a simple task, and I don't have a clue what CURL is LOL

  12. #12
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SystemOverload View Post
    Ummmmm, that seems rather complicated for such a simple task, and I don't have a clue what CURL is LOL
    http://www.php.net/curl LOL
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  13. #13
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have access to the server accept the webserver root, so can't intall anything unless it's pure php...

    But it's all sorted, no worries..

    thnx


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
  •