Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts

    How do i GET a variable from within a variable

    Hi,

    I am using rawurldecode to decode a URL so that i can GET a variable from the URL once decoded.

    But i having trouble actually obtaining a variable from within the decoded URL variable.

    I cannot seem to get the variable which i want, which is $site=site from the URL variable $decode_url

    The url would look like this:

    http://domain.com/tree/manage/redire...bs&site=sportG

    I hope that makes sense, here is the code

    Code:
    <?php
    $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $raw_url = "$url"; 
    $decoded_url = rawurldecode($raw_url);
    echo "$decoded_url";
    ?>
    <?php
    $site=$_GET['site'];
    echo "$site";
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The URL you posted; thats what is actually encoded and than decoded correct?
    Strip out the querystring information and then parse it. Do not allow it to ever hit global level; use a second parameter in parse_str:
    PHP Code:
    <?php

    $in 
    rawurldecode($someRawData);
    $rawVars = array();
    parse_str(parse_url($inPHP_URL_QUERY), $rawVars);

    var_dump($rawVars);
    ?>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi,
    Yes the url posted is what has been decoded, and that element works fine, i just dont know how to obtain/GET the site=sportG from the decoded url.

    I'm new at php and would normally use

    $site=$_GET['site'];
    echo "$site";

    but using this would still effectively take it from the non decoded address bar url and not from the $decoded_url

    How do i GET the site=parameter from the $decoded_url and echo it?

    Thanks In-Advance
    Jenna

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by mobimad View Post
    Hi,
    Yes the url posted is what has been decoded, and that element works fine, i just dont know how to obtain/GET the site=sportG from the decoded url.

    I'm new at php and would normally use

    $site=$_GET['site'];
    echo "$site";

    but using this would still effectively take it from the non decoded address bar url and not from the $decoded_url

    How do i GET the site=parameter from the $decoded_url and echo it?

    Thanks In-Advance
    Jenna
    Look above, the $rawVars is an array that should contain an offsite for 'site'.
    Do not allow this passed url variables to obtain global scope or link to a superglobal variable for security reasons. You will obtain them from the extracted array instead to prevent any overwrite of controlled variables.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi, Sorry but i am new to php and i havent studied arrays yet, so your post is somewhat confusing, would it be possible to show me exactly what i need to do. Where would that code go to get the 'site' variable.

    $decode_url = domain.com/tree/manage/redirect.php?p=id=84&tabs=Balls&site=sportG


    Thanks so far Fou-Lu

    i could add the code you provided, but i have no idea where it should go, or if i need to alter it in any way.

    Thanks
    Jenna

    Code:
    <?php
    $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $raw_url = "$url"; 
    $decoded_url = rawurldecode($raw_url);
    ?>
    <?php 
    $in = rawurldecode($someRawData); 
    $rawVars = array(); 
    parse_str(parse_url($in, PHP_URL_QUERY), $rawVars); 
    var_dump($rawVars); 
    ?> 
    <?php
    $site=$_GET['site'];
    echo "$site";
    ?>

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I think the following is what Fou-Lu suggested.
    PHP Code:
    <?php 
    $url
    =$_SERVER['REQUEST_URI'];
    $in rawurldecode($url); 
    $rawVars = array(); 
    parse_str(parse_url($inPHP_URL_QUERY), $rawVars); 
    var_dump($rawVars); 
    ?> 
    <?php
    echo $rawVars['site'];
    ?>
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    mobimad (03-17-2010)

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Just try, for testing, with this content alone in that file, and post the output.

    Code:
    <?php
    
    exit(print_r($_GET));
    
    ?>

  • #8
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thanks Abduraooft

    if i now wanted to use an if statement would it the following work:

    Thanks
    Jenna

    Code:
    <?php 
    $url=$_SERVER['REQUEST_URI'];
    $in = rawurldecode($url); 
    $rawVars = array(); 
    parse_str(parse_url($in, PHP_URL_QUERY), $rawVars); 
     ?> 
    <?php
    if($rawVars['site'] == 'sportG') { 
    do something here
    }
    if($rawVars['site'] == 'sportX') { 
    do something here
    }
    
    ?>

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by mobimad View Post
    Thanks Abduraooft

    if i now wanted to use an if statement would it the following work:

    Thanks
    Jenna

    Code:
    <?php 
    $url=$_SERVER['REQUEST_URI'];
    $in = rawurldecode($url); 
    $rawVars = array(); 
    parse_str(parse_url($in, PHP_URL_QUERY), $rawVars); 
     ?> 
    <?php
    if($rawVars['site'] == 'sportG') { 
    do something here
    }
    if($rawVars['site'] == 'sportX') { 
    do something here
    }
    
    ?>
    If everything goes correctly and the site exists in $rawVars, then yep you can check that way (or by using strcmp($rawVars['site'], 'sportG') === 0).

    Edit:
    BTW, I'm not certain that the REQUEST_URI is what we're looking for here. I was kinda under the impression that would be $url = $_GET['site'];?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    @mattF

    this is the outcome

    Array ( [p] => id=84&table=Balls&page=/prev_page.php?&find=bond&site=sportG [c] => 0 [u] => 35317546 [t] => 91669BA082824EDS4B9C6724AC131132 [n] => GBR_BTBROADBAND [r] => GBR [cr] => gbr [x] => -1 [e] => 0 [b] => 4226864287 ) 1

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by mobimad View Post
    @mattF

    this is the outcome

    Array ( [p] => id=84&table=Balls&page=/prev_page.php?&find=bond&site=sportG [c] => 0 [u] => 35317546 [t] => 91669BA082824EDS4B9C6724AC131132 [n] => GBR_BTBROADBAND [r] => GBR [cr] => gbr [x] => -1 [e] => 0 [b] => 4226864287 ) 1
    The URL isn't necessary to retrieve at all then, $_GET['p'] contains the encoded string you want, which can be dropped directly into a parse_str:
    PHP Code:

    $in 
    = isset($_GET['p']) ? $_GET['p'] : '';
    $rawVars = array();
    parse_str($in$rawVars); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mobimad (03-17-2010)

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Fou-Lu View Post
    The URL isn't necessary to retrieve at all then, $_GET['p'] contains the encoded string you want, which can be dropped directly into a parse_str:
    Even that shouldn't be necessary. The URI in the request is seriously screwed. Cleaning that up, (which would be the best method), should lead to $_GET['site'] being a valid var.

    At the moment, mobimad, you have this as the request URI, (working from the link in your initial post):

    Code:
    http://domain.com/tree/manage/redirect.php?p=id=66&tabs=Walls&page=/find.php?&search=bobs&site=sportG
    That should be formatted like this:

    Code:
    http://domain.com/tree/manage/redirect.php?id=66&tabs=Walls&page=/find.php&search=bobs&site=sportG
    If it's formatted as above, all of the GET array vars should be available via their respective keys instead of being broken like they are at the moment. It appears to be getting intepreted as one complete string rather than separate parts.
    Last edited by MattF; 03-17-2010 at 05:51 PM.

  • #13
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    OK, i am able to get the site= from the url now,

    thankyou....

    but when i run my code i get an error:

    Warning: Cannot modify header information - headers already sent by (output started at /home/smart/public_html/domain.com/tap/manage/redirect.php:6) in /home/smart/public_html/domain.com/tap/manage/redirect.php on line 28

    I am now trying to use an if statement using the site= so if site=sportG redirect to this page

    or if site=sportF redirect to this page.

    How do i overcome this?

    I'm nearly there

    Code:
    <?php 
    $url=$_SERVER['REQUEST_URI'];
    $in = rawurldecode($url); 
    $rawVars = array(); 
    parse_str(parse_url($in, PHP_URL_QUERY), $rawVars); 
     ?> 
    <?php
    if($rawVars['site'] == 'sportG') { 
    $host  = $_SERVER['HTTP_HOST'];
    $extra = 'download.php?';
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $unencodedP = urldecode(stripslashes($_REQUEST['p']));
    $costP = ($_REQUEST['c']);
    $userP = ($_REQUEST['u']);
    $networkP = ($_REQUEST['n']);
    $regionP = ($_REQUEST['r']);
    header("Location: http://$host$uri/$extra$unencodedP&n=$networkP&u=$userP&r=$regionP&c=$costP");
    }
    if($rawVars['site'] == 'sportF') { 
    $host  = $_SERVER['HTTP_HOST'];
    $extra = 'domain.com/download.php?';
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $unencodedP = urldecode(stripslashes($_REQUEST['p']));
    $costP = ($_REQUEST['c']);
    $userP = ($_REQUEST['u']);
    $networkP = ($_REQUEST['n']);
    $regionP = ($_REQUEST['r']);
    header("Location: http://$extra$unencodedP&n=$networkP&u=$userP&r=$regionP&c=$costP");
    }
    
    ?>

  • #14
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Just after your opening PHP tag at the top of the script, add this line:

    ob_start();

  • Users who have thanked MattF for this post:

    mobimad (03-17-2010)

  • #15
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thanks Everyone

    I feel a wait off my shoulders........for now

    MattF just for future reference, how did the ob_start(); solve this error?

    Also does this code look ok, i mean the best way to do it?

    Thanks a million
    Jenna


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •