...

View Full Version : How do i GET a variable from within a variable



mobimad
03-17-2010, 03:38 PM
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/redirect.php?p=id=66&tabs=Walls&page=/find.php?&search=bobs&site=sportG

I hope that makes sense, here is the 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";
?>

Fou-Lu
03-17-2010, 04:22 PM
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

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

var_dump($rawVars);
?>

mobimad
03-17-2010, 04:43 PM
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

Fou-Lu
03-17-2010, 05:13 PM
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.

mobimad
03-17-2010, 05:36 PM
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



<?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";
?>

abduraooft
03-17-2010, 06:04 PM
I think the following is what Fou-Lu suggested.
<?php
$url=$_SERVER['REQUEST_URI'];
$in = rawurldecode($url);
$rawVars = array();
parse_str(parse_url($in, PHP_URL_QUERY), $rawVars);
var_dump($rawVars);
?>
<?php
echo $rawVars['site'];
?>

MattF
03-17-2010, 06:11 PM
Just try, for testing, with this content alone in that file, and post the output.



<?php

exit(print_r($_GET));

?>

mobimad
03-17-2010, 06:28 PM
Thanks Abduraooft

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

Thanks
Jenna



<?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
}

?>

Fou-Lu
03-17-2010, 06:32 PM
Thanks Abduraooft

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

Thanks
Jenna



<?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).


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'];?

mobimad
03-17-2010, 06:34 PM
@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

Fou-Lu
03-17-2010, 06:36 PM
@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:



$in = isset($_GET['p']) ? $_GET['p'] : '';
$rawVars = array();
parse_str($in, $rawVars);

MattF
03-17-2010, 06:49 PM
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):



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:



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.

mobimad
03-17-2010, 06:50 PM
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 :)



<?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");
}

?>

MattF
03-17-2010, 06:56 PM
Just after your opening PHP tag at the top of the script, add this line:

ob_start();

mobimad
03-17-2010, 07:06 PM
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

Fou-Lu
03-17-2010, 07:09 PM
No, thats a cheezy method to fix an underlying problem. ob_start initializes the output buffer so you can throw output before putting headers. Instead, fix the underlying problem.
From the looks of the error message, the problem is caused right here:


parse_str(parse_url($in, PHP_URL_QUERY), $rawVars);
?>
<?php
if($rawVars['site'] == 'sportG') {
$host = $_SERVER['HTTP_HOST'];



Remove the ?><?php section in there. The newline is creating output which pushes all headers through, which is why you can no longer send them.
Also, add an exit() immediately following the header("Location....") calls.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum