Query string using HTML

by 8 replies
9
Hi warriors!

Got a programming question and would appreciate any advice.

I have a HTML site and the link that is given to my affiliates look something like this:

http://mysite.com/?10001

now how do i retrieve that query string using html? (not sure if I've phrased this correctly)

meaning to say, i would like the "10001" to appear somewhere in my website.

would appreciate any advice...

thanks!

Calvin
#programming #html #query #string
  • Normally, a query string has 2 parts, a field name and it's value, separated by an equals sign, like...

    http://mysite.com/?aff=10001

    In the above example, "aff" would be the field name, and "10001" is its value.

    So to extract the value of "aff", all you need is a little bit of PHP...

    <?php $aff_value = $_GET['aff']; ?>

    Then you can display that value anywhere you need to on the page...

    <?php echo $aff_value; ?>

    Of course, you will need to give that page a PHP extension, but that's pretty much all there is to it.
  • http://mysite.com/?10001

    [php
    <?php
    $url="http://mysite.com/?10001";
    $parts=explode($url, '/?');
    $string=$parts['1'];
    echo $string;
    ?>
    [/php]
    • [1] reply
    • ^ That will only work if the URL value always ends with "10001". If you need to account for varying values at the end of the URL (which I'm assuming is what the OP wants to do), then you'd need to change $url to this in order for that code to work...

      $url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

      Also, the explode line should be this instead...

      $parts = explode('?', $url);

      So, altogether...

      <?php
      $url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
      $parts = explode('?', $url);
      $string = $parts['1'];
      echo $string;
      ?>
  • None of which you should do without escaping the data before putting it into the page. You don't want to allow arbitrary people to link to your page injecting whatever scripts or other garbage they so choose. Read about cross site scripting before you inject any content in query strings into your page.
    • [1] reply
    • Agree with wayfarer - you must check $_GET or $_POST data before using it in your query. Brandon gives you an overview how you can achieve to get parameters from URL but you'll need to use more secure approach.
  • Banned
    You'll never get the query string with HTML, try PHP.

    get google search query string "php"
  • thanks!

    But what if my affiliate links come in this format: http://www.mysite.com/?10001

    so how should the codes be inserted?

    Calvin
    • [1] reply
    • Brandon already showed you complete code:

      <?php
      $url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
      $parts = explode('?', $url);
      $string = $parts['1'];
      echo $string;
      ?>

Next Topics on Trending Feed