faqts : Computers : Programming : Languages : PHP : Common Problems : URLs

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

24 of 28 people (86%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do I pass a sting in a url eg. $MyString='Have a nice day'?

Dec 17th, 2007 02:20
dman, Philip Olson, Richard Heyes, William Holt, Arcady Novosyolov, Christian Hope, Nathan Wallace,


Passing information through GET is easy:
  <?php
    $str = 'have a nice day';
    $enc_str = urlencode($str);
    echo "<a href='pass.php?str=$str'>go here</a>";
  ?>
The key is encoding the spaces.  One can also use rawurlencode(), see 
the manual entries for more details:
  http://www.php.net/urlencode
In short, urlencode() will turn your spaces into plus signs, so the URL 
magically becomes compliant:
  pass.php?str=have+a+nice+day
So, in pass.php, you can access this information like any other GET 
variable, which is:
  <?php
    // Has worked since PHP 3
    print $HTTP_GET_VARS['str'];
    // Has worked since PHP 4.1.0
    print $_GET['str'];
    // Works if the PHP directive register_globals = on
    // As of 4.2.0, register_globals defaults to off.
    print $str;
  ?>