SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_SERVER["PHP_SELF"] with $_GET??

    stymiee gave me thhis code:

    $_SERVER["PHP_SELF"]


    so that i can return a user to their previous page after they login. it works except when the pages is made with get variables

    say the pages is /product.php?pid=145 it only returns product.php and returns them to an empty page.....

    i could just say
    PHP Code:
    <?=$_SERVER["PHP_SELF"].'?pid='.$_GET['pid']?>
    but theres all different ones like category.php?cid=32

    so is there a way i can just grab all of it? like the $_SERVER["PHP_SELF"] but grab the varisables too?

    thanks!
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  2. #2
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can stick -

    PHP Code:

    $_SERVER
    ['PHP_SELF'] . $_SERVER['QUERY_STRING']; 
    those two together!

    That should work!

  3. #3
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i had to add a ? in the middle but that workked!!

    PHP Code:
    $cur_page $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; 
    thanks!
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  4. #4
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DANG IT!!!.....


    that only counts the first one, if theres 2 like ?cid=34&pid=243 the it only takes the first one
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  5. #5
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tut... I always forget it doesnt include the question mark.

    No problem!

  6. #6
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there a way to count hoe many there are and add them all?
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  7. #7
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if theres more will they be an array
    PHP Code:
    $_SERVER['QUERY_STRING'][0].'&'.$_SERVER['QUERY_STRING'][1
    possibly? ...im sorry im still pretty new to php
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  8. #8
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by halfasleeps
    DANG IT!!!.....


    that only counts the first one, if theres 2 like ?cid=34&pid=243 the it only takes the first one
    I had a quick look around and I can not only find no example of it only showing the first parameter but found several examples of it showing more than one????

  9. #9
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by halfasleeps
    if theres more will they be an array
    PHP Code:
    $_SERVER['QUERY_STRING'][0].'&'.$_SERVER['QUERY_STRING'][1
    possibly? ...im sorry im still pretty new to php
    Na, it wont be an array.... here have a look at this, shows examples of more than one param.

    http://www.keithjbrown.co.uk/vworks/php/php_p2.php

  10. #10
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by halfasleeps
    is there a way to count hoe many there are and add them all?
    You could foreach through the $_GET array and build up your own version of the $_SERVER['QUERY_STRING']

  11. #11
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help, i realised that it is catching both as i look into the broweser when im typing in it says .php?p=/product.php?cid=23&pid=234

    the problem is that p = /product.php?cid=23&pid=234

    but since theres an & in there the browser sees it as
    p = /product.php?cid=23

    &

    pid = 234

    is there a way to put it all in quotes or something so it doesnt get seperated like that?
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  12. #12
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im not sure I get what you mean?? sorry!??

  13. #13
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mark_W
    You could foreach through the $_GET array and build up your own version of the $_SERVER['QUERY_STRING']
    could you explain to me how the proper way to do that would be?
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  14. #14
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think he meant something like:


    PHP Code:
     
    $query 
    '';
    $i 0;
     
    foreach(
    $_GET AS $key => $value)
    {
     
    $query .= (++$i 1) ? '&' .$key .'=' .$value '?' .$key .'=' .$value;
     
    }
     
    //to test output
    echo $query
    Silly

  15. #15
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do this....

    PHP Code:

    $qString 
    ="?";

    foreach (
    $_GET as $var => $value) {
        
    $qString .= $var."=".$value."&";
    }

    echo 
    $qString
    But you will end up with an extra & at the end, you could always use the substring function to trim this off though!

  16. #16
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe this?
    PHP Code:
    $qs 'p='.$_SERVER['PHP_SELF'].'&'.$_SERVER['QUERY_STRING'];
    echo 
    '<a href="script.php?'.$qs.'">link</a>'

  17. #17
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that still takes me back to the problem before that you didnt understand what i meant.

    let me try to explain better,

    the var p or $_GET['p'] contains the url of the previous page to send them back to after they log in.

    p contains this: /product.php?cid=23&pid=234

    but since the string that p contains, contains an &: .php?p=/product.php?cid=23&pid=234

    the broswer seperates it into 2 variables

    so when i need $_GET['p'] to return /product.php?cid=23&pid=234

    the broswer actually sees $_GET['p'] = /product.php?cid=23 and another $_GET['pid'] = 234

    get it?
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  18. #18
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if thats the case you may be better storing the previous page as a session variable. Rather than storing it in $_GET

    Then you can access it whenever you like with far more ease.

    If you have say :

    PHP Code:

    $_SESSION
    ['cur_page'] = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; 
    Then you can redirect them back by getting the value from the $_SESSION['cur_page'] variable! If you dont know much about session then you will need to call session_start on all the pages that need to make use of this session variable, and you will need to call it before any output is sent to the browser!

  19. #19
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey thanks alot man!
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  20. #20
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to run urlencode() on the url before using it in the query string. For example, a link should not look like:

    mypage.php?pid=/product.php?cid=23&pid=234

    it should look like:

    mypage.php? %2Fproduct.php%3Fcid%3D23%26pid%3D234

    which it will after you run it through urlencode().
    obviously you would then run this through urldecode() to find the actual url....

  21. #21
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by halfasleeps
    hey thanks alot man!
    No problem, its probably a better solution than using $_GET anyway, as $_GET is limited. It will also keep your urls nice and clean incase you want to use mod_rewrite or something in the future.

    Hope you can get it working how you want to now.


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
  •