SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extract variables from a URL with PHP

    Hi,
    I'm just wondering if it is possible to pull the following values (id and num) from this URL?

    website_url/website_name/generatebuynowbtn.php#id=65&num=1

    This url is beng dynamically generated and I cannot convert the # into a ? in order to use the $GET_ method

    Any help would be greatly appreciated

  2. #2
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by awx2d View Post
    Hi,
    I'm just wondering if it is possible to pull the following values (id and num) from this URL?

    website_url/website_name/generatebuynowbtn.php#id=65&num=1

    This url is beng dynamically generated and I cannot convert the # into a ? in order to use the $GET_ method

    Any help would be greatly appreciated
    Hmm...

    PHP Code:
     
    //First we set the url variable here
    $url 'website_url/website_name/generatebuynowbtn.php#id=65&num=1';
     
    //We search for a match to grab the id value and the num
    //value in the url variable above
    preg_match('#\#id=([0-9]+)\&num=([0-9]+)#',$url,$variables);
     
    //We assign the values to their own variables and
    //make sure they are a number with int()
    $url_id intval($variables[1]);
    $url_num intval($variables[2]); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow that was quick, can the url be dynamically grabbed?

  4. #4
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/parse_str

    PHP Code:
    $vars = array();

    parse_str(substr($strstrpos($str'#') + 1), $vars); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Script works great, any ideas as to how to pull in the full url dynamically to populate $url?

  6. #6
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you need to do anything with these variables, or are you just transfering the querystring to another URL ?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I need these values to query my database

  8. #8
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by awx2d View Post
    Script works great, any ideas as to how to pull in the full url dynamically to populate $url?
    Is the url locally on your server or on another server?

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Currently it is locally on my server i.e. localhost though when/if i get it working it will be hosted somewhere.

    I've tried REQUEST_URI but it only gets as far as /websitename/generatebuynowbtn.php without the variables I need. Javascript will pull it with location.href but I dont know if I can add this to a php variable ...

    Bit lost really

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, I'm quite dissapointed that no one has given the actual answer here.

    The # is a command to the browser, it's not sent to the server.

    For example:
    This URL: http://www.sitepoint.com/forums/showthread.php?t=556061
    The Top: http://www.sitepoint.com/forums/show...p?t=556061#top
    The Bottom: http://www.sitepoint.com/forums/show...=556061#bottom
    Some post: http://www.sitepoint.com/forums/show...essage_3867033
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So there is no way to do it? Can Javascript be used to pull the url into php?

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, replace the # with ?

    Alternatively, dont use the # in the first place.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #13
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using slideshowpro which is generating the #, this cannot be changed, if it was ? I would not have a problem getting the values in the first instance ..

    Any other suggestions?

    If I used js to create a link with ? instead of # and pass this to the next page, i assume this would work but how could i make this seemless, is this the only way I could do it?

  14. #14
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could have it copy the hash as querystring variables, so both things work.

    it's not sent to the server
    You know, I never realized that.

  15. #15
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Ok, I'm quite dissapointed that no one has given the actual answer here.

    The # is a command to the browser, it's not sent to the server.

    For example:
    This URL: http://www.sitepoint.com/forums/showthread.php?t=556061
    The Top: http://www.sitepoint.com/forums/show...p?t=556061#top
    The Bottom: http://www.sitepoint.com/forums/show...=556061#bottom
    Some post: http://www.sitepoint.com/forums/show...essage_3867033
    Thats because the # seemed to be required, otherwise ? would be a very simple solution that he would of tried already and from the ops recent post that is the case.

  16. #16
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by awx2d View Post
    I am using slideshowpro which is generating the #, this cannot be changed, if it was ? I would not have a problem getting the values in the first instance ..

    Any other suggestions?

    If I used js to create a link with ? instead of # and pass this to the next page, i assume this would work but how could i make this seemless, is this the only way I could do it?
    Dont know if it would work but have you looked into mod rewrite?

  17. #17
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joebert View Post
    You could have it copy the hash as querystring variables, so both things work.



    You know, I never realized that.
    Yes, one use for # is bookmarks. When a bookmark is clicked it doesnt refresh the page.

  18. #18
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Dont know if it would work but have you looked into mod rewrite?
    It won't.

    As you said, it's not refreshed. A request isn't sent to the server.

    Use JS to redirect after replaceing the # with ?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  19. #19
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    It won't.

    As you said, it's not refreshed. A request isn't sent to the server.

    Use JS to redirect after replaceing the # with ?
    Yes, was messing around with some js code I put together:

    Code:
    <script type="text/javascript">
     curr_hash = location.hash;
     new_hash = curr_hash.replace("#", "?");
          window.location = 'http://yoursite/somescript.php' + new_hash;
    </script>
    Obviously you need to grab the rest of the url to pass along with it. But as is if you had this url:

    http://www.somesite.com/test.php#test=1

    Then ran the code above to forward to somescript it would then go to:

    http://www.somesite.com/somescript.php?test=1

    Edit:
    Just tested it with putting #id=65&num=1 at the end of the url and it redirected as ?id=65&num=1

    So I think that is the output you are looking for?

  20. #20
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats great thanks for the help lads, I'll have a play around with this tomorrow and see if I can get it all working ..

    Thanks again
    Adam

  21. #21
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be a pain but I am trying to customise the js above to create a link e.g. if the url is as above website_url/generatebuynowbtn.php#id=65&num=1 how can I create a link on the same page such as this: <a href="website_url/calcImage.php?id=65&num=1">Buy Now</a> thus giving me the approriate variables to query my database and give me the product_id

    Thanks again for all the help here lads ..

  22. #22
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorted!

    <script type="text/javascript">
    curr_hash = location.hash;
    new_hash = curr_hash.replace("#", "?");
    document.write('<a href="http://localhost:8888/site_name/calcImage.php' + new_hash +'">');
    document.write('Buy Now');
    document.write('</a>');
    </script>
    Last edited by awx2d; Jun 27, 2008 at 09:13.

  23. #23
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just being picky here.

    Why are you using capital HTML?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  24. #24
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope

  25. #25
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I was asking why you use it, not if.

    document.write('<A HREF="http://localhost:8888/bodynudes/calcImage.php' + new_hash +'">');
    document.write('Buy Now');
    document.write('</A>');
    Just seems very old-fasioned really.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •