HELP - Pretty simple PHP Question

Discussion in 'PHP' started by qardinal, Nov 14, 2008.

  1. #1
    I have a pretty simple question about PHP that I hope you'll be so kind as to help me out with.

    Basically, I'd like to be able to echo the text after a "?" mark on my page from the URL.

    So, if a user types in http://www.digitalpoint.com/?hey there

    I'd like to be able to display the words "hey there" on the page, or anything else that follows the "?" I've tried a script for doing this, but it treats all spaces like it would a URL, it shows "hey%20there", instead of "hey there".

    I much appreciate any help you can give, thank you very much in advance.
     
    qardinal, Nov 14, 2008 IP
  2. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    hmm... Don't know if that's possible.

    try to print_r($_GET)
     
    elias_sorensen, Nov 14, 2008 IP
    qardinal likes this.
  3. qardinal

    qardinal Peon

    Messages:
    333
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm sorry I'm pretty new to this, I don't really know what you mean by that.
     
    qardinal, Nov 14, 2008 IP
  4. logondotinfo

    logondotinfo Peon

    Messages:
    314
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you could make it easier on yourself. Make the urls print out liek this:

    www.mydomain.com?v=hey_there

    You can change the v= to whatever you like, just remember what it is, you can also if you like add in other variables:

    www.mydomain.com?v-hey_there&y=hey_back

    Then to echo out these into your content, you simply need to add:

    print($_GET[v]); //which will print hey_there
    print($_GET[y]); //which will print hey_back
    Code (markup):
    The above replies function print_r($GET); is a function to print the contents of an array (the array being between the brackets, and in theis instance the $_GET variables which is the name given to variables contained inside the url)

    Hope that helps.
     
    logondotinfo, Nov 14, 2008 IP
    qardinal likes this.
  5. sterex

    sterex Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The way the information is posted to the server by the URL is this: http://domain.ext/?name=value

    There is an easy way to redirect all your /?hey there requests via the mod_rewrite. Just add this rule in your .htaccess file in the root of your domain:

    RewriteRule ^domain\.ext/\?([a-z]+)$ /hi.php?name=$1

    Where hi.php is the file you use to echo the text. Your visitors will see the URL: http://domain.ext/?sterex however, all the action will be happening at 'hi.php?name=sterex' :)
     
    sterex, Nov 14, 2008 IP
    qardinal likes this.
  6. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Put this into a file:
    
    <?
    foreach ($_GET as $key=>$value) {
    	echo $key;
    }
    ?>
    
    PHP:
    E.g. if you put it into text.php, you can do it with text.php?hey

    Try it yourself: http://pastedrop.com/hey.php?hello
     
    elias_sorensen, Nov 14, 2008 IP
  7. qardinal

    qardinal Peon

    Messages:
    333
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #7
    BEAUTIFUL!!!! You've saved my life man. Or solved a very frustrating problem for me. Same difference. Thank you.
     
    qardinal, Nov 14, 2008 IP
  8. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Yeah, but if you want to do it a shorter way, without an equalization mark, use the code I wrote in #6.

    With logondotinfo's code, you have to use e.g. ?text=hello, with mine, you can do it with ?hello :)

    And you shouldn't use print($_GET[v]); too, use
    echo $_GET[v];
    PHP:
    instead.
     
    elias_sorensen, Nov 14, 2008 IP
  9. qardinal

    qardinal Peon

    Messages:
    333
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'll try both ways, whichever one works best. At least I know I've got this problem solved =D.
     
    qardinal, Nov 14, 2008 IP
  10. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    P.S.
    If you just want to get the stuff after the ? use $_SERVER['QUERY_STRING'];

    That would give you hey there out of http://domain.com/index.php?hey there
     
    atlantaazfinest, Nov 14, 2008 IP
  11. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #11
    $_SERVER['QUERY_STRING'] will do
     
    ads2help, Nov 14, 2008 IP
  12. qardinal

    qardinal Peon

    Messages:
    333
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #12

    I tried this one, but it adds an underscore between each word, eg "hey_there"

    I'm correctly trying all suggestions, thanks.
     
    qardinal, Nov 14, 2008 IP
  13. i-h8-scammers

    i-h8-scammers Banned

    Messages:
    140
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    i learned php on here tizag.com/phpT/forloop.php

    hope that helps
     
    i-h8-scammers, Nov 14, 2008 IP
  14. mediabitdesign

    mediabitdesign Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    <?php
    $word= explode("?", $_SERVER['REQUEST_URI']);
    $word = $word[1];
    echo $word;
    ?>
    Try this
     
    mediabitdesign, Nov 14, 2008 IP
  15. mediabitdesign

    mediabitdesign Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    <?php
    $word= explode("?", $_SERVER['REQUEST_URI']);
    $word = urldecode($word[1]);
    echo $word;?>
    Better still if you have spaces then you will get urlencode, so urldecode it
     
    mediabitdesign, Nov 14, 2008 IP
  16. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #16
    
    <?
    foreach ($_GET as $key=>$value) { 
    echo str_replace("_","",$key);
    }
    ?>
    
    PHP:
     
    elias_sorensen, Nov 14, 2008 IP
  17. mediabitdesign

    mediabitdesign Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #17
    <?
    foreach ($_GET as $key=>$value) {
    echo str_replace("_"," ",$key);
    }
    ?>
    You would have to replace to "_" with " "
     
    mediabitdesign, Nov 14, 2008 IP
  18. mediabitdesign

    mediabitdesign Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    <?php
    $word= explode("?", $_SERVER['REQUEST_URI']);
    $word = urldecode($word[1]);
    echo $word;
    ?>
    This would take about half the time to process than the foreach
     
    mediabitdesign, Nov 14, 2008 IP
    qardinal likes this.
  19. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #19
    No, he don't. My code was 100% correct. He want's to replace _ with a line space. It does that. Test it yourself.

    The correct code is still:
    
    <?
    foreach ($_GET as $key=>$value) { 
    	echo str_replace("_"," ",$key);
    }
    ?>
    
    PHP:
     
    elias_sorensen, Nov 14, 2008 IP
  20. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #20
    Half the time of... 2 milliseconds?
     
    elias_sorensen, Nov 14, 2008 IP